线程
线程
涩谷直子 发表于10个月前
线程
  • 发表于 10个月前
  • 阅读 2
  • 收藏 0
  • 点赞 0
  • 评论 0

移动开发云端新模式探索实践 >>>   

插入方法:

public List insertPhone(String idCmsUploadFiles, Map assignMap) throws IOException{
		CmsLogger.audit("审批通过,开始插入发分明细表:时间为:" + DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ssS") + "---参数为:idCmsUploadFiles=" + idCmsUploadFiles + "---assignMap=" + JSONObject.toJSONString(assignMap));
		List list = new ArrayList();
		try{
			//获取切分的文件
			List splitFileList = assignIntegrationService.listCmsSplitFilesByUploadFilesId(idCmsUploadFiles);
	        int runnableNum = splitFileList.size();
	        CmsLogger.audit("获取到切分文件"+runnableNum+"个");
	        CyclicBarrier barrier  = new CyclicBarrier(runnableNum);
	        for(int k = 0 ; k < runnableNum ; k++){
	        	Map splitFileMap = (Map)splitFileList.get(k);
	        	CmsLogger.audit("获取到切分文件对象:splitFileMap = " + JSONObject.toJSONString(splitFileMap));
	        	Future<String> future = executor.submit(new insertPhoneRunnable(barrier,assignMap,splitFileMap));
	        	list.add(future);
	        }
		} catch(Exception e){
			CmsLogger.error("审批通过,开始插入发分明细表出现异常:"  + e.getMessage(),e);
		} finally {
			executor.shutdown();
		}
		return list;
	}

 

插入线程类:

class insertPhoneRunnable implements Callable<String>{
		
		private CyclicBarrier cyclicBarrier;
		private Map splitFileMap;
		private Map assignMap;
		private String idAssignIntegration;
		public insertPhoneRunnable(CyclicBarrier cyclicBarrier,Map assignMap,Map splitFileMap) {
			this.cyclicBarrier = cyclicBarrier;
			this.assignMap = assignMap;
			this.splitFileMap = splitFileMap;
			idAssignIntegration = (String) assignMap.get("idAssignIntegration");
		}

		@Override
		public String call() {
			String flag = "00";
            try {
				String splitFilePath = (String) splitFileMap.get("splitFilePath");
				String batch = (String) splitFileMap.get("threadId");
				CmsLogger.audit("线程"+Thread.currentThread().getName()+"正在读取文件写入发分明细表,splitFilePath=" +splitFilePath + "batch=" + batch);
				
				List paramList = new ArrayList();
				BufferedReader br = null;
				File file = null;
				String content = "";
				int i = 0;
				try{
					file = new File(splitFilePath);
					br = new BufferedReader(new FileReader(file));
					
					while((content = br.readLine()) != null){
						i++;
						String[] cons = content.split(",");
						Map map = new HashMap();
						map.put("phoneNo", cons[1]);
						map.put("idIntegrationAssign", idAssignIntegration);
						map.put("batch", batch);
						map.put("integration", cons[2]);
						map.put("status", "0");
						paramList.add(map);
						if(i % 20 == 0){
							assignIntegrationService.insertPhoneToDelivery(paramList);
							paramList.clear();
						}
					}
					if(!paramList.isEmpty()){
						assignIntegrationService.insertPhoneToDelivery(paramList);
					}
				} catch (Exception e){
					flag = "01";
					//记录插入失败的文件
					CmsLogger.error("插入发分明细表出现异常,异常发分文件为:" + splitFilePath,e);
					Map tmpMap = assignIntegrationService.getAssignIntegration(idAssignIntegration);
					Map errorMap = new HashMap();
					errorMap.put("idAssignIntegration", idAssignIntegration);
					errorMap.put("phoneListReason", tmpMap.get("phoneListReason") + "[插入发分明细表出现异常,异常发分文件为:" + splitFilePath +"]");
					errorMap.put("updateDate", new Timestamp(System.currentTimeMillis()));
					assignIntegrationService.updateAssignIntegrationById(errorMap);
				} finally {
					if(br != null){
						try {
							br.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
					file = null;
					CmsLogger.audit("线程"+Thread.currentThread().getName()+"写入文件["+splitFilePath+"]完毕,共"+i+"条记录,等待其他线程写入完毕");
					cyclicBarrier.await();
				}
            } catch (InterruptedException e) {
                CmsLogger.error("插入发分明细表出现异常," + e.getMessage(),e);
                flag = "01";
            }catch(BrokenBarrierException e){
                CmsLogger.error("插入发分明细表出现异常," + e.getMessage(),e);
                flag = "01";
            }
			CmsLogger.audit("所有线程写入完毕,返回值为"+ flag +",时间为:" + DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ssS"));
			return flag;
		}
	}

 

 

 

 

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 2
博文 26
码字总数 31642
×
涩谷直子
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: