Commit ce3a91c1 authored by lcl's avatar lcl

解决异步调用报错问题

parent 7b8853bd
...@@ -8,10 +8,14 @@ import org.springframework.beans.factory.annotation.Qualifier; ...@@ -8,10 +8,14 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport; import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
/** /**
* 异步配置 * 异步配置
...@@ -22,16 +26,56 @@ import java.util.concurrent.ScheduledExecutorService; ...@@ -22,16 +26,56 @@ import java.util.concurrent.ScheduledExecutorService;
@Configuration @Configuration
public class AsyncConfig extends AsyncConfigurerSupport { public class AsyncConfig extends AsyncConfigurerSupport {
@Autowired // @Autowired
@Qualifier("scheduledExecutorService") // @Qualifier("scheduledExecutorService")
private ScheduledExecutorService scheduledExecutorService; // private ScheduledExecutorService scheduledExecutorService;
//
// /**
// * 自定义 @Async 注解使用系统线程池
// */
// @Override
// public Executor getAsyncExecutor() {
// return scheduledExecutorService;
// }
/**
* 自定义 @Async 注解使用系统线程池
*/
@Override @Override
public Executor getAsyncExecutor() { public Executor getAsyncExecutor() {
return scheduledExecutorService; ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
// 核心线程数:线程池创建时候初始化的线程数
taskExecutor.setCorePoolSize(10);
// 最大线程数:线程池最大的线程数,只有在缓冲队列满了之后才会申请超过核心线程数的线程
taskExecutor.setMaxPoolSize(20);
// 缓冲队列:用来缓冲执行任务的队列
taskExecutor.setQueueCapacity(50);
// 允许线程的空闲时间60秒:当超过了核心线程之外的线程在空闲时间到达之后会被销毁
taskExecutor.setKeepAliveSeconds(60);
// 线程池名的前缀:设置好了之后可以方便我们定位处理任务所在的线程池
taskExecutor.setThreadNamePrefix("HiTask-");
// 缓冲队列满了之后的拒绝策略:由调用线程处理(一般是主线程)
//taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
taskExecutor.initialize();
//解决使用@Async注解,获取不到上下文信息的问题
taskExecutor.setTaskDecorator(runnable -> {
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
return ()->{
try {
// 我们set 进去 ,其实是一个ThreadLocal维护的.
RequestContextHolder.setRequestAttributes(requestAttributes);
runnable.run();
} finally {
// 最后记得释放内存
RequestContextHolder.resetRequestAttributes();
}
};
});
return taskExecutor;
} }
/** /**
......
...@@ -50,8 +50,8 @@ public interface CbProjectFileService extends IService<CbProjectFile> { ...@@ -50,8 +50,8 @@ public interface CbProjectFileService extends IService<CbProjectFile> {
/** /**
* 查询项目文件解析列表 * 查询项目文件解析列表
* @param projectId 项目ID * @param projectId 项目ID
* @param cbType 成本阶段(0:标前成本、1:标后成本、2:转固成本) * @param cbType 成本类型(0:直接费成本、1:工料汇总、2:措施项目、3:其他项目、4:现场经费、5:成本汇总)
* @param cbStage 成本类型(0:直接费成本、1:工料汇总、2:措施项目、3:其他项目、4:现场经费、5:成本汇总) * @param cbStage 成本阶段(0:标前成本、1:标后成本、2:转固成本)
* @return * @return
*/ */
List<CbProjectFile> selectAnalysisList(Long projectId, Integer cbType, Integer cbStage); List<CbProjectFile> selectAnalysisList(Long projectId, Integer cbType, Integer cbStage);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment