thread.corePoolSize = 10
thread.maxPoolSize = 20
thread.queueCapacity = 100
thread.threadNamePrefix = 60
thread.rejectedExecutionHandler = CallerRunsPolicy
@Data
@Configuration
@PropertySource(value = "classpath:application-file.properties")
@ConfigurationProperties(prefix = "thread")
public class ThreadConfig {
@ApiModelProperty("核心池大小")
private Integer corePoolSize;
@ApiModelProperty("最大线程数")
private Integer maxPoolSize;
@ApiModelProperty("缓冲队列")
private Integer queueCapacity;
@ApiModelProperty("线程空闲时间")
private String threadNamePrefix;
@ApiModelProperty("线程策略")
private String rejectedExecutionHandler;
}
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
@Slf4j
@Component
@EnableAsync
public class ScheduleConfig {
@Resource
private ThreadConfig threadConfig;
private static final String EXECUTOR_NAME01 = "asyncExecutor01";
private static final String EXECUTOR_NAME02 = "asyncExecutor02";
@Bean(name = EXECUTOR_NAME01)
public Executor getAsyncExecutor01() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(threadConfig.getCorePoolSize());
threadPoolTaskExecutor.setMaxPoolSize(threadConfig.getMaxPoolSize());
threadPoolTaskExecutor.setQueueCapacity(threadConfig.getQueueCapacity());
threadPoolTaskExecutor.setKeepAliveSeconds(300);
threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
threadPoolTaskExecutor.setThreadNamePrefix(threadConfig.getThreadNamePrefix());
threadPoolTaskExecutor.setRejectedExecutionHandler(getRejectedExecutionHandler(threadConfig.getRejectedExecutionHandler()));
threadPoolTaskExecutor.initialize();
return threadPoolTaskExecutor;
}
@Bean(name = EXECUTOR_NAME02)
public Executor getAsyncExecutor02() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(threadConfig.getCorePoolSize());
threadPoolTaskExecutor.setMaxPoolSize(threadConfig.getMaxPoolSize());
threadPoolTaskExecutor.setQueueCapacity(threadConfig.getQueueCapacity());
threadPoolTaskExecutor.setKeepAliveSeconds(300);
threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
threadPoolTaskExecutor.setThreadNamePrefix(threadConfig.getThreadNamePrefix());
threadPoolTaskExecutor.setRejectedExecutionHandler(getRejectedExecutionHandler(threadConfig.getRejectedExecutionHandler()));
threadPoolTaskExecutor.initialize();
return threadPoolTaskExecutor;
}
public RejectedExecutionHandler getRejectedExecutionHandler(String rejectedName){
Map<String, RejectedExecutionHandler> rejectedExecutionHandlerMap = new HashMap<>(16);
rejectedExecutionHandlerMap.put("CallerRunsPolicy", new ThreadPoolExecutor.CallerRunsPolicy());
rejectedExecutionHandlerMap.put("AbortPolicy", new ThreadPoolExecutor.AbortPolicy());
rejectedExecutionHandlerMap.put("DiscardPolicy", new ThreadPoolExecutor.DiscardPolicy());
rejectedExecutionHandlerMap.put("DiscardOldestPolicy", new ThreadPoolExecutor.DiscardOldestPolicy());
return rejectedExecutionHandlerMap.getOrDefault(rejectedName, new ThreadPoolExecutor.CallerRunsPolicy());
}
}
@Slf4j
@EnableAsync
@EnableScheduling
@Component
public class DataTask {
@Async("asyncExecutor01")
@Scheduled(cron = "0/10 * * * * *")
public void a(){
....
}
}