spring's regular tasks, super easy to use

Method 1: Use annotations

@Component
@EnableScheduling//可以在启动类上注解也可以在当前文件
public class TestJob {
 
    @Scheduled(cron = "0/10 * * * * ?")
    public void runfirst(){
        System.out.println("********first job is ok******");
    }
 
    @Scheduled(fixedRate = 1000 * 10)
    public void runsecend(){
        System.out.println("********second job is ok******");
    }
 
    @Scheduled(fixedDelay=1000)
    public void runThird(){
        System.out.println("********third job is ok*******");
    }
}

@EnableScheduling role annotation is found comment @Scheduled task and background. This method is used to label @Scheduled a timed task method cron, fixedRate, fixedDelay three is used to schedule the time of these three ways are executed once every ten seconds provided

fixedRate: Scheduled represent more than one call every time, regardless of whether the task executing the fixedDelay: indicates that the task after the implementation recall how often cron is an expression of the form to indicate time, excerpts from several other bloggers example, I believe that the basic look no problems.

Expression meaning:

“0 0 12 * * ?”                每天中午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期间的每1分钟触发
 
“0 0/5 14 * * ?”              在每天下午2点到下午2:55期间的每5分钟触发
 
“0 0/5 14,18 * * ?”           在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
 
“0 0-5 14 * * ?”              在每天下午2点到下午2:05期间的每1分钟触发
 
“0 10,44 14 ? 3 WED”          每年三月的星期三的下午2:10和2:44触发
 
“0 15 10 ? * MON-FRI”         周一至周五的上午10:15触发
 
“0 15 10 15 * ?”              每月15日上午10:15触发
 
“0 15 10 L * ?”               每月最后一日的上午10:15触发
 
“0 15 10 ? * 6L”              每月的最后一个星期五上午10:15触发
 
“0 15 10 ? * 6L 2002-2005”    2002年至2005年的每月的最后一个星期五上午10:15触发
 
“0 15 10 ? * 6#3”             每月的第三个星期五上午10:15触发
 
0 6 * * *                     每天早上6点
 
0 /2 * *                      每两个小时
 
0 23-7/2,8 * * *             晚上11点到早上8点之间每两个小时,早上八点
 
0 11 4 * 1-3                  每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点

Second way: using the interface SchedulingConfigurer:

@Configuration
@ComponentScan(value = "com.es.evaluation_teaching_wp.utils")
@EnableScheduling
public class Config implements SchedulingConfigurer {
   @Override
   public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {

       taskRegistrar.addFixedRateTask(new Runnable() {
           @Override
           public void run() {
               System.out.println("addFixedRateTask new Runnable() 定时任务执行了。");
           }
       },100);

       taskRegistrar.addTriggerTask(new Runnable() {
           @Override
           public void run() {
               System.out.println("addTriggerTask new Runnable() 定时任务执行了。");
           }
       }, new Trigger() {
           @Override
           public Date nextExecutionTime(TriggerContext triggerContext) {
               // 定时任务触发,可修改定时任务的执行周期
               CronTrigger trigger = new CronTrigger("*/2 * * * * ?");
               Date nextExecDate = trigger.nextExecutionTime(triggerContext);
               return nextExecDate;
           }
       });
   	
     //或者使用JDK 1.8的Lambda表达式
       taskRegistrar.addFixedRateTask(() -> System.out.println("Lambda表达式,执行定时任务1: " + new Date()), 1000);
       TriggerTask triggrtTask = new TriggerTask( // 任务内容.拉姆达表达式
               () -> {System.out.println("Lambda表达式,执行定时任务2: " + new Date());},
               // 设置触发器,这里是一个拉姆达表达式,传入的TriggerContext类型,返回的是Date类型
               triggerContext -> {
                   // 2.3 返回执行周期(Date)
                   return new CronTrigger("*/2 * * * * ?").nextExecutionTime(triggerContext);
               });

       taskRegistrar.addTriggerTask(triggrtTask);

   }


}

Multithreading configuration:

By default, is single-threaded mode SchedulingConfigurer use, if you need to configure multiple threads, you need to specify PoolSize, add the following code:

@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
	ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
	taskScheduler.setPoolSize(10);
	taskScheduler.initialize();
	taskRegistrar.setTaskScheduler(taskScheduler);
}

Dynamically modify timing rules:

Cron can define a variable set to provide a method, modify the value of the expression in accordance with various conditions cron from the outside;

/**

  • @description: dynamically modifying the timing parameters cron task
    * /
    @Component
    @EnableScheduling
    public class TestTask the implements SchedulingConfigurer {

    private static String cron = “0 0/1 * * * ?”;

    public void setCron(String cron) {
    this.cron = cron;
    }

    @Override
    public void configureTasks (ScheduledTaskRegistrar taskRegistrar) {
    taskRegistrar.addTriggerTask (the Runnable new new () {
    @Override
    public void RUN () {
    // business logic regular tasks
    System.out.println ( "1 dynamically modify the timing parameters cron job." )
    }
    }, the trigger new new () {
    @Override
    public nextExecutionTime a Date (triggerContext triggerContext) {
    // timer task trigger, modify the execution timing of the task period
    a CronTrigger a CronTrigger trigger new new = (the cron);
    a Date nextExecDate = trigger.nextExecutionTime (triggerContext) ;
    return nextExecDate;
    }
    });
    }
    }

2. The other is

https://blog.csdn.net/jianggujin/article/details/77937316

This method is based SchedulingConfigurer source, ScheduledTaskRegistrar capture instance class, additions and deletions by the timer task operating in TaskScheduler instance of the class, not the regular maintenance tasks using ScheduledTaskRegistrar.addTriggerTask method. So we need to write the code yourself regular maintenance task list, deletion, implementation code control task more complicated.

If you want to achieve timing strategies can be dynamically modified, it is recommended to use open source components Quartz.

Published 168 original articles · won praise 55 · views 120 000 +

Guess you like

Origin blog.csdn.net/eases_stone/article/details/104569111