参考文章

https://zhuanlan.zhihu.com/p/95813468
https://www.cnblogs.com/oliverreal/p/10652380.html
cron表达式
https://blog.csdn.net/qq_39019865/article/details/79709389

定时任务实例

  1. @EnableScheduling//开启定时任务
  2. @EnableTransactionManagement//开启事务
  3. @MapperScan("com.my.learn.mapper")
  4. @SpringBootApplication
  5. @EnableAspectJAutoProxy //开启spring对注解aop的支持
  6. public class MyBatisLearnApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(MyBatisLearnApplication.class);
  9. }
  10. }
  11. //@PropertySource("classpath:/xxxx.properties") 可以读取指定文件
  12. @Component
  13. public class FixedPrintTask {
  14. @Scheduled(cron = "${task.rule}")
  15. public void execute() {
  16. System.out.println("schedule execute " + System.currentTimeMillis());
  17. }
  18. @Scheduled(cron = "0/5 * * * * ?")
  19. public void execute2() {
  20. System.out.println("schedule execute 2 " + System.currentTimeMillis());
  21. }
  22. }

application.yml文件

  1. task:
  2. rule: 0/5 * * * * ?

cron规则

image.png

image.png

image.png