package com.fdkankan.scene.config; import org.springframework.boot.autoconfigure.batch.BatchProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.util.CollectionUtils; import java.lang.reflect.Method; import java.util.Arrays; import java.util.concurrent.Executors; @Configuration public class ScheduledConfig implements SchedulingConfigurer { /** * 此配置实现: * 1、同一个定时任务等待上一次执行完毕后再执行 * 2、不同定时任务同时执行 * @param taskRegistrar */ @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { Method[] methods = BatchProperties.Job.class.getMethods(); int defaultPoolSize = 3; int corePoolSize = 0; if (!CollectionUtils.isEmpty(Arrays.asList(methods))) { for (Method method : methods) { Scheduled annotation = method.getAnnotation(Scheduled.class); if (annotation != null) { corePoolSize++; } } if (defaultPoolSize > corePoolSize) { corePoolSize = defaultPoolSize; } taskRegistrar.setScheduler(Executors.newScheduledThreadPool(corePoolSize)); } } }