Spring Boot 2.x actual timing of the task scheduling

In the back-end development, some scenes that require the use of timed tasks, such as: timing synchronization batch of data, regularly clean up some of the data provided in the Spring Boot @Scheduledcomment on the timing of the scheduling function provides for simple, stand-alone scheduling the program is enough. This article ready to look at actual cases @Scheduledof usage.

Development combat

  1. New Spring Boot engineering, the main pom document reads as follows:

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>online.javaadu.schedule</groupId> <artifactId>scheduledemo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>scheduledemo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 
  2. New regular task components using @Scheduledannotations modification methods to be scheduled, will print the current time in the process.

    package online.javaadu.schedule.scheduledemo;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; @Component public class ScheduledTasks { private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class); private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); //第一次执行之前延后10秒钟;后续每隔5秒执行1次 @Scheduled(fixedRate = 5000, initialDelay = 10000) public void reportCurrentTime() { log.info("The time is now {}", dateFormat.format(new Date())); } } 
  3. The opening timing in scheduling capabilities in ScheduledemoApplication - that is open @Scheduledannotation timing scheduling functions, and print a line just up the log when the system is used to reflect the role of the previous step initialDelay.

    package online.javaadu.schedule.scheduledemo;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; import java.text.SimpleDateFormat; import java.util.Date; @SpringBootApplication @EnableScheduling public class ScheduledemoApplication { private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class); private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); public static void main(String[] args) { SpringApplication.run(ScheduledemoApplication.class, args); log.info("---The time is now {}", dateFormat.format(new Date())); } } 
  4. Click run, the results are as follows the demo can be seen that 23: 15: 35 Application started, 10 seconds after the timing schedule tasks begin execution, then once every 5 seconds printing time.

    operation result

Analysis and interpretation

We look at with @Scheduledannotated source code to see if the case provided in addition to the example above, which notes what other functions?

  • cron, you can support the complexity of the more sophisticated time
  • zone, parse cron expression when parsing zone
  • FIXEDDELAY (and fixedDelayString), between the two scheduling need to add a fixed delay
  • fixedRate (and fixedRateString), how often did not need to schedule time
  • initialDelay (and initialDelayString), need long delay before the first scheduled
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class) public @interface Scheduled { /** * 特殊的cron表达式,如果设置成这个值,则表示将定时调度器关闭,不再调度。 */ String CRON_DISABLED = ScheduledTaskRegistrar.CRON_DISABLED; /** * cron表达式,可以支持复杂的定时调度需求 */ String cron() default ""; /** * cron表达式解析的时候,解析依赖的时区 */ String zone() default ""; /** * 两次调度触发之间暂停的毫秒数,Long类型 */ long fixedDelay() default -1; /** * 两次调度触发之间暂停的毫秒数,String类型 */ String fixedDelayString() default ""; /** * 每隔几毫秒调度一次 */ long fixedRate() default -1; /** * 每隔几毫秒调度一次,String类型 */ String fixedRateString() default ""; /** * 第一次执行之前,延迟多少毫秒 */ long initialDelay() default -1; /** * 第一次执行之前,延迟多少毫秒,String类型 */ String initialDelayString() default ""; }
来源:吉林网站优化

Guess you like

Origin www.cnblogs.com/1994jinnan/p/12080458.html