文档章节

线程

涩谷直子
 涩谷直子
发布于 2017/07/21 16:00
字数 472
阅读 2
收藏 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;
		}
	}

 

 

 

 

© 著作权归作者所有

共有 人打赏支持
涩谷直子
粉丝 3
博文 34
码字总数 45758
作品 0
深圳
程序员

暂无文章

django rest framework 外键序列化方法与问题总结

django rest framework 外键序列化方法与问题总结 当借口中需要出现一对多关系的时候,我们可以用rest_framwork的序列化功能来处理,代码如下. # models.pyfrom django.db import modelscl...

_Change_
昨天
1
0
SingleNumber136 leetCode

Given a non-empty array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you im......

woshixin
昨天
1
0
String ,  StringBuffer ,  StringBuilder的区别

String , StringBuffer , StringBuilder的区别 String 首先,String 是用来表示一个字符串常量的,它是一个不可变对象,意味着,一旦我们创建了某个字符串之后,就不能再改变它的值了,我们可...

tsmyk0715
昨天
3
0
区块链100讲:UTXO 和 Account 模型对比

在当前区块链世界中,主要有两种记录保存方式,UTXO 模式(Unspent Transaction Output) 和 Account 模式。Bitcoin 采用的是 UTXO 模型,Ethereum 采用的 Account 模型,同样 CITA 也采用了 ...

HiBlock
昨天
1
0
Vue中路由管理器Vue Router使用介绍(三)

一、路由定义添加动态参数定义 1.路由定义项,使用:xx 方式 定义动态参数 { path:'/user/:id/:name', name:'user', component:()=>import('./views/User.vue') ...

tianma3798
昨天
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部