SpringBoot异步编程

配置@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;

/**
* @ClassName: AsyncConfig  
* @Description: TODO(异步线程配置)  
* @author gangyu2
* @date 2020年02月01日 上午15:58:10  
 */
@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() {
    // Spring 默认配置是核心线程数大小为1,最大线程容量大小不受限制,队列容量也不受限制。
    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 {
        //1.最终疑似数据集合
        List<EmpiPatientMerge> resultSuspectList=new ArrayList<EmpiPatientMerge>();
        //2.最终符合数据集合
        List<EmpiPatientMerge> resultMergeList=new ArrayList<EmpiPatientMerge>();
        Map<String,Object> result=new HashMap<String,Object>();

		//3.疑似数据列表
        String type=StringUtils.isNotBlank(suspectMainData.getIdCard())?"1":"2";
		List<EmpiPatientMerge> suspectList=patientMergeService.findSupectDataPage(startPostion, endPostion, suspectMainData.getMpi(),type);
		if(CollUtil.isNotEmpty(suspectList)) {
			//4.转换主数据计算模型
	        ComparisonModelBO comparisonModle =new ComparisonModelBO();
	        MergeDataTask.conversionModel(comparisonModle, suspectMainData);
	        //5.疑似数据模型
	        ComparisonModelBO beComparisonModel=null;
	        for (EmpiPatientMerge beSuspectData : suspectList) {
	        	beComparisonModel=new ComparisonModelBO();
	        	//6.转换被比较计算模型
	        	MergeDataTask.conversionModel(beComparisonModel, beSuspectData);
	        	//7.开始计算相似分值
	            ComparisonUtil.score(comparisonModle,beComparisonModel);
	            //8.处理相似分值
	            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
				Map<String,String> repeatData=new HashMap<String,String>();
				//处理数据Futrue
				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();
				}

				//更新疑似的job_type标志为 SysCode.EMPI.MERGE_TYPE.JOB_TYPE
		        EmpiPatientMerge patient=new EmpiPatientMerge();
		        patient.setJobType(SysCode.EMPI.MERGE_TYPE.JOB_TYPE);
		        patient.setId(list.get(0).getId());
		        patientMergeService.patientMergeData(patient);
			}else {
				flag=false;
			}
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值