Timing tasks are generally present in medium and large enterprise-level projects. In order to reduce the pressure on servers and databases, time periods are often used to complete certain business logic. The most common thing is that the financial service system pushes callbacks. Generally, the payment system order will continue to callbacks when it does not receive a successful callback and returns content. This kind of callback is generally completed by timed tasks. There is also the generation of reports. We usually complete this operation when the number of customers visits is too small, which is often in the early morning. At this time, we can also use timed tasks to complete the logic. SpringBoot has built-in timing tasks for us, and we only need one annotation to enable timing for us to use.
In development, timing tasks are a common function. Developing timing 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, Alibaba Cloud's maven repository image is used here.
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-scheduled</name> <description>Demo project for Spring Boot</description> <!-- Alibaba Cloud maven repository--> <repositories> <repository> <id>public</id> <name>aliyun nexus</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> </repository> </repositories> <pluginRepository> <pluginRepository> <id>public</id> <name>aliyun nexus</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepository> </pluginRepository> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.5.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
2. Customize the task scenario
Timed tasks are implemented, providing scenarios such as fixed cycles, fixed cycle delay intervals and formulated time points. Use @Scheduled annotation 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()));}//The first time delays execution by 1 second, and then execute @Scheduled(initialDelay = 1000, fixedDelay = 2000) public void timerInit() {System.out.println("init : "+dateFormat.format(new Date()));}//Execute @Scheduled(cron = "50 16 20 * * ?") 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:16:54
Summarize
The above is all the content of this article about Springboot implementing scheduled task code through Scheduled, and I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
Detailed explanation of Spring boot cross-domain setting instance
Get to know Spring Boot quickly
A brief discussion on the advantages of Springboot to Spring
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!