微服务定时任务 莫名其妙全部死掉的原因 @Scheduled(cron = "0 */1 * * * ?")

springboot 我们一般都用 @Scheduled作为定时任务

@Scheduled默认为单线程模式 ,如果同时有多个定时任务同时运行,是按同步运行的,所以如果有某个定时任务进入死循环或者用时比较长 就会影响其他的定时任务执行

解决: 设置@Scheduled多线程

添加下面类

package com.midea.mcc.task;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

/**
 * @description: 设置定时任务多线程,防止某个定时任务死循环导致其他定时任务不能正常运行。
 * @author: 
 * @date: 2019-10-23 10:07
 */
@Configuration
@EnableAsync
public class ScheduleConfig {
    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(10); 设置10条  
        return taskScheduler;
    }
}

 

你可能感兴趣的:(微服务定时任务 莫名其妙全部死掉的原因 @Scheduled(cron = "0 */1 * * * ?"))