In the process of program development, regular tasks are often used to achieve some functions, such as:
- The system relies on non-core data from external systems and can be synchronized periodically (once a day)
- In Spring Boot, you can use the @scheduled annotation to quickly implement Scheduled tasks.
The @Scheduled annotations are supported in three ways:
- FixedRate fixed frequency
- FixedDelay fixedDelay
- Cron custom cron expression so next, let’s explain how to implement it.
fixedRate
First, we need to add @enablesCheduling annotation to the startup class:
package com.zwwhnly.springbootdemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @ SpringBootApplication @ EnableScheduling public class SpringbootdemoApplication {/ * * / other code public static void main(String[] args) { SpringApplication.run(SpringbootdemoApplication.class, args); }}Copy the code
Then create a new test class TestSchedule with the @Component annotation.
Finally, add a test method with the @scheduled annotation. To see the effect, we have printed the current system time every 5 seconds, as follows:
package com.zwwhnly.springbootdemo;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class TestSchedule {
private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // The @scheduled (fixedRate = 5000) public void is executed every 5 secondstestFixedRate() {
System.out.println("Current time:"+ simpleDateFormat.format(new Date())); }}Copy the code
In a real project, however, it is unlikely to be so regular, such as methods that take longer than 5 seconds to execute (which should be common), but how does the program execute at that time?
Let’s change the program so that the method takes longer than 5 seconds to execute:
// The @scheduled (fixedRate = 5000) public void is executed every 5 secondstestFixedRate() {
System.out.println("Current time:"+ simpleDateFormat.format(new Date())); try { Thread.sleep(6000); } catch (InterruptedException e) { e.printStackTrace(); }}Copy the code
We can see that the output time is now every 6 seconds, from which we can conclude:
- If a method takes longer to execute than a defined fixed frequency (say, 5 seconds), the next task is executed immediately after the last one.
fixedDelay
Add a new method, testFixedDelay. Here we use fixedDelay:
// The interval between the end of the last task and the start of the next task is 5se_scheduled (fixedDelay = 5000) public voidtestFixedDelay()
{
System.out.println("Current time:" + simpleDateFormat.format(new Date()));
}
Copy the code
In case you’re wondering, this is the same result as fixedRate, executed every 5 seconds.
Spring Boot supports both fixedRate and fixedDelay.
Differences between fixedRate and fixedDelay
To give you a clearer view of the difference between fixedRate and fixedDelay, let’s change the fixedDelay method to take longer than 5 seconds:
// The interval between the end of the last task and the start of the next task is 5se_scheduled (fixedDelay = 5000) public voidtestFixedDelay() {
System.out.println("Current time:"+ simpleDateFormat.format(new Date())); try { Thread.sleep(6000); } catch (InterruptedException e) { e.printStackTrace(); }}Copy the code
Now the interval between the two output times is 11 seconds, from which we can draw the conclusion:
With fixedDelay, after the last task is executed, the next task is executed 5 seconds later. The difference between fixedRate and fixedDelay is as follows:
- FixedRate is executed at a fixed frequency, and fixedDelay is executed at a fixed time.
cron
Cron expressions are more flexible than the two methods described above, because they basically meet the configuration requirements of various scenarios, such as fixed frequency execution, fixed point in time execution, etc.
First, we use cron expressions to execute every 5 seconds in the above example:
@Scheduled(cron = "0/5 * * * *?")
public void testCron() {
System.out.println("Current time:" + simpleDateFormat.format(new Date()));
}
Copy the code
Manual setup method takes more than 5 seconds to execute:
@Scheduled(cron = "0/5 * * * *?")
public void testCron() {
System.out.println("Current time:"+ simpleDateFormat.format(new Date())); try { Thread.sleep(6000); } catch (InterruptedException e) { e.printStackTrace(); }}Copy the code