SpringBoot integration (a, timed task task)

SpringBoot integrate regular tasks task is very simple, is divided into the following three steps:

1. Start classes with the @EnableScheduling comment

2. On the controller classes with the @Component comment

3. Add @Scheduled annotation on the controller method to

After starting the program, it will automatically begin the task

 

SpringBoot integrated mission task timer

 

Start Class Code

package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;


@SpringBootApplication
//开启定时任务
@EnableScheduling
public class DemoApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

controller Code

@Component
 public  class cronjobscontroller { 

    Private  static  Final the SimpleDateFormat the dataFormat = new new the SimpleDateFormat ( "HH: mm: SS" ); 

    // every three seconds to perform a task 
    @Scheduled (fixedRate = 3000 )
     public  void Cronjobs () { 
        the System.out. the println ( "time is:" + dataFormat.format ( new new a Date ())); 
    } 

}

Console effect

 

 

The method of determining the timing SpringBoot timer task rules Cron

 

Online cron Expression Builder Address: http: //cron.qqe2.com/

Note: Here we must note that this tool 'in' is not supported. cron-digit strict requirements 6.

In the above example we use @Scheduled (fixedRate = 3000 ) manner, meaning performed once every three seconds. So cron more powerful, its use is as follows:

For example: I want to perform once every 1 second, then the configuration is as follows

Here are some common cron:

1 second execution time: 0/1 * * * *?

1 minute execution time: 0 * / 1 * * *?

1 hour From: 00 0/1 * *?

 

0012 * *? 12:00 each day trigger
01510? * * Every day 10:15 triggering
01510 * *? 2005 2005 daily 10:15 triggering
0 * 14 * *? Every afternoon 2:00 to 2 point 59 cents per minute trigger
0 0/5 14 * *? every afternoon 2:00 to 2:59 (the whole point of beginning, every 5 minutes trigger)
0 0/5 * 14, 18 *? 2:00 every afternoon to 2:59 (the whole point of beginning, trigger every 5 minutes)
(the whole point of beginning, trigger every 5 minutes) every afternoon 18:00 to 18:59
00-514 * *? 2:00 every afternoon to 2:05 trigger per minute
0 10,44 14? 3 WED 3 month of every Wednesday afternoon 2:10 and 2:44 trigger (special circumstances, at a time settings, perform two or more times case)
? 0 59 2 * 5 per week FRI 2:59 trigger;
? 0 15 10 * MON-FRI every day from Monday to Friday morning 10:15 triggering
0151015 * 15th of every month? 10:15 triggering
0 15 10 L *? last day of each month 10:15 triggering
0 15 10? * 6L last week of each month Friday 10:15 triggering
0 15 10? * 6L 2002-2005 from 2005 to 2002 per month last week Friday 10:15 triggering
01510? * 6 # 3 of the third week of each month starting Friday trigger
0012 1/5 *? Noon every first trigger once every 5 days
011,111,111? Every November 11th 11:11 Trigger (Singles)

 

 

 

 

 

reference:

1. Mu-class network video

 

Continually updated! ! !

 

Guess you like

Origin www.cnblogs.com/flyinghome/p/12494053.html