zoukankan      html  css  js  c++  java
  • SpringBoot 线程池配置 定时任务,异步任务

    package com.chitic.schedule.data.config.job;
    
    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.annotation.EnableScheduling;
    import org.springframework.scheduling.annotation.SchedulingConfigurer;
    import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
    import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
    import org.springframework.scheduling.config.ScheduledTaskRegistrar;
    
    import java.util.concurrent.Executor;
    import java.util.concurrent.ThreadPoolExecutor;
    
    /**
     * @Description TODO 线程池配置  定时任务,异步任务
     * @Author GX
     * @Date 2019/7/1 15:19
     * @Version V1.0
     **/
    @Configuration
    @EnableAsync
    @EnableScheduling
    @Slf4j
    public class ExecutorConfig implements SchedulingConfigurer, AsyncConfigurer {
    
        /**
         * 定时任务使用的线程池
         * @return
         */
        @Bean(destroyMethod = "shutdown", name = "taskScheduler")
        public ThreadPoolTaskScheduler taskScheduler(){
            ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
            scheduler.setPoolSize(10);
            scheduler.setThreadNamePrefix("task-");
            scheduler.setAwaitTerminationSeconds(600);
            scheduler.setWaitForTasksToCompleteOnShutdown(true);
            return scheduler;
        }
    
        /**
         * 异步任务执行线程池
         * @return
         */
        @Bean(name = "asyncExecutor")
        public ThreadPoolTaskExecutor asyncExecutor() {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(10);
            executor.setQueueCapacity(1000);
            executor.setKeepAliveSeconds(600);
            executor.setMaxPoolSize(20);
            executor.setThreadNamePrefix("taskExecutor-");
            executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
            executor.initialize();
            return executor;
        }
    
        @Override
        public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
            ThreadPoolTaskScheduler taskScheduler = taskScheduler();
            scheduledTaskRegistrar.setTaskScheduler(taskScheduler);
        }
    
        @Override
        public Executor getAsyncExecutor() {
            return asyncExecutor();
        }
    
        @Override
        public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
            return (throwable, method, objects) -> {
                log.error("异步任务执行出现异常, message {}, emthod {}, params {}", throwable, method, objects);
            };
        }
    
    }
  • 相关阅读:
    转载CSDN(educast):c# 对两个Datatable的结构相同进行合并
    .net升级到4.0之后,出现'System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its depen
    转载来自博客园(poleices):C#和JavaScript交互(asp.net前台和后台互调)总结
    Bat文件 执行MSSQL 脚本
    多功能jQuery对话框插件 jBox 2.3 正式版
    十一种Web网站程序性能测试工具介绍
    大型电子商务网站架构
    Nginx负载均衡
    文语通5.0破解版下载 有详细图解安装说明
    分享8个超棒的免费高质量图标搜索引擎
  • 原文地址:https://www.cnblogs.com/gaomanito/p/11120164.html
Copyright © 2011-2022 走看看