Springboot配置Schedule定时任务线程池
Springboot默认的定时任务是单线程的,当我们的定时任务较多并且耗时较长时,为了提升效率我们可以给定时任务配置一个线程池,以多线程的方式执行定时任务。
代码清单:
ScheduleConfig.java
//启用自动化配置
@Configuration
//启用定时任务
@EnableScheduling
public class ScheduleConfig implements SchedulingConfigurer {
@Value("${thread.pool.corePoolSize:10}")
private int corePoolSize;
@Value("${thread.pool.maxPoolSize:500}")
private int maxPoolSize;
@Value("${thread.pool.keepAliveSeconds:300}")
private int keepAliveSeconds;
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.setScheduler(getExecutor(corePoolSize,maxPoolSize,keepAliveSeconds));
}
/**
* 获取线程池
*
* @param corePoolSize 最小线程数
* @param maxPoolSize 最大线程数
* @param keepAliveSeconds 允许空闲时间(秒)
* @return 返回队列
*/
protected ScheduledExecutorService getExecutor(int corePoolSize, int maxPoolSize, long keepAliveSeconds) {
//线程名称
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("schedule-pool-%d").build();
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(corePoolSize, namedThreadFactory, new ThreadPoolExecutor.CallerRunsPolicy());
//最小线程数
executor.setCorePoolSize(corePoolSize);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//最大线程数
executor.setMaximumPoolSize(maxPoolSize);
//允许空闲时间(秒)
executor.setKeepAliveTime(keepAliveSeconds, TimeUnit.SECONDS);
return executor;
}
}
Springboot配置Async异步线程池
在进行一些耗时又不需要同步执行的动作时我们可以使用Springboot给我提供的 @Async注解,实现这个异步的执行,注意该注解修饰的方法必须是public的,在同一个类中调用该方法是无效的。
代码清单:
AsyncConfig.java
@Configuration
@EnableAsync
@Slf4j
public class AsyncConfig implements AsyncConfigurer {
@Value("${thread.pool.corePoolSize:10}")
private int corePoolSize;
@Value("${thread.pool.maxPoolSize:500}")
private int maxPoolSize;
@Value("${thread.pool.keepAliveSeconds:300}")
private int keepAliveSeconds;
@Value("${thread.pool.queueCapacity:2000}")
private int queueCapacity;
@Override
public Executor getAsyncExecutor() {
return this.getExecutor(corePoolSize, maxPoolSize, keepAliveSeconds, queueCapacity);
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return (throwable, method, objects) -> log.error("Async ERROR: throwable={},method={},params={}", throwable, method, objects);
}
/**
* 获取线程池
*
* @param corePoolSize 最小线程数
* @param maxPoolSize 最大线程数
* @param keepAliveSeconds 允许空闲时间(秒)
* @param queueCapacity 缓冲队列数
* @return 返回队列
*/
protected ThreadPoolTaskExecutor getExecutor(int corePoolSize, int maxPoolSize, int keepAliveSeconds, int queueCapacity){
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//最小线程数
executor.setCorePoolSize(corePoolSize);
//缓冲队列数,corePoolSize满时启用缓冲
executor.setQueueCapacity(queueCapacity);
//最大线程数
executor.setMaxPoolSize(maxPoolSize);
//允许空闲时间(秒)
executor.setKeepAliveSeconds(keepAliveSeconds);
//指定用于新创建的线程名称的前缀
executor.setThreadNamePrefix("AsyncExecutor-");
//指定拒绝策略
executor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor exe) {
log.error("异步任务线程池队列已满.");
}
});
executor.initialize();
return executor;
}
}
测试效果:
GitHub地址:https://github/qinming99/tugos-demo
更多推荐
Springboot配置Schedule定时任务线程池和Async异步线程池
发布评论