业务背景
前端发起请求,后端处理完后(约10W+),进行保存,后端还未处理成功,前端就报了超时(这里不对前端的请求时间做修改),但是其实后端一直都在保存,导致用户体验及其不佳。
实施概述
通过@Async注解,使前端发起请求后,立刻【伪完成】(loading也没有,能直接进行其他操作),但是此时后端在处理。
核心代码
参考的是form服务的异步下载,不对的地方望指正。
1、PropertiesEvent
public class PropertiesEvent extends ApplicationEvent {
public PropertiesEvent(Object source) {
super(source);
}
}
2、
/**
* 异步任务执行配置
*/
@EnableAsync(proxyTargetClass = true)
@EnableConfigurationProperties(FormAsyncTaskProperties.class)
@Configuration
public class FormAsyncTaskConfig implements ApplicationListener<PropertiesEvent> {
@Autowired
private FormAsyncTaskProperties formAsyncTaskProperties;
private boolean isInit=false;
private void resetExecutor(){
ThreadPoolTaskExecutor executor= SpringUtil.getBean("compulsoryCourseExecutor");
String corePoolJson = formAsyncTaskProperties.getCorePoolJson();
String [] corePoolJsons= corePoolJson.split("[|]");
executor.setCorePoolSize(Integer.parseInt(corePoolJsons[0]));
executor.setMaxPoolSize(Integer.parseInt(corePoolJsons[1]));
executor.setQueueCapacity(Integer.parseInt(corePoolJsons[2]));
executor.setThreadNamePrefix("compulsoryCourseExecutor_");
}
@Bean("compulsoryCourseExecutor")
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new CustomThreadPoolTaskExecutor();
String corePoolJson = formAsyncTaskProperties.getCorePoolJson();
String [] corePoolJsons= corePoolJson.split("[|]");
executor.setCorePoolSize(Integer.parseInt(corePoolJsons[0]));
executor.setMaxPoolSize(Integer.parseInt(corePoolJsons[1]));
executor.setQueueCapacity(Integer.parseInt(corePoolJsons[2]));
//线程池前缀
executor.setThreadNamePrefix("compulsoryCourseExecutor_");
/*
rejection-policy:当pool已经达到max size的时候,如何处理新任务
CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
*/
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
this.isInit=true;
return executor;
}
@Override
public void onApplicationEvent(PropertiesEvent propertiesEvent) {
if(this.isInit){
this.resetExecutor();
}
}
}
3、FormAsyncTaskProperties
@ConfigurationProperties(prefix="compulsorycourseasynctask")
@RefreshScope
public class FormAsyncTaskProperties {
/**
* 线程池维护线程的最小数量|最大数量|队列最大长度.
*/
private String corePoolJson="5|20|50";
public String getCorePoolJson() {
return corePoolJson;
}
public void setCorePoolJson(String corePoolJson) {
this.corePoolJson = corePoolJson;
SpringUtil.publishEvent(new PropertiesEvent(this));
}
}
4、需要在服务的启动类加上@EnableAsync注解。
5、在具体的业务代码的serviceImpl加上@Async(value = “compulsoryCourseExecutor”)
如 XXXserviceImpl
//具体的方法
@Async(value = "compulsoryCourseExecutor")
public JsonResult handleCourseCompulsory() {
JsonResult result = new JsonResult(true);
List userDtoList = new ArrayList<>();
}
6、若是大批量保存实体,可使用
this.saveBatch(entity,1000);