1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| @Slf4j @EnableAsync @Configuration public class AsyncExecutorConfig implements AsyncConfigurer {
@Bean public ThreadPoolTaskExecutor asyncServiceExecutor() { int cpu = Runtime.getRuntime().availableProcessors(); log.info("start asyncServiceExecutor cpu : {}", cpu); ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(cpu); executor.setMaxPoolSize(cpu); executor.setQueueCapacity(50); executor.setWaitForTasksToCompleteOnShutdown(true); executor.setAwaitTerminationSeconds(60); executor.setThreadNamePrefix("async-service-"); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); executor.initialize(); return executor; }
@Override public Executor getAsyncExecutor() { return asyncServiceExecutor(); }
@Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return (throwable, method, objects) -> { StringBuilder sb = new StringBuilder(); for (Object param : objects) { sb.append(param).append(","); } log.error("Exception message - {},Method name - {},Parameter value - {}", throwable.getMessage(), method.getName(), sb.toString()); };
} }
|