feat(Spring线程池优化->yaml文档配置):

默认配置属性类: AsyncExecutionProperties
yaml配置文档:
austin-web/src/main/resources/application.yml
pull/6/head
whywhathow 3 years ago
parent ff450e42f7
commit be7efd485a

@ -35,6 +35,12 @@
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
</dependency>
<!-- yaml文档 配置化 spring线程池 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.5.6</version>
</dependency>
</dependencies>
</project>

@ -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() {

@ -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;
}
}
}

@ -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 # 任务执行完,在关闭应用
Loading…
Cancel
Save