package com.ruoyi.common.core.config;
|
|
import java.lang.reflect.Method;
|
import java.util.concurrent.Executor;
|
import java.util.concurrent.ThreadPoolExecutor;
|
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Configuration;
|
import org.springframework.scheduling.annotation.AsyncConfigurer;
|
import org.springframework.scheduling.annotation.EnableAsync;
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
@Configuration
|
@EnableAsync
|
public class BaseAsyncConfigurer implements AsyncConfigurer {
|
|
@Bean("AsyncExecutor")
|
public ThreadPoolTaskExecutor executor() {
|
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
int corePoolSize = 10;
|
executor.setCorePoolSize(corePoolSize);
|
int maxPoolSize = 50;
|
executor.setMaxPoolSize(maxPoolSize);
|
int queueCapacity = 10;
|
executor.setQueueCapacity(queueCapacity);
|
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
String threadNamePrefix = "AsyncExecutor-";
|
executor.setThreadNamePrefix(threadNamePrefix);
|
executor.setWaitForTasksToCompleteOnShutdown(true);
|
// 使用自定义的跨线程的请求级别线程工厂类19
|
int awaitTerminationSeconds = 5;
|
executor.setAwaitTerminationSeconds(awaitTerminationSeconds);
|
executor.initialize();
|
return executor;
|
}
|
|
@Override
|
public Executor getAsyncExecutor() {
|
return executor();
|
}
|
|
/*异步任务中异常处理*/
|
@Override
|
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
|
return (Throwable ex, Method method, Object... params) -> {
|
//todo 异步方法异常处理
|
System.out.println("class#method: " + method.getDeclaringClass().getName() + "#"
|
+ method.getName());
|
System.out.println("type : " + ex.getClass().getName());
|
System.out.println("exception : " + ex.getMessage());
|
};
|
}
|
|
}
|