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 in SpringBoot

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

Share

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

SpringBoot how to schedule tasks, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

This article will show you 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 scheduled tasks

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 ());}}

By adding @ Scheduled annotation to the method, it shows that the method is a scheduling task.

@ Scheduled (fixedRate = 5000): execute 5 seconds after the last execution time point

@ Scheduled (fixedDelay = 5000): execute 5 seconds after the last execution is completed

@ Scheduled (initialDelay=1000, fixedRate=5000): execute after the first delay of 1 second, and then execute every 5 seconds according to the rules of fixedRate

Scheduled (cron= "/ 5"): use cron expressions to define rules, what cro expressions are, and use your own search engine.

test

Start the springboot project, and the console prints out the current time in less than 5 seconds.

2017-04-29 17 com.forezp.task.ScheduledTasks 39Switzerland 37.672 INFO 677-- [pool-1-thread-1] com.forezp.task.ScheduledTasks: The time is now 17:39:37

2017-04-29 17 com.forezp.task.ScheduledTasks 39 INFO 42.671-- [pool-1-thread-1] com.forezp.task.ScheduledTasks: The time is now 17:39:42

2017-04-29 17 com.forezp.task.ScheduledTasks 39 com.forezp.task.ScheduledTasks 47.672 INFO 677-- [pool-1-thread-1] com.forezp.task.ScheduledTasks: The time is now 17:39:47

2017-04-29 17 com.forezp.task.ScheduledTasks 39 com.forezp.task.ScheduledTasks 52.675 INFO 677-- [pool-1-thread-1] com.forezp.task.ScheduledTasks: The time is now 17:39:52

Summary

It is easy to create a scheduled task in springboot, which only takes 2 steps:

1. Add @ EnableScheduling comments to the entry of the program.

two。 Add @ Scheduled annotation to the timing method.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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