• SpringBoot开启定时任务


    第一步是在应用启动类上添加@EnableScheduling 注解来允许当前应用开启定时任务。

    1. import org.springframework.boot.SpringApplication;
    2. import org.springframework.boot.autoconfigure.SpringBootApplication;
    3. import org.springframework.scheduling.annotation.EnableScheduling;
    4. /**
    5. * @author mhlevel
    6. * @date 2020-08-20 11:44
    7. */
    8. @SpringBootApplication
    9. @EnableScheduling
    10. public class springbootApplication {
    11. public static void main(String[] args) {
    12. SpringApplication.run(springbootApplication.class, args);
    13. }
    14. }

    第二步是创建一个定时任务的类,它里面可能会包含很多个定时任务(一个任务就是对应到类中的一个方法),注意,这个定时任务类需要用@Component 注解标注,以便Spring 容器能扫描到这个类

    1. import org.springframework.scheduling.annotation.Scheduled;
    2. import org.springframework.stereotype.Component;
    3. @Component
    4. public class OrderJob {
    5. @Scheduled(cron = "0/3 * * * * ? ")
    6. public void autoCloseOrder(){
    7. System.out.println("执行定时任务,当前时间为:"+ new Date());
    8. }
    9. }

    其中cron的值,可以在在线Cron表达式生成器此网页中设置自己想要间隔的时间,自动生成对应的cron表达式。比如我这边设置的是每隔3秒,执行一次。

    测试

    1. import lombok.extern.slf4j.Slf4j;
    2. import org.springframework.scheduling.annotation.Scheduled;
    3. import org.springframework.stereotype.Component;
    4. import java.time.LocalDateTime;
    5. import java.time.format.DateTimeFormatter;
    6. /**
    7. * @author mhlevel
    8. * @date 2020-08-20 21:19
    9. */
    10. @Slf4j
    11. @Component
    12. public class BootSchedule {
    13. private final DateTimeFormatter fmt = DateTimeFormatter.ofPattern(
    14. "HH:mm:ss"
    15. );
    16. // 上次任务开始后隔三秒开启下一次任务
    17. @Scheduled(fixedRate = 3000)
    18. public void schedule01(){
    19. log.info("schedule01 -> {}", LocalDateTime.now().format(fmt));
    20. }
    21. // 上次任务执行完毕的时间点之后3s再执行
    22. @Scheduled(fixedDelay = 3000)
    23. public void schedule02(){
    24. log.info("schedule02 -> {}", LocalDateTime.now().format(fmt));
    25. }
    26. // 第一次延后2s 执行,之后每3s 执行一次
    27. @Scheduled(initialDelay = 1000, fixedRate = 3000)
    28. public void schedule03(){
    29. log.info("schedule03 -> {}", LocalDateTime.now().format(fmt));
    30. }
    31. // 每3s 执行一次
    32. @Scheduled(cron = "0/3 * * * * ?")
    33. public void schedule04(){
    34. log.info("shedule04 -> {}", LocalDateTime.now().format(fmt));
    35. }
    36. }

    corn表达式解释

    只在项目第一次启动的时候启动

    使用配置类

    • ApplicationContext事件机制是观察者设计模式的实现,通过ApplicationEvent类和ApplicationListener接口,可以实现ApplicationContext事件处理,我们这里就只用ApplicationListener接口就行。
    • 如果容器中存在ApplicationListener的Bean,当ApplicationContext调用publishEvent方法时,对应的Bean会被触发。

       用到了Spring的内置事件ContextRefreshedEvent,当ApplicationContext被初始化或刷新时,会触发ContextRefreshedEvent事件

    1. @Service
    2. //项目启动自动执行方法的配置类
    3. public class SearchReceiveConfig implements ApplicationListener<ContextRefreshedEvent> {
    4. //注入
    5. @Autowired
    6. private TestService testService;
    7. @Override
    8. public void onApplicationEvent(ContextRefreshedEvent event) {
    9. //保证只执行一次
    10. if (event.getApplicationContext().getParent()==null){
    11. //需要执行的方法
    12. this.testService.getTest();
    13. }
    14. }
    15. }
    Service层
    
    1. @Service
    2. public class TestService {
    3. public void getTest(){
    4. System.out.println("测试");
    5. }
    6. }

  • 相关阅读:
    如何构建城市经济大脑分析指标框架?六大分析主题
    ESP32-C3入门教程 基础篇⑪——Non-Volatile Storage (NVS) 非易失性存储参数的读写
    上位机与MES对接的常见方式
    面试系列Java高级:如何解决接口幂等性
    leetcode 300. 最长递增子序列、674. 最长连续递增序列、718. 最长重复子数组
    杰理AC632蓝牙芯片ADC
    19. 删除链表的倒数第 N 个结点
    C++ 一个时间转换类封装
    美摄AIGC创新引擎,助力企业快速搭建AIGC能力(一)
    mysql安装完全排坑指南
  • 原文地址:https://blog.csdn.net/weixin_53998054/article/details/125994506