Handling Schedule timing tasks in Spring Boot projects

For projects built with Spring Boot, generally speaking, in actual projects, in order to improve the responsiveness of services, we generally do it through load balancing or reverse proxying multiple nodes. In layman's terms, we generally deploy multiple instances of the project, or deploy multiple copies, with different startup ports for each instance. But the code for each instance is actually the same. If we write timed tasks in our project, we will face a problem. For example, if we deploy 3 instances, as soon as the three instances are started, all the timed tasks will be started. Then at the same time point, the timed tasks will be started. It will be executed together, that is, it will be executed 3 times, which is likely to cause errors in our business.

 

At this time, we have several simple ways to deal with it:

1. Add custom configuration to the configuration file and control it through switches: for example, add: schedule=enable, schedule=disable, so that in our actual code, we are making judgments, that is, we can achieve through configuration, there is only one The instance actually executes the scheduled task, and the other instance does not execute it. However, this method actually starts the scheduled tasks, but during the execution, we manually make judgments and execute the real processing logic.

 

2. Logic separation means that we write the logic that we really need to process the scheduled tasks as a rest service or rpc service, and then we can create a separate scheduled task project. This project should not have any business code. It only has timing. The task function, when to start, or how often to start, after startup, through rest or RPC, the service that actually processes the logic is called. At the same time, we don't even need to create a new project, we can do it through linux cron. At the same time, this method also has an advantage. For example, sometimes, our scheduled task will have problems for some reason and it is not executed, then we can use curl or wget and many other methods to schedule the execution of the task again.

 

In spring boot, how to use timed tasks is relatively simple. According to the second method, in fact, I need to create a new project to complete the function of timed tasks. In fact, we can create a new ordinary java project and introduce quartz to achieve it.

In spring boot's entry class Application.java, schedule support is allowed

@SpringBootApplication
@EnableScheduling
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

 Then, create a new execution class Jobs.java

@Component
public class Jobs {
    public final static long ONE_Minute =  60 * 1000;

    @Scheduled(fixedDelay=ONE_Minute)
    public void fixedDelayJob(){
        System.out.println(Dates.format_yyyyMMddHHmmss(new Date())+" >>fixedDelay执行....");
    }

    @Scheduled(fixedRate=ONE_Minute)
    public void fixedRateJob(){
        System.out.println(Dates.format_yyyyMMddHHmmss(new Date())+" >>fixedRate执行....");
    }

    @Scheduled(cron="0 15 3 * * ?")
    public void cronJob(){
        System.out.println(Dates.format_yyyyMMddHHmmss(new Date())+" >>cron执行....");
    }
}

These are the simplest two ways, how many minutes to execute once, fixedDelay and fixedRate, the unit is milliseconds, so 1 minute is 60 seconds × 1000
The difference between them is that fixedRate is once every many minutes, no matter how much your business execution costs. time. I execute it once a minute, and fixedDelay is executed 1 minute after the task is executed. So according to the actual business, we will choose different ways.

And there is another type of timed task, such as execution at 3:15 every day, then we need to use another method: cron expression.

 

There are 7 digits in cron, but the last digit is the year, which can be left blank, so we can write 6 digits:

* The first digit, representing seconds, takes the value 0-59
* The second digit, indicating points, takes the value 0-59
* The third digit, indicating the hour, takes the value 0-23
* Fourth digit, date day/day, value 1-31
* Fifth digit, date month, value 1-12
* The sixth digit, week, value 1-7, Monday, Tuesday..., Note: not the first week, the second week
          Also: 1 for Sunday, 2 for Monday.
* The 7th is the year, can be left blank, the value is 1970-2099

 

In cron, there are also some special symbols with the following meanings:

(*) Asterisk: can be understood as the meaning of every, every second, every minute, every day, every month, every year...
(?) Question mark: The question mark can only appear in the two positions of the date and week, indicating that the value of this position is uncertain, at 3 o'clock every day
 Implementation, so the position of the sixth week, we do not need to pay attention, it is an uncertain value. At the same time: date and
 Weeks are two mutually exclusive elements, indicated by question marks that no value is specified. For example, January 10, such as Monday 1,
 If the position of the week is to specify another Tuesday, it is inconsistent.
(-) minus sign: express a range, such as using "10-12" in the hour field, it means from 10 to 12 o'clock,
 i.e. 10,11,12
(,) Comma: Express a list value, such as using "1,2,4" in the week field, it means Monday, Tuesday,
 Thursday
(/) slash: such as: x/y, x is the starting value, y is the step size, such as the first digit (second) 0/15 is, starting from 0 seconds,
 Every 15 seconds, the last is 0, 15, 30, 45, 60 Another: */y, which is equivalent to 0/y

 

Here are a few examples for you to verify:

0 0 3 * * ? Executed at 3 o'clock every day
0 5 3 * * ? Executed at 3:5 every day
0 5 3 ? * * Executed at 3:5 every day, same as above
0 5/10 3 * * ? Executed at 5 minutes, 15 minutes, 25 minutes, 35 minutes, 45 minutes, and 55 minutes at 3:00 every day
0 10 3 ? * 1 Every Sunday, executed at 3:10, Note: 1 means Sunday    
0 10 3 ? * 1#3 The third week of each month, executed on Sunday, # can only appear in the position of the week

 

Transfer: http://www.jianshu.com/p/ef18af5a9c1d

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326314265&siteId=291194637