通过自定义定时配置对定时任务抛出的异常进行统一处理,代码如下:
/**
* 定时任务配置
* @author congyiqun
* @since 2021-05-31
*/
@Slf4j
@Configuration
@EnableScheduling //开启定时任务
public class SchedulingConfimplements SchedulingConfigurer {
private final ThreadPoolTaskSchedulertaskScheduler;
public SchedulingConf() {
taskScheduler =new ThreadPoolTaskScheduler();
taskScheduler.setErrorHandler(throwable ->log.error(ExceptionUtils.getStackTrace(throwable)));
taskScheduler.setThreadNamePrefix("@scheduled-");
taskScheduler.initialize();
}
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.setScheduler(taskScheduler);
}
}
网友评论