Spring定時任務開發,實現動態修改時間引數,手動開啟關停等
阿新 • • 發佈:2018-12-30
@Service @Lazy(false) @EnableScheduling public class MyScheduler extends BaseService implements SchedulingConfigurer{ public static String cron = "0/2 * * * * ?"; int i =0; @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.addTriggerTask(new Runnable() { @Override public void run() { i++; // 需要實現的任務邏輯 logger.info("第"+(i)+"次開始執行操作... " +"時間:【" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS").format(new Date()) + "】"); } },new Trigger(){ @Override public Date nextExecutionTime(TriggerContext triggerContext) { //任務觸發,可修改任務的執行週期 CronTrigger trigger = new CronTrigger(cron); Date nextExec = trigger.nextExecutionTime(triggerContext);return nextExec; } }); } }