In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you how Springboot achieves scheduled tasks through Scheduled. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Scheduled tasks generally exist in medium-and large-scale enterprise projects, and time periods are often used to complete some business logic in order to reduce the pressure on servers and databases. It is more common for financial services systems to push callbacks. Generally, payment system orders will have a continuous callback when they do not receive a successful callback. This callback is usually completed by a fixed-time task. There is the generation of reports, we usually complete this operation when the number of customer visits is too small, which is often in the early morning. At this time, we can also use scheduled tasks to complete the logic. SpringBoot has built-in scheduling tasks for us, and we only need an annotation to turn on timing for our use.
In development, scheduled tasks are a common feature. Developing scheduled tasks under spring boot is actually very simple. The specific code is as follows:
1. Configure the dependency package pom.xml
Since the default maven repository is often not accessible, the maven repository image of Aliyun is used here.
4.0.0 com.example demo 0.0.1-SNAPSHOT jar spring-boot-scheduled Demo project for Spring Boot public aliyun nexus http://maven.aliyun.com/nexus/content/groups/public/ true public aliyun nexus http://maven.aliyun.com/nexus/content/groups/public/ true false Org.springframework.boot spring-boot-starter-parent 1.4.5.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
2. Customize the task scenario
Timing task implementation, providing fixed cycle, fixed cycle delay interval and fixed time point execution and other scenarios. The @ Scheduled annotation is used for annotation.
ExampleTimer.java
Package com.example;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Componentpublic class ExampleTimer {SimpleDateFormat dateFormat = new SimpleDateFormat ("HH:mm:ss"); @ Scheduled (fixedRate = 10000) public void timerRate () {System.out.println (dateFormat.format (new Date () } / / delay execution for 1 second for the first time, then execute @ Scheduled (initialDelay = 1000, fixedDelay = 2000) public void timerInit () {System.out.println ("init:" + dateFormat.format (new Date () 2 seconds after execution;} / / execute @ Scheduled (cron = "50 16 20 * *?") at 20:16:50 every day. Public void timerCron () {System.out.println ("current time:" + dateFormat.format (new Date ();}}
3. Start the application
To start the program, you need to add the @ EnableScheduling annotation.
SpringBootScheduledApplication.java
Package com.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.scheduling.annotation.EnableScheduling;@SpringBootApplication@EnableSchedulingpublic class SpringBootScheduledApplication {public static void main (String [] args) {SpringApplication.run (SpringBootScheduledApplication.class, args);}}
4. Output result
20:16:27init: 20:16:28init: 20:16:30init: 20:16:32init: 20:16:34init: 20:16:3620:16:37init: 20:16:38init: 20:16:40init: 20:16:42init: 20:16:44init: 20:16:4620:16:47init: 20:16:48current time: 20:16:50init: 20:16:50init: 20:16:52init: 20 20:16:48current time 16 20:16:48current time 54 above is all the content of the article "how Springboot implements scheduled tasks through Scheduled" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.