Annotations SpringBoot profile automatically mapped to use the property and timing tasks

        1, regular tasks common Java class that comes java.util.Timer
            timer: Configuration is too much trouble, time delay problem
            timertask: not recommended

        2, Quartz frame
            simpler configuration
            xml or annotation

        3, SpringBoot manner using annotations opening timing Task
            1) Start the class which @EnableScheduling opening timing of the task, auto scan
            2) traffic class annotated timer task is to scan the container @Component
            3) a method of performing a timing annotate @Scheduled (fixedRate = 2000 ) performed regularly once

 

Not much to say directly on the code

@Component //让Spring 扫描到这个
@PropertySource("classpath:application.yml") // 配置文件的路径
@ConfigurationProperties(prefix = "task") // 映射配置文件里面的那个节点
@EnableScheduling // 开启定时任务
public class TaskTest {

    @Scheduled(cron = "${cron.time}") //@Scheduled 那个方法开启定时任务  cron = "${cron.time}" 任务的时间
    public  void printTime(){
        System.err.println("当前时间:"+new Date());
    }
}

This OK

@PropertySource("classpath:application.yml") 

I used this idea when there will be a problem, said classpath not found here need to add a jar in the pom file

<!--配置文件处理-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

@Scheduled(cron = "${cron.time}")

This one has more attributes cron is a form of expression, as well as with many options such as

@Scheduled(fixedRate = 2000)
@Scheduled(fixedRateString = "2000")
@Scheduled(fixedDelay = 2000)
@Scheduled(fixedDelayString = "2000")

fixedRate This is two seconds to perform a complete regardless of whether the implementation of the following

fixedDelay This is two seconds and then performed once but the following method will wait for execution is finished

cron:
  time: "*/2 * * * * *"

"* / 2 * * * * *" indicates 2 seconds to perform a

There is a webmaster tool can match the time

https://tool.lu/crontab/

 

 

 

Published 10 original articles · won praise 0 · Views 515

Guess you like

Origin blog.csdn.net/DNCCCC/article/details/105031016