1. 程式人生 > >spring註解 @Scheduled(cron = "0 0 1 * * *")的使用來實現定時的執行任務

spring註解 @Scheduled(cron = "0 0 1 * * *")的使用來實現定時的執行任務

<span style="font-size:14px;">初次接觸定時類的小程式,還是走了很多的彎路,如今終於搞定了,總結如下:</span>
<span style="font-size:14px;">import com.activityvip.api.service.SecurityBlockAccountService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.springframework.scheduling.annotation.Scheduled;

/**
 *
 */
@Service
public class SecurityAccountUtil {
    private static Logger logger = Logger.getLogger(SecurityAccountUtil.class);
    private static final List<String> localhost = Arrays.asList("emidas-activityvip-web02.nh", "emidas-activityvip-web01.beta");
    @Autowired
    SecurityBlockService securityBlockService;
    @Autowired
    SecurityBlockAccountService securityBlockAccountService;
    @Autowired
    SecurityBlockAccountDao securityBlockAccountDao;

    @Scheduled(cron = "0 0 1 * * *")
    public void run() {
        if (checkoutHostName()) {
            try {

                logger.info("thread is running");
                DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String lastTime = securityBlockAccountService.getLastTime();
                Date myDate1 = dateFormat1.parse(lastTime);

                Date dAfter = getNextDay(myDate1);
                Integer numByTime = securityBlockService.getAllNumByTime(myDate1, dAfter);
                Integer accountByTime = securityBlockService.getAllAccountByTime(myDate1, dAfter);
                securityBlockAccountService.insertAccountNum(numByTime, accountByTime, dAfter);

                logger.info("thread is end");
            } catch (Exception e) {
                logger.info("SecurityAccountUtil:" + e.getMessage());
                e.printStackTrace();
            }
        }
    }


    /**
     * 獲取當前時間後一天的時間
     *
     * @param date
     * @return
     */
    public static Date getNextDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        date = calendar.getTime();
        return date;
    }

    /**
     * 過濾主機名
     *
     * @return
     */
    protected boolean checkoutHostName() {
        String hostName = null;
        try {
            hostName = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            logger.info(e);
            hostName = "localhost";
        }

        return localhost.contains(hostName);
    }
}</span>

最近實習寫了一個小小的定時統計的程式,用的是spring的 @Scheduled註解,該註解依賴下面的jar包

<span style="font-size:14px;"> <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency></span>
在使用該註解以前請做好以下準備工作,配置好相應的xm檔案。

配置定時註解的步驟:http://blog.csdn.net/sd4000784/article/details/7745947

下面給出cron引數中各個引數的含義:

CRON表示式    含義 
"0 0 12 * * ?"    每天中午十二點觸發 
"0 15 10 ? * *"    每天早上10:15觸發 
"0 15 10 * * ?"    每天早上10:15觸發 
"0 15 10 * * ? *"    每天早上10:15觸發 
"0 15 10 * * ? 2005"    2005年的每天早上10:15觸發 
"0 * 14 * * ?"    每天從下午2點開始到2點59分每分鐘一次觸發 


"0 0/5 14 * * ?"    每天從下午2點開始到2:55分結束每5分鐘一次觸發 
"0 0/5 14,18 * * ?"    每天的下午2點至2:55和6點至6點55分兩個時間段內每5分鐘一次觸發 
"0 0-5 14 * * ?"    每天14:00至14:05每分鐘一次觸發 
"0 10,44 14 ? 3 WED"    三月的每週三的14:10和14:44觸發 
"0 15 10 ? * MON-FRI"    每個週一、週二、週三、週四、週五的10:15觸發