From be7efd485a9b45d5d852b01df310fb6ec06ef14f Mon Sep 17 00:00:00 2001 From: whywhathow Date: Sun, 27 Feb 2022 13:56:42 +0800 Subject: [PATCH] =?UTF-8?q?feat(Spring=E7=BA=BF=E7=A8=8B=E6=B1=A0=E4=BC=98?= =?UTF-8?q?=E5=8C=96->yaml=E6=96=87=E6=A1=A3=E9=85=8D=E7=BD=AE):?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 默认配置属性类: AsyncExecutionProperties yaml配置文档: austin-web/src/main/resources/application.yml --- austin-cron/pom.xml | 6 + .../cron/config/AsyncConfiguration.java | 35 +++--- .../cron/config/AsyncExecutionProperties.java | 116 ++++++++++++++++++ austin-web/src/main/resources/application.yml | 13 ++ 4 files changed, 154 insertions(+), 16 deletions(-) create mode 100644 austin-cron/src/main/java/com/java3y/austin/cron/config/AsyncExecutionProperties.java create mode 100644 austin-web/src/main/resources/application.yml diff --git a/austin-cron/pom.xml b/austin-cron/pom.xml index a784774..21abab6 100644 --- a/austin-cron/pom.xml +++ b/austin-cron/pom.xml @@ -35,6 +35,12 @@ com.xuxueli xxl-job-core + + + org.springframework.boot + spring-boot-configuration-processor + 2.5.6 + \ No newline at end of file diff --git a/austin-cron/src/main/java/com/java3y/austin/cron/config/AsyncConfiguration.java b/austin-cron/src/main/java/com/java3y/austin/cron/config/AsyncConfiguration.java index e415dd3..0333cf2 100644 --- a/austin-cron/src/main/java/com/java3y/austin/cron/config/AsyncConfiguration.java +++ b/austin-cron/src/main/java/com/java3y/austin/cron/config/AsyncConfiguration.java @@ -3,43 +3,46 @@ package com.java3y.austin.cron.config; import com.google.common.base.Throwables; import lombok.extern.slf4j.Slf4j; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; +import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; 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; - /** * 处理定时任务的线程池配置信息,为@Async注解服务 + * 自定义线程池配置 * * @author 3y + * @see TaskExecutionAutoConfiguration */ @Slf4j @Configuration @EnableAsync +@EnableConfigurationProperties(AsyncExecutionProperties.class) public class AsyncConfiguration implements AsyncConfigurer { - @Bean("austinExecutor") - public ThreadPoolTaskExecutor executor() { + @Primary + public ThreadPoolTaskExecutor executor(AsyncExecutionProperties properties) { + log.info("funExecutor -- init "); ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); - executor.setCorePoolSize(10); - executor.setMaxPoolSize(10); - executor.setQueueCapacity(30); - executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); - executor.setThreadNamePrefix("austinAsyncExecutor-"); - executor.setWaitForTasksToCompleteOnShutdown(true); - executor.setAwaitTerminationSeconds(10); + executor.setCorePoolSize(properties.getCoreSize()); // 核心线程数 + executor.setMaxPoolSize(properties.getMaxSize()); // 最大线程数 + executor.setKeepAliveSeconds(properties.getKeepAlive()); // 最大存活时间 + executor.setQueueCapacity(properties.getQueueCapacity()); // 阻塞队列容量 + executor.setThreadNamePrefix(properties.getThreadNamePrefix()); // 设置名称前缀 + executor.setRejectedExecutionHandler(properties.getRejectedHandler().getHandler());// 设置拒绝策略 + executor.setAllowCoreThreadTimeOut(properties.isAllowCoreThreadTimeout());// 是否允许核心线程超时 + executor.setWaitForTasksToCompleteOnShutdown(properties.isWaitForTasksToCompleteOnShutDown()); + executor.setAwaitTerminationSeconds(properties.getAwaitTerminationSeconds()); + log.info("austinExecutor: {} ", executor); executor.initialize(); return executor; } - @Override - public Executor getAsyncExecutor() { - return executor(); - } @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { diff --git a/austin-cron/src/main/java/com/java3y/austin/cron/config/AsyncExecutionProperties.java b/austin-cron/src/main/java/com/java3y/austin/cron/config/AsyncExecutionProperties.java new file mode 100644 index 0000000..b5fa9e7 --- /dev/null +++ b/austin-cron/src/main/java/com/java3y/austin/cron/config/AsyncExecutionProperties.java @@ -0,0 +1,116 @@ +package com.java3y.austin.cron.config; + +import lombok.Data; +import org.springframework.boot.autoconfigure.task.TaskExecutionProperties; +import org.springframework.boot.context.properties.ConfigurationProperties; + +import javax.annotation.PostConstruct; +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.ThreadPoolExecutor; + +/** + * @program: austin + * @description: spring 自定义线程池配置类 + * @author: WhyWhatHow + * @create: 2022-02-27 09:41 + * @see TaskExecutionProperties + **/ +@Data +@ConfigurationProperties("austin.async.task") +public class AsyncExecutionProperties { + /** + * 核心线程数,默认数量当前cpu核心线程数 + */ + int coreSize; + /** + * 最大线程数 ,默认coreSize*2 + */ + int maxSize; + /** + * 线程名前缀 eg: "austinAsyncExecutor-" + */ + private String threadNamePrefix = "austinAsyncExecutor-"; + + /** + * queue capacity + */ + private int queueCapacity = 1000; + + /** + * 线程最大存活时间,单位s + */ + private int keepAlive = 60; + + /** + * 是否允许核心线程超时 + */ + private boolean allowCoreThreadTimeout = false; + + /** + * 拒绝策略 ,默认callRun + */ + private RejectedEnum rejectedHandler = RejectedEnum.CALLRUNSPOLICY; + + + /** + * 是否在关机时等待任务完成 ,默认为true + */ + private boolean waitForTasksToCompleteOnShutDown = true; + + /** + * 阻止关机的最大秒数 ,默认10s + */ + private int awaitTerminationSeconds = 10; + + /** + * 初始化 核心线程数, 最大线程数, 以用户配置为主 + */ + @PostConstruct + void init() { + if (coreSize <= 0) { + this.coreSize = Runtime.getRuntime().availableProcessors(); + } + if (maxSize <= 0) { + this.maxSize = coreSize << 1; + } + } + + /** + * 拒绝策略枚举 + */ + public enum RejectedEnum { + /** + * 直接抛出异常 + */ + ABORTPOLICY(new ThreadPoolExecutor.AbortPolicy()), + /** + * 交个当前run_thread 运行 + */ + CALLRUNSPOLICY(new ThreadPoolExecutor.CallerRunsPolicy()), + /*** + * 直接丢掉 + */ + DISCARDPOLICY(new ThreadPoolExecutor.DiscardPolicy()), + /** + * 丢掉队列中排队时间最久的任务 + */ + DISCARDOLDESTPOLICY(new ThreadPoolExecutor.DiscardOldestPolicy()); + /** + * 线程池默认拒绝策略 + */ + private RejectedExecutionHandler handler; + + RejectedEnum(RejectedExecutionHandler handler) { + this.handler = handler; + } + + + public RejectedExecutionHandler getHandler() { + return handler; + } + + public void setHandler(RejectedExecutionHandler handler) { + this.handler = handler; + } + } +} diff --git a/austin-web/src/main/resources/application.yml b/austin-web/src/main/resources/application.yml new file mode 100644 index 0000000..a3a671a --- /dev/null +++ b/austin-web/src/main/resources/application.yml @@ -0,0 +1,13 @@ +austin: + async: + task: + thread-name-prefix: "fun-task-" # task前缀名 + max-size: 8 #最大线程数 + core-size: 4 #核心线程数 + queue-capacity: 100 + keep-alive: 60 + rejected-handler: callrunspolicy #拒绝策略,不能自定义 + allow-core-thread-timeout: true # 是否允许核心线程超时,默认false + await-termination-seconds: 10 # + wait-for-tasks-to-complete-on-shut-down: true # 任务执行完,在关闭应用 +