You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
udi-wms-java/src/main/java/com/glxp/api/task/TaskExecutorConfig.java

52 lines
2.2 KiB
Java

2 years ago
package com.glxp.api.task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2 years ago
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.lang.reflect.Method;
import java.util.concurrent.Executor;
2 years ago
//@EnableAsync
//@Configuration
@ConditionalOnProperty(prefix = "spring.sgrain.async-thread-pool", name = "enable", havingValue = "true", matchIfMissing = false)
2 years ago
public class TaskExecutorConfig implements AsyncConfigurer {
private static final Logger log = LoggerFactory.getLogger(TaskExecutorConfig.class);
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
threadPool.setCorePoolSize(10);//当前线程数
threadPool.setMaxPoolSize(200);// 最大线程数
threadPool.setQueueCapacity(Integer.MAX_VALUE);//线程池所使用的缓冲队列
2 years ago
threadPool.setWaitForTasksToCompleteOnShutdown(true);//等待任务在关机时完成--表明等待所有线程执行完
threadPool.setAwaitTerminationSeconds(60 * 15);// 等待时间 默认为0此时立即停止并没等待xx秒后强制停止
threadPool.setThreadNamePrefix("MyAsync-");// 线程名称前缀
threadPool.initialize(); // 初始化
return threadPool;
}
class MyAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
//手动处理捕获的异常
@Override
public void handleUncaughtException(Throwable throwable, Method method, Object... obj) {
System.out.println("-------------》》》捕获线程异常信息");
log.info("Exception message - " + throwable.getMessage());
log.info("Method name - " + method.getName());
for (Object param : obj) {
log.info("Parameter value - " + param);
}
}
}
2 years ago
}