文档章节

线程

涩谷直子
 涩谷直子
发布于 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
深圳
程序员

暂无文章

qduoj~前端~二次开发~打包docker镜像并上传到阿里云容器镜像仓库

上一篇文章https://my.oschina.net/finchxu/blog/1930017记录了怎么在本地修改前端,现在我要把我的修改添加到部署到本地的前端的docker容器中,然后打包这个容器成为一个本地镜像,然后把这...

虚拟世界的懒猫
33分钟前
1
0
UML中 的各种符号含义

Class Notation A class notation consists of three parts: Class Name The name of the class appears in the first partition. Class Attributes Attributes are shown in the second par......

hutaishi
44分钟前
0
0
20180818 上课截图

小丑鱼00
今天
1
0
Springsecurity之SecurityContextHolderStrategy

注:下面分析的版本是spring-security-4.2.x,源码的github地址是: https://github.com/spring-projects/spring-security/tree/4.2.x 先上一张图: 图1 SecurityContextHolderStrategy的三个......

汉斯-冯-拉特
今天
0
0
LNMP架构(Nginx负载均衡、ssl原理、生成ssl密钥对、Nginx配置ssl)

Nginx负载均衡 网站的访问量越来越大,服务器的服务模式也得进行相应的升级,比如分离出数据库服务器、分离出图片作为单独服务,这些是简单的数据的负载均衡,将压力分散到不同的机器上。有时...

蛋黄_Yolks
今天
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部