美文网首页
SpringBoot2 异步线程Async线程池配置

SpringBoot2 异步线程Async线程池配置

作者: lry102 | 来源:发表于2022-10-14 14:45 被阅读0次

    Async线程池通用配置

    import lombok.extern.slf4j.Slf4j;
    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;
    
    import java.util.concurrent.Executor;
    import java.util.concurrent.ThreadPoolExecutor;
    
    /**
     *  使用@EnableAsync来开启异步任务支持,定义线程池
     **/
    @Slf4j
    @Configuration
    @EnableAsync
    public class AsyncExecutorConfig implements AsyncConfigurer {
    
        /**
         * 核心线程数(默认线程数)
         */
        private static final int CORE_POOL_SIZE = 5;
        /**
         * 最大线程数
         */
        private static final int MAX_POOL_SIZE = 10;
        /**
         * 允许线程空闲时间(单位:默认为秒)
         */
        private static final int KEEP_ALIVE_TIME = 10;
        /**
         * 缓冲队列大小
         */
        private static final int QUEUE_CAPACITY = 200;
        /**
         * 线程池名前缀
         */
        private static final String THREAD_NAME_PREFIX = "Async-Service-";
    
    
        /**
         * 自定义线程池
         *
         * @return
         */
        @Bean
        public ThreadPoolTaskExecutor asyncTaskExecutor() {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(CORE_POOL_SIZE);
            executor.setMaxPoolSize(MAX_POOL_SIZE);
            executor.setQueueCapacity(KEEP_ALIVE_TIME);
            executor.setKeepAliveSeconds(QUEUE_CAPACITY);
            executor.setThreadNamePrefix(THREAD_NAME_PREFIX);
            /**
             * 当线程池的任务缓存队列已满并且线程池中的线程数目达到maximumPoolSize,如果还有任务到来就会采取任务拒绝策略
             * 通常有以下四种策略:
             * ThreadPoolExecutor.AbortPolicy:丢弃任务并抛出RejectedExecutionException异常。
             * ThreadPoolExecutor.DiscardPolicy:丢弃任务,但是不抛出异常。
             * ThreadPoolExecutor.DiscardOldestPolicy:丢弃队列最前面的任务,然后重新尝试执行任务(重复此过程)
             * ThreadPoolExecutor.CallerRunsPolicy:重试添加当前的任务,自动重复调用 execute() 方法,直到成功
             */
            executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
            executor.initialize();
            return executor;
        }
    
        /**
         * 指定默认线程池
         */
        @Override
        public Executor getAsyncExecutor() {
            return taskExecutor();
        }
    
        @Override
        public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
            return (ex, method, params) -> log.error("线程池执行任务发横未知错误,执行方法:{}", method.getName(), ex);
        }
    

    相关文章

      网友评论

          本文标题:SpringBoot2 异步线程Async线程池配置

          本文链接:https://www.haomeiwen.com/subject/sacczrtx.html