配置@Async
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {
private static final int CORE_POOL_SIZE = 30;
private static final int MAX_POOL_SIZE = 50;
private static final int QUEUE_CAPACITY = 100;
@Bean
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(CORE_POOL_SIZE);
executor.setMaxPoolSize(MAX_POOL_SIZE);
executor.setQueueCapacity(QUEUE_CAPACITY);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.setThreadNamePrefix("异步线程-");
executor.initialize();
return executor;
}
}
新建一个异步接口
@Service
public class ComparisonSuspectAsync{
@Async
public Future<Map<String,Object>> recursiveComparisonSuspectNew(EmpiPatientMerge suspectMainData,int startPostion,int endPostion) throws Exception {
List<EmpiPatientMerge> resultSuspectList=new ArrayList<EmpiPatientMerge>();
List<EmpiPatientMerge> resultMergeList=new ArrayList<EmpiPatientMerge>();
Map<String,Object> result=new HashMap<String,Object>();
String type=StringUtils.isNotBlank(suspectMainData.getIdCard())?"1":"2";
List<EmpiPatientMerge> suspectList=patientMergeService.findSupectDataPage(startPostion, endPostion, suspectMainData.getMpi(),type);
if(CollUtil.isNotEmpty(suspectList)) {
ComparisonModelBO comparisonModle =new ComparisonModelBO();
MergeDataTask.conversionModel(comparisonModle, suspectMainData);
ComparisonModelBO beComparisonModel=null;
for (EmpiPatientMerge beSuspectData : suspectList) {
beComparisonModel=new ComparisonModelBO();
MergeDataTask.conversionModel(beComparisonModel, beSuspectData);
ComparisonUtil.score(comparisonModle,beComparisonModel);
MergeDataTask.handleSimScore(suspectMainData, beSuspectData, beComparisonModel, resultSuspectList, resultMergeList);
}
}
result.put("resultSuspectList", resultSuspectList);
result.put("resultMergeList", resultMergeList);
result.put("suspectMainData", suspectMainData);
return new AsyncResult<Map<String, Object>>(result);
}
}
调用异步接口
public void asyncComparisonSuspect() throws Exception {
boolean flag=true;
while(flag) {
List<EmpiPatientMerge> list=patientMergeService.findSuspectDataList(0, 1, "1", null);
if(CollUtil.isNotEmpty(list)) {
List<Future<Map<String, Object>>> comparisonfutures=new ArrayList<Future<Map<String, Object>>>();
for(EmpiPatientMerge suspectMainData:list) {
int startPostion=0;
int endPostion=10000;
int dataSize=10000;
int totalSize=totalPage(suspectMainData,dataSize);
for(int i=1;i<=totalSize;i++) {
Future<Map<String, Object>> f=comparisonSuspectAsync.recursiveComparisonSuspectNew(suspectMainData, startPostion,endPostion);
startPostion+=dataSize;
endPostion+=dataSize;
comparisonfutures.add(f);
}
}
Map<String,String> repeatData=new HashMap<String,String>();
List<Future<Boolean>> handleSuspectFutures=new ArrayList<Future<Boolean>>();
for (Future<Map<String,Object>> future : comparisonfutures) {
Map<String,Object> futureMap = (Map<String,Object> )future.get();
List<EmpiPatientMerge> resultSuspectList=(List<EmpiPatientMerge>)futureMap.get("resultSuspectList");
List<EmpiPatientMerge> resultMergeList=(List<EmpiPatientMerge>)futureMap.get("resultMergeList");
EmpiPatientMerge suspectMainData=(EmpiPatientMerge)futureMap.get("suspectMainData");
if(CollUtil.isNotEmpty(resultMergeList)) {
for(Iterator<EmpiPatientMerge> it=resultMergeList.iterator();it.hasNext();) {
repeatSuspectData(repeatData, it);
}
Future<Boolean> f=mergeAndSuspectAsync.autoMerge(suspectMainData, resultMergeList);
handleSuspectFutures.add(f);
}
if(CollUtil.isNotEmpty(resultSuspectList)) {
for(Iterator<EmpiPatientMerge> it=resultSuspectList.iterator();it.hasNext();) {
repeatSuspectData(repeatData,it);
}
Future<Boolean> f=mergeAndSuspectAsync.manualHandle(suspectMainData, resultSuspectList);
handleSuspectFutures.add(f);
}
}
for (Future<Boolean> future : handleSuspectFutures) {
future.get();
}
EmpiPatientMerge patient=new EmpiPatientMerge();
patient.setJobType(SysCode.EMPI.MERGE_TYPE.JOB_TYPE);
patient.setId(list.get(0).getId());
patientMergeService.patientMergeData(patient);
}else {
flag=false;
}
}
}