How Spring plans tasks to be used
This article is to share with you about how Spring planning tasks are used. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The details are as follows:
A little bit of eye contact
Starting with Spring3.1, the implementation of scheduled tasks in Spring becomes extraordinarily simple. Only the next two steps are needed.
1 enable support for scheduled tasks by annotating @ EnableScheduling on the configuration class.
2 annotate @ Scheduled on the method in which you want to perform the scheduled task, declaring that this is a scheduled task.
Spring supports many types of scheduled tasks through @ Scheduled, including cron, fixDelay, fixRate, and so on.
Second actual combat
1 configuration class
Package com.wisely.highlight_spring4.ch4.taskscheduler;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.EnableScheduling;@Configuration@ComponentScan ("com.wisely.highlight_spring4.ch4.taskscheduler") @ EnableScheduling / / 1public class TaskSchedulerConfig {}
2 scheduled task execution class
Package com.wisely.highlight_spring4.ch4.taskscheduler;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Service;@Servicepublic class ScheduledTaskService {private static final SimpleDateFormat dateFormat = new SimpleDateFormat ("HH:mm:ss"); @ Scheduled (fixedRate = 5000) / / this method is a scheduled task and is executed at regular intervals using the fixedRate attribute. Public void reportCurrentTime () {System.out.println ("execute every five seconds" + dateFormat.format (new Date ();} @ Scheduled (cron = "0 28 11? * *") / / execute public void fixTimeExecution () {System.out.println ("at specified time" + dateFormat.format (new Date ()) + "execute") at 11:28 every day;}}
3 main class
Package com.wisely.highlight_spring4.ch4.taskscheduler;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main {public static void main (String [] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (TaskSchedulerConfig.class);}}
Three running results
19:58:50 is performed every five seconds
19:58:55 is performed every five seconds
19:59:00 is performed every five seconds
19:59:05 is performed every five seconds
19:59:10 is performed every five seconds
19:59:15 is performed every five seconds
19:59:20 is performed every five seconds
19:59:25 is performed every five seconds
19:59:30 is performed every five seconds
19:59:35 is performed every five seconds
19:59:40 is performed every five seconds
19:59:45 is performed every five seconds
19:59:50 is performed every five seconds
19:59:55 is performed every five seconds
20:00:00 is performed every five seconds
20:00:05 is performed every five seconds
Thank you for reading! This is the end of this article on "how to use Spring Planning tasks". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!