前言
SpringBoot项目实现由cron表达式定义的计划任务
单线程计划任务
启用计划任务
1 2 3 4 5 6 7
| @EnableScheduling @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
|
定义计划任务
1 2 3 4 5 6 7
| @Component public class Cls { @Scheduled(cron = "* * * * * ?") public void method() { ... } }
|
指定时区
1 2 3 4 5 6 7
| @Component public class Cls { @Scheduled(cron = "* * * * * ?", zone = "Asia/Shanghai") public void method() { ... } }
|
多线程计划任务
启用计划任务
1 2 3 4 5 6 7 8
| @EnableAsync @EnableScheduling @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
|
定义计划任务
1 2 3 4 5 6 7 8
| @Component public class Cls { @Async @Scheduled(cron = "* * * * * ?") public void method() { ... } }
|
完成