123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- package cn.efunbox.manage.base.configuration;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.core.task.SimpleAsyncTaskExecutor;
- import org.springframework.scheduling.annotation.AsyncConfigurer;
- import org.springframework.scheduling.annotation.EnableAsync;
- import java.lang.reflect.Method;
- import java.util.concurrent.Executor;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- @Configuration
- @EnableAsync()
- public class AsyncConfig implements AsyncConfigurer {
- private Logger logger = LoggerFactory.getLogger(getClass());
- @Value("${schedule.threadPoolAsyncTaskExecutor.thread.size}")
- private Integer threadSize;
- /**
- * name = threadPoolAsyncTaskExecutor 时
- * 使用基于线程池的 Task 执行器
- * @return
- */
- @Bean(name = "threadPoolAsyncTaskExecutor")
- public Executor threadPoolAsyncTaskExecutor() {
- ExecutorService executor = Executors.newFixedThreadPool(threadSize);
- return executor;
- }
- @Bean(name = "scheduledExecutorService")
- public ScheduledExecutorService threadPoolAsyncScheduleExecutor() {
- ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(threadSize);
- return scheduledExecutorService;
- }
- /**
- * 只有@Async 注解时
- * 使用 原生的 SimpleAsyncTaskExecutor 执行器
- * @return
- */
- @Override
- public Executor getAsyncExecutor() {
- return new SimpleAsyncTaskExecutor();
- }
- /**
- * 实现 异常处理 handler
- * @return
- */
- @Override
- public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
- return new CustomAsyncExceptionHandler();
- }
- class CustomAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
- @Override
- public void handleUncaughtException(final Throwable throwable, final Method method, final Object... obj) {
- logger.error("Exception message - " + throwable.getMessage());
- logger.error("Method name - " + method.getName());
- for (final Object param : obj) {
- logger.error("Param - " + param);
- }
- }
- }
- }
|