Spring定时任务,不重启,动态修改

..........
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Created by Extreme on 2017/8/7.
 */
@Component
@EnableScheduling
public class ImgCompressTask implements SchedulingConfigurer{
    //job日志
    private final static Logger logger = LoggerFactory.getLogger(ImgCompressTask.class);

    @Autowired
    private ImgCompressQueneService imgCompressQueneService;


    private static DictionaryService dictionaryService = SpringContextUtil.getBean("dictionaryServiceImpl");

    private static String cron;

    private static long sleepTime;

    public ImgCompressTask() {
        //初始化定时周期
        cron = dictionaryService.
                findDictionaryByCode("ImgCompressCircle").get(0).getDictionaryValue();

        //初始化休眠时间
        String sleepTimeString = dictionaryService.
                                  findDictionaryByCode("ImgCompressListenThreadSleepTime").get(0).getDictionaryValue();

        sleepTime = Long.valueOf(sleepTimeString);
        //开启监听线程,监听cron在数据库的变化
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    try {
                        //每隔多长时间查询一次数据库,判断是否发生变化,10                        Thread.sleep(sleepTime);
                    } catch (InterruptedException e) {
                       logger.error("taskThread error",e);
                    }
                    //从数据库获取最新任务值
                    String latestCron = dictionaryService.
                            findDictionaryByCode("ImgCompressCircle").get(0).getDictionaryValue();
                    if (!latestCron.equals(cron)) {
                        //修改
                        cron = latestCron;
                    }
                }
            }
        }).start();
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {

        Runnable task = new Runnable() {
            @Override
            public void run() {
                try {
                    //图片压缩任务
                    imgCompressQueneService.updateCompress();
                }catch(Exception e){
                    logger.error("imgCompress error",e);
                }
            }
        };

        Trigger trigger = new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                //任务触发,可修改任务的执行周期.
                CronTrigger trigger = new CronTrigger(cron);
                Date nextExec = trigger.nextExecutionTime(triggerContext);
                return nextExec;
            }
        };

        scheduledTaskRegistrar.addTriggerTask(task, trigger);
    }
}

你可能感兴趣的:(spring)