In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.