Home  >  Article  >  Java  >  How Springboot implements scheduled tasks through Scheduled

How Springboot implements scheduled tasks through Scheduled

WBOY
WBOYforward
2023-05-16 18:10:141257browse

Scheduled tasks generally exist 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 more common ones are push callbacks from financial service systems. Generally, payment system orders will continuously call back when they do not receive successful callback return content. Such callbacks are usually completed by scheduled tasks. There is also 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 scheduled tasks for us. We only need an annotation to enable timing for our use.

In development, scheduled tasks are a common function. It is actually very simple to develop scheduled tasks under spring boot. The specific code is as follows:

1. Configure dependency package pom.xml

Since the default maven warehouse is often inaccessible, Alibaba Cloud's maven warehouse image 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. Customized task scenarios

Scheduled task implementation provides scenarios such as fixed period, fixed period delay interval and set time point execution. 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()));
	}
	//第一次延迟1秒执行,当执行完后2秒再执行
	@Scheduled(initialDelay = 1000, fixedDelay = 2000)
	    public void timerInit() {
		System.out.println("init : "+dateFormat.format(new Date()));
	}
	//每天20点16分50秒时执行
	@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
@EnableScheduling
public class SpringBootScheduledApplication {
	public static void main(String[] args) {
		SpringApplication.run(SpringBootScheduledApplication.class, args);
	}
}

4. Output result

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

The above is the detailed content of How Springboot implements scheduled tasks through Scheduled. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete