|
| 1 | +package top.cadecode.uniboot.framework.config; |
| 2 | + |
| 3 | +import com.dtp.core.thread.DtpExecutor; |
| 4 | +import lombok.RequiredArgsConstructor; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | +import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; |
| 7 | +import org.springframework.context.annotation.Bean; |
| 8 | +import org.springframework.context.annotation.Configuration; |
| 9 | +import org.springframework.scheduling.annotation.AsyncConfigurer; |
| 10 | +import org.springframework.scheduling.annotation.SchedulingConfigurer; |
| 11 | +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; |
| 12 | +import org.springframework.scheduling.config.ScheduledTaskRegistrar; |
| 13 | + |
| 14 | +import java.util.concurrent.Executor; |
| 15 | + |
| 16 | +/** |
| 17 | + * 线程池设置 |
| 18 | + * |
| 19 | + * @author Cade Li |
| 20 | + * @date 2023/3/15 |
| 21 | + */ |
| 22 | +@Slf4j |
| 23 | +@Configuration |
| 24 | +public class ThreadPoolConfig { |
| 25 | + |
| 26 | + /** |
| 27 | + * Spring 定时任务线程池 |
| 28 | + */ |
| 29 | + @Bean(name = "taskScheduler", destroyMethod = "shutdown") |
| 30 | + public ThreadPoolTaskScheduler taskScheduler() { |
| 31 | + ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); |
| 32 | + scheduler.setPoolSize(6); |
| 33 | + scheduler.setThreadNamePrefix("taskScheduler"); |
| 34 | + scheduler.setWaitForTasksToCompleteOnShutdown(true); |
| 35 | + scheduler.setAwaitTerminationSeconds(5); |
| 36 | + return scheduler; |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + @Slf4j |
| 41 | + @RequiredArgsConstructor |
| 42 | + @Configuration |
| 43 | + public static class ExecutorConfig implements AsyncConfigurer, SchedulingConfigurer { |
| 44 | + |
| 45 | + private final DtpExecutor asyncExecutor; |
| 46 | + private final ThreadPoolTaskScheduler taskScheduler; |
| 47 | + |
| 48 | + @Override |
| 49 | + public Executor getAsyncExecutor() { |
| 50 | + return asyncExecutor; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { |
| 55 | + return (throwable, method, objects) -> { |
| 56 | + log.error("异步任务执行出现异常, message {}, method {}, params {}", |
| 57 | + throwable, method, objects); |
| 58 | + }; |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { |
| 64 | + taskRegistrar.setTaskScheduler(taskScheduler); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments