top.doudou.common.aop.collector.LogCollectorExecutorConfiguration Maven / Gradle / Ivy
package top.doudou.common.aop.collector;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.Arrays;
import java.util.concurrent.Executor;
/**
* @Description
* @Author 傻男人 <[email protected]>
* @Date 2020-09-24 14:53
* @Version V1.0
*/
@EnableAsync
@Configuration
@ComponentScan
@Slf4j
public class LogCollectorExecutorConfiguration implements AsyncConfigurer {
/**
* 默认配置一个空的收集器
*/
@Bean
@ConditionalOnMissingBean(value = LogCollector.class)
public LogCollector nothingCollector() {
return new DefaultCollector();
}
/**
* 配置 日志收集器异步线程池
*/
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(256);
executor.setThreadNamePrefix("LogCollectorAsyncExecutor-");
executor.setRejectedExecutionHandler((r, exec) ->
log.error("LogCollectorAsyncExecutor thread queue is full,activeCount:" + exec.getActiveCount() + ",Subsequent collection tasks will be rejected,please check your LogCollector or config your Executor"));
executor.initialize();
return executor;
}
/**
* AsyncUncaughtExceptionHandler
*/
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return (ex, method, params) -> log.error("LogCollectorExecutor execution Exception [method: " + method + " ,params: " + Arrays.toString(params) + " ]", ex);
}
}