项目中使用callable 返回,释放tomcat线程 增加吞吐量
//列表查询
@ApiOperation(value = "职务、部门、工种列表", notes = "下拉框列表,职务、部门、工种列表大数据高性能")
@RequestMapping(value = "/listParatest", method = RequestMethod.GET)
public Callable<Map<String, Object>> listPara2() {
System.out.println("主线开始"+Thread.currentThread()+":"+System.currentTimeMillis());
Callable<Map<String, Object>> personinfo =new Callable<Map<String, Object>>() {
@Override
public Map<String, Object> call() throws Exception {
System.out.println("子线开始"+Thread.currentThread()+":"+System.currentTimeMillis());
Map<String, Object> personMap = personService.listPostAndDepAndTypeWork2();//用于下拉列表选择职务
System.out.println("子程结束"+Thread.currentThread()+":"+System.currentTimeMillis());
return personMap;
}
};
System.out.println("主线程结束"+Thread.currentThread()+":"+System.currentTimeMillis());
return personinfo;
}
@Override
public Map<String, Object> listPostAndDepAndTypeWork2() {
ExecutorService tasks = Executors.newFixedThreadPool(3);
Callable<List<Map<String, Object>>> postinfo = new Callable<List<Map<String, Object>>>() {
@Override
public List<Map<String, Object>> call() throws Exception {
List<Map<String, Object>> postList = postDao.findAllPost();//用于下拉列表选择职务
return postList;
}
};
Callable<List<Map<String, Object>>> departmentinfo = new Callable<List<Map<String, Object>>>() {
@Override
public List<Map<String, Object>> call() throws Exception {
List<Map<String, Object>> departmentList = departmentDao.findAllDepartment();//用于下拉列表选择部门
return departmentList;
}
};
Callable<List<Map<String, Object>>> typeworkinfo = new Callable<List<Map<String, Object>>>() {
@Override
public List<Map<String, Object>> call() throws Exception {
List<Map<String, Object>> typeworkList = typeworkDao.findAllTypework();//用于下拉列表选择工种
return typeworkList;
}
};
FutureTask<List<Map<String, Object>>> postTask = new FutureTask<List<Map<String, Object>>>(postinfo);
FutureTask<List<Map<String, Object>>> departmentTask = new FutureTask<List<Map<String, Object>>>(departmentinfo);
FutureTask<List<Map<String, Object>>> typeworkTask = new FutureTask<List<Map<String, Object>>>(typeworkinfo);
tasks.submit(postTask);
tasks.submit(departmentTask);
tasks.submit(typeworkTask);
Map<String, Object> map = new HashMap<String, Object>();
try {
map.put("postList", postTask.get());
map.put("departmentList", departmentTask.get());
map.put("typeworkList", typeworkTask.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return map;
}
警告提醒
2019-01-24 10:58:12 920 [WARN]:
!!!
An Executor is required to handle java.util.concurrent.Callable return values.
Please, configure a TaskExecutor in the MVC config under "async support".
The SimpleAsyncTaskExecutor currently in use is not suitable under load.
-------------------------------
Request URI: '/AnRanPSLS/person/listParatest'
!!!
后参照网上资料加入以下重写方法来源:https://www.jb51.net/article/134289.htm
@Configuration
@ConfigurationProperties(prefix = "web-mvc-async")
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
public void configureAsyncSupport(final AsyncSupportConfigurer configurer) {
configurer.setDefaultTimeout(60 * 1000L);
configurer.registerCallableInterceptors(timeoutInterceptor());
configurer.setTaskExecutor(threadPoolTaskExecutor());
}
@Bean
public TimeoutCallableProcessingInterceptor timeoutInterceptor() {
return new TimeoutCallableProcessingInterceptor();
}
@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor t = new ThreadPoolTaskExecutor();
t.setCorePoolSize(corePoolSize);
t.setMaxPoolSize(maxPoolSize);
t.setQueueCapacity(queueCapacity);
t.setThreadNamePrefix("WYF-Thread-");
return t;
}
}
springboot2 继承WebMvcConfigurationSupport 后自动配置失效,swagger-ui.html 无法访问,查资料后加入以下配置来源:https://blog.csdn.net/liu0bing/article/details/80826590
@Bean
LoginInterceptor localInterceptor() {
return new LoginInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/user/login")
.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
发现项目中使用的entityManager 的
query2.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
NativeQueryImpl报错,自动化配置失效导致。后又查找发现不继承WebMvcConfigurationSupport,改为实现
WebMvcConfigurer接口即可,具体实现如下代码即可
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void configureAsyncSupport(final AsyncSupportConfigurer configurer) {
configurer.setDefaultTimeout(60 * 1000L);
configurer.registerCallableInterceptors(timeoutInterceptor());
configurer.setTaskExecutor(threadPoolTaskExecutor());
}
@Bean
public TimeoutCallableProcessingInterceptor timeoutInterceptor() {
return new TimeoutCallableProcessingInterceptor();
}
@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor t = new ThreadPoolTaskExecutor();
t.setCorePoolSize(10);
t.setMaxPoolSize(100);
t.setQueueCapacity(20);
t.setThreadNamePrefix("WYF-Thread-");
return t;
}
}