| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- package com.cec.plugins.config;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.scheduling.annotation.AsyncConfigurer;
- import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
- import java.lang.reflect.Method;
- import java.util.concurrent.Executor;
- import java.util.concurrent.ThreadPoolExecutor;
- /**
- * @author zhanghaijun
- * @date 2023/8/20 10:25
- * @description 异步执行配置信息
- */
- @Slf4j
- @Configuration
- public class AsyncConfig implements AsyncConfigurer {
- @Override
- public Executor getAsyncExecutor() {
- ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
- executor.setCorePoolSize(8); // 核心线程数
- executor.setMaxPoolSize(32); // 最大线程数
- executor.setQueueCapacity(30); // 队列大小
- executor.setKeepAliveSeconds(60); // 线程最大空闲时间
- // 指定用于新创建的线程名称的前缀。
- executor.setThreadNamePrefix("async-cec-");
- executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
- // 这一步千万不能忘了,否则报错: java.lang.IllegalStateException: ThreadPoolTaskExecutor not initialized
- executor.initialize();
- return executor;
- }
- /** 异步执行异常全局处理 */
- @Override
- public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
- return (Throwable ex, Method method, Object... params) -> {
- log.error("glob async exception: {},method: {}, params: {}", ex.getMessage(), method.getName(), params);
- };
- }
- }
|