package com.liftsail.rsademo.utils;标签:异步,配置,springframework,线程,import,org,threadPoolTaskExecutor,threadPoolTaskSchedul From: https://www.cnblogs.com/liftsail/p/16919761.html
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.config.Task;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @Author 张帆
* @Description 不积跬步无以至千里
* @Date 2022/11/23 20:50
*/
@Configuration
@Slf4j
public class ThreadPoolConfig implements AsyncConfigurer, SchedulingConfigurer {
/**
* 异步任务执行线程池参数
*/
private static final Integer CORE_POOL_SIZE = 5;
private static final Integer MAX_POOL_SIZE = 200;
private static final Integer QUEUE_CAPACITY = 2000;
private static final String THREAD_NAME_PREFIX = "async-thread-";
private static final Integer KEEP_ALIVE_SECONDS = 60;
/**
* 定时任务线程池线程名前缀
*/
private static final String SCHEDULER_THEREAD_NAME_PREFIX = "task-";
/**
* @param
* @description: 创建执行spring task定时任务的线程池,调用@scheduled注解的定时任务
* @author: xiaomifeng1010
* @date: 2022/3/13
* @return: TaskScheduler
**/
@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(10);
threadPoolTaskScheduler.setThreadNamePrefix(SCHEDULER_THEREAD_NAME_PREFIX);
threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true);
return threadPoolTaskScheduler;
}
/**
* @param
* @description: 创建执行异步任务的线程池,用于调用 @async注解的方法
* @author: xiaomifeng1010
* @date: 2022/3/13
* @return: ThreadPoolTaskExecutor
**/
@Bean("asyncThreadPoolTaskExecutor")
public ThreadPoolTaskExecutor asyncThreadPoolTaskExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
// 核心线程数量
threadPoolTaskExecutor.setCorePoolSize(CORE_POOL_SIZE);
// 最大线程数量
threadPoolTaskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
// 队列中最大任务数
threadPoolTaskExecutor.setQueueCapacity(QUEUE_CAPACITY);
// 线程名称前缀
threadPoolTaskExecutor.setThreadNamePrefix(THREAD_NAME_PREFIX);
// 当达到最大线程数时如何处理新任务(拒绝策略)
threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 线程空闲后最大存活时间
threadPoolTaskExecutor.setKeepAliveSeconds(KEEP_ALIVE_SECONDS);
// 初始化线程池
threadPoolTaskExecutor.initialize();
// 关闭线程池
threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
return threadPoolTaskExecutor;
}
/**
* Callback allowing a {@link TaskScheduler
* TaskScheduler} and specific {@link Task Task}
* instances to be registered against the given the {@link ScheduledTaskRegistrar}.
*
* @param taskRegistrar the registrar to be configured.
*/
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setTaskScheduler(threadPoolTaskScheduler());
}
/**
* The {@link Executor} instance to be used when processing async
* method invocations.
*/
@Override
public Executor getAsyncExecutor() {
return asyncThreadPoolTaskExecutor();
}
/**
* The {@link AsyncUncaughtExceptionHandler} instance to be used
* when an exception is thrown during an asynchronous method execution
* with {@code void} return type.
*/
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return (throwable, method, objects) -> {
log.error("异步任务执行出现异常, message {}, method {}, params {}", throwable, method, objects);
};
}
}