Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to schedule tasks through spring

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to schedule tasks through spring". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to schedule tasks through spring.

Construction project

Create a Springboot project and add @ EnableScheduling to its program entry to start the scheduling task.

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

Create a scheduled task

Create a scheduled task and print the current time on the console every 5s.

@ Componentpublic class ScheduledTasks {private static final Logger log = LoggerFactory.getLogger (ScheduledTasks.class); private static final SimpleDateFormat dateFormat = new SimpleDateFormat ("HH:mm:ss"); @ Scheduled (fixedRate = 5000) public void reportCurrentTime () {log.info ("The time is now {}", dateFormat.format (new Date ());}} indicates that the method is a scheduling task by annotating the method with @ Scheduled. @ Scheduled (fixedRate=5000): execute @ Scheduled 5 seconds after the last execution time point (fixedDelay = 5000): execute @ Scheduled (initialDelay=1000, fixedRate=5000) 5 seconds after the last execution completion time point: execute after 1 second delay for the first time, and then execute @ Scheduled (cron= "/ 5") every 5 seconds according to fixedRate rules: define rules through cron expressions, what is cro expressions, and use your own search engine. The test starts the springboot project, and the console prints the current time in less than 5 seconds. 2017-04-29 17 com.forezp.task.ScheduledTasks com.forezp.task.ScheduledTasks: The time is now 17 com.forezp.task.ScheduledTasks 372017-04-29 17 com.forezp.task.ScheduledTasks 42.671 INFO 677-[pool-1-thread-1] com.forezp.task.ScheduledTasks: The time is now 17 com.forezp.task.ScheduledTasks 422017-04-29 17 Switzerland 47.672 INFO 677-[pool-1-thread-1] com.forezp.task.ScheduledTasks: The time is now 17 : 39 com.forezp.task.ScheduledTasks 472017-04-29 1715 INFO 3915 INFO 677-[pool-1-thread-1] com.forezp.task.ScheduledTasks: The time is now here at 17:39:52 I believe you have a deeper understanding of "how to schedule tasks through spring". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report