SpringBoot定时任务 schedule
所属分类 springboot
浏览量 1315
在启动类上增加注解@EnableScheduling ( springboot2 不加也可以)
使用 ScheduledThreadPoolExecutor 执行定时任务 工作线程数默认1
任务执行抛出异常,下一次会继续执行
注意与 JDK ScheduledExecutorService 的区别
JDK ScheduledExecutorService 任务抛出异常,任务不会再执行
ScheduledThreadPoolExecutor使用介绍
线程数配置
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
// 7个工作线程
taskRegistrar.setScheduler(Executors.newScheduledThreadPool(7));
}
}
任务不会重叠执行 ,只有执行完了 , 才会触发下一次执行
pool-3-thread-3 TaskBean.task2() run 2020-06-30T07:50:36.776
pool-3-thread-1 TaskBean.task1() run 2020-06-30T07:50:37.005
pool-3-thread-5 TaskBean.task2() run 2020-06-30T07:50:38.778
pool-3-thread-1 TaskBean.task1() run 2020-06-30T07:50:40.003
pool-3-thread-5 TaskBean.task2() run 2020-06-30T07:50:40.782
pool-3-thread-5 TaskBean.task2() run 2020-06-30T07:50:42.786
pool-3-thread-6 TaskBean.task1() run 2020-06-30T07:50:43.003
pool-3-thread-2 TaskBean.task2() run 2020-06-30T07:50:44.788
@Component
public class TaskBean {
// 定时任务
private static final Random rand = new Random();
@Scheduled(cron="* * * * * ?")
public void task1(){
// 模拟执行抛异常
if(rand.nextInt(10)%3 ==0){
throw new RuntimeException("TaskBean.task1() run error,"+LocalDateTime.now());
}
System.out.println(Thread.currentThread().getName() + " TaskBean.task1() run " + LocalDateTime.now());
try{
Thread.sleep(2000);
}catch(Throwable e){
System.out.println("sleep error,"+e);
}
}
@Scheduled(fixedDelayString = "2000")
public void task2() {
System.out.println(Thread.currentThread().getName() + " TaskBean.task2() run " + LocalDateTime.now());
}
}
cron 每隔10秒执行一次
@Scheduled(cron = "0/10 * * * * *")
第0秒开始执行,方法执行了15秒,下一次执行时间第20秒
执行时间超过调度频率时,在下个周期执行
固定间隔
@Scheduled(fixedDelay = 1000*10)
第0秒开始执行,方法执行了15秒,下一次执行时间 第25秒
@Scheduled(initialDelay = 0, fixedDelay = 10*1000L)
固定频率
@Scheduled(fixedRate = 1000*10)
第0秒开始执行,方法执行了15秒,下一次执行时间 第15秒
执行时间超过调度频率时,立即执行 下一次
2020-06-30 18:26:50 [pool-3-thread-6] ERROR org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler TaskUtils.java 96 handleError -Unexpected error occurred in scheduled task.
java.lang.RuntimeException: TaskBean.task1() run error,2020-06-30T18:26:50.003
at demo.bean.TaskBean.task1(TaskBean.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
完整代码
https://gitee.com/dyyx/springboothello/blob/master/src/main/java/demo/bean/TaskBean.java
ScheduledThreadPoolExecutor使用介绍
Timer和ScheduledExecutorService的区别
cron表达式实例
上一篇
下一篇
springboot @Autowired 注解处理要点
spring autowired注解不生效的一种情况
cron表达式中 * 与 ? 的区别
mybatis独立使用(不依赖spring)
mybatis中 DefaultSqlSessionFactory和SqlSessionManager的区别
mybatis 代码方式创建 SqlSessionFactory