定时任务
-
入门案例
创建一个 Spring Initializr项目
/*
* 开启定时任务
*/
@EnableScheduling
@SpringBootApplication
public class CrontabApplication {
public static void main(String[] args) {
SpringApplication.run(CrontabApplication.class, args);
}
/*
*9:00-20:00每半小时执行一次
*/
@Scheduled(cron = "0/5 * * * * ?")
public void task01() {
System.out.println(Thread.currentThread().getName() + "执行定时任务01 - " + DateFormat.getDateTimeInstance().format(new Date()));
}
/*
*9:00-20:00每四小时执行一次
*/
@Scheduled(cron = "0/6 * * * * ?")
public void task02() {
System.out.println(Thread.currentThread().getName() + "执行定时任务02 - " + DateFormat.getDateTimeInstance().format(new Date()));
}
}
/*
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.6.RELEASE)
2020-04-25 12:40:41.490 INFO 1276 --- [ main] com.toroidal.crontab.CrontabApplication : Starting CrontabApplication on DESKTOP-3CU3HOD with PID 1276 (E:\work_space\java_work\crontab\target\classes started by Toroidal in E:\work_space\java_work\crontab)
2020-04-25 12:40:41.493 INFO 1276 --- [ main] com.toroidal.crontab.CrontabApplication : No active profile set, falling back to default profiles: default
2020-04-25 12:40:41.799 INFO 1276 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler'
2020-04-25 12:40:41.810 INFO 1276 --- [ main] com.toroidal.crontab.CrontabApplication : Started CrontabApplication in 0.552 seconds (JVM running for 0.832)
scheduling-1执行定时任务02 - 2020-4-25 12:40:42
scheduling-1执行定时任务01 - 2020-4-25 12:40:45
scheduling-1执行定时任务02 - 2020-4-25 12:40:48
scheduling-1执行定时任务01 - 2020-4-25 12:40:50
scheduling-1执行定时任务02 - 2020-4-25 12:40:54
scheduling-1执行定时任务01 - 2020-4-25 12:40:55
scheduling-1执行定时任务02 - 2020-4-25 12:41:00
scheduling-1执行定时任务01 - 2020-4-25 12:41:00
scheduling-1执行定时任务01 - 2020-4-25 12:41:05
scheduling-1执行定时任务02 - 2020-4-25 12:41:06
*/
-
cron表达式
设置定时任务排除某段时间
在线生成cron表达式:http://cron.qqe2.com/
-
异步多线程实现
/**
* @author Toroidal
* @date 2020/4/25 11:13
*/
/*
* 开启定时任务
* 开启异步执行定时任务
*/
@EnableScheduling
@EnableAsync
@SpringBootApplication
public class CrontabApplication {
public static void main(String[] args) {
SpringApplication.run(CrontabApplication.class, args);
}
/*
*9:00-20:00每半小时执行一次
* 开启异步执行定时任务
*/
@Async
@Scheduled(cron = "0/5 * * * * ?")
public void task01() {
System.out.println(Thread.currentThread().getName() + "执行定时任务01 - " + DateFormat.getDateTimeInstance().format(new Date()));
}
/*
*9:00-20:00每四小时执行一次
* 开启异步执行定时任务
* */
@Async
@Scheduled(cron = "0/6 * * * * ?")
public void task02() {
System.out.println(Thread.currentThread().getName() + "执行定时任务02 - " + DateFormat.getDateTimeInstance().format(new Date()));
}
}
/*
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.6.RELEASE)
2020-04-25 12:36:12.065 INFO 8600 --- [ main] com.toroidal.crontab.CrontabApplication : Starting CrontabApplication on DESKTOP-3CU3HOD with PID 8600 (E:\work_space\java_work\crontab\target\classes started by Toroidal in E:\work_space\java_work\crontab)
2020-04-25 12:36:12.068 INFO 8600 --- [ main] com.toroidal.crontab.CrontabApplication : No active profile set, falling back to default profiles: default
2020-04-25 12:36:12.521 INFO 8600 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler'
2020-04-25 12:36:12.538 INFO 8600 --- [ main] com.toroidal.crontab.CrontabApplication : Started CrontabApplication in 0.762 seconds (JVM running for 1.128)
2020-04-25 12:36:15.007 INFO 8600 --- [ scheduling-1] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
task-1执行定时任务01 - 2020-4-25 12:36:15
task-2执行定时任务02 - 2020-4-25 12:36:18
task-3执行定时任务01 - 2020-4-25 12:36:20
task-4执行定时任务02 - 2020-4-25 12:36:24
task-5执行定时任务01 - 2020-4-25 12:36:25
task-6执行定时任务01 - 2020-4-25 12:36:30
task-7执行定时任务02 - 2020-4-25 12:36:30
Process finished with exit code -1
*/