Explain how to use @ scheduled to create scheduled tasks in spring boot
We often encounter such scenarios when writing spring boot applications. For example, I need to send some SMS, e-mail and other operations regularly, or I may check and monitor some flags and parameters regularly.
Create scheduled task
Writing timing tasks in spring boot is very simple. The following is an example of how to create timing tasks in spring boot to output the current time every 5 seconds.
Add the @ enableshcheduling annotation to the main class of spring boot to enable the configuration of scheduled tasks
Create scheduled task implementation class
Run the program, you can see the following output in the console, and the scheduled task starts to operate normally.
2016-05-15 10:40:04.073 INFO 1688 --- [ main] : Started Application in 1.433 seconds (JVM running for 1.967)
Present time: 10:40:09
Present time: 10:40:14
Present time: 10:40:19
Present time: 10:40:24
Present time: 10:40:29522
Present time: 10:40:34
You can also refer to the official scheduling tasks for the above simple introductory examples
@Scheduled details
In the above introductory example, the @ scheduled (fixedrate = 5000) annotation is used to define the tasks to be executed every 5 seconds. The use of @ scheduled can be summarized as follows:
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.