Springboot implements timed task code through Scheduled

  • 2020-11-25 07:16:04
  • OfStack

Timing task 1 generally exists in large and medium-sized enterprise projects. In order to reduce the pressure on servers and databases, some business logic is usually completed in time. A common one is the financial service system push callback. The order of general payment system will be continuously called back when no successful callback is received. Such callback 1 is usually completed by a timed task. There is also the report generation, we usually in the customer visits are too small to complete this operation, that is often in the early morning. We can also use timed tasks to complete the logic. SpringBoot has built-in timer tasks for us, so we only need one annotation to turn on the timer for our use.

In development, timing task is a common function. Developing timing task under spring boot is actually very simple. The specific code is as follows:

1. Configure the dependency package pom.xml

Since the default maven warehouse is often inaccessible, ali Cloud's maven warehouse 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>

  <!--  Ali cloud maven warehouse  -->
  <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>
  <pluginRepositories>
    <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>
  </pluginRepositories>

  <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

Timing task implementation, provide a fixed period, fixed period delay interval and set time point execution and other scenarios. Use the @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;
@Component
public class ExampleTimer {
	SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
	@Scheduled(fixedRate = 10000)
	    public void timerRate() {
		System.out.println(dateFormat.format(new Date()));
	}
	// The first 1 Time delay 1 Seconds to execute when finished 2 Seconds to perform 
	@Scheduled(initialDelay = 1000, fixedDelay = 2000)
	    public void timerInit() {
		System.out.println("init : "+dateFormat.format(new Date()));
	}
	// Every day, 20 point 16 points 50 Seconds to perform 
	@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, 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
@EnableScheduling
public class SpringBootScheduledApplication {
	public static void main(String[] args) {
		SpringApplication.run(SpringBootScheduledApplication.class, args);
	}
}

4. Output results


20:16:27
init : 20:16:28
init : 20:16:30
init : 20:16:32
init : 20:16:34
init : 20:16:36
20:16:37
init : 20:16:38
init : 20:16:40
init : 20:16:42
init : 20:16:44
init : 20:16:46
20:16:47
init : 20:16:48
current time : 20:16:50
init : 20:16:50
init : 20:16:52
init : 20:16:54

conclusion

That's the end of this article on Springboot using Scheduled to implement the timing task code, I hope to help you. Those who are interested can continue to see this site:

Detail example of Spring boot cross-domain setup

Learn Spring Boot quickly

On the advantages of Springboot over Spring

If there is any deficiency, please let me know. Thank you for your support!


Related articles: