1、设置一个静态集合:
public static HashSet<String> pendingArray = new HashSet<>();//静态文件目录保存
2、定时刷新,添加新的元素
@Async //@Scheduled(cron = "0/10 * * * * ?") @Scheduled(cron = "0 0/2 * * * ?") public void timedTaskDataAnalysis(){ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@定时任务" + df.format(new Date())); try { synchronized (OrderDataInsertService.pendingArray) {//重点在synchronized方法上 HashSet<String> newList = orderDataAnalysisService.fileParsingGetData(newfilePath+new SimpleDateFormat("yyyyMMdd").format(new Date()),oldfilePath);//新 if(newList!=null){ int pendingSize; pendingSize = OrderDataInsertService.pendingArray.size(); //判断文件名称是否重复 OrderDataInsertService.pendingArray.addAll(newList); if(pendingSize == 0){ orderDataInsertService.fileParsingGetData(newfilePath+new SimpleDateFormat("yyyyMMdd").format(new Date()),oldfilePath); } } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); logger.info("ErrCode:-------错误描述"); } }
3、synchronized应用的具体方法实现
/** * * @return 获取交易数据文件 解析并保存 */ public void fileParsingGetData(String newfilePath,String oldfilePath){ int index = 0; TvipReturnDataType listName = new TvipReturnDataType(); List<String> newStrArrays = new ArrayList<>();//成功数据 //解析已被复制的二进制文件 遍历所有的文件信息,将数据解析放入list集合中 List<TvipSaleTicketDataVo> listAll = new ArrayList<TvipSaleTicketDataVo>(); while(pendingArray.size()>0){ index++; doProcess(newfilePath,oldfilePath); } if(pendingArray.size() == 0){ logger.info("==============================TRX文件上传文件夹为空,已操作文件个数:"+index); index=0; } } @Transactional private void doProcess(String newfilePath,String oldfilePath){ String filePath = pendingArray.iterator().next(); System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"+pendingArray.size()+filePath); try { synchronized (pendingArray) { List<String> errorStrArrays = new ArrayList<>();//错误数据 String fileName = filePath; filePath = newfilePath+connector+filePath; String headerFile = "";//头文件 String fileData = "";//数据 String crcData = "";//crc数据文件 File file1 = new File(filePath); byte[] bytes = ByteFileAnalysisUtilEntity.File2byte(file1); String[] listString = ByteFileAnalysisUtilEntity.byte2HexString(bytes).split("0D0A0D0A0D0A"); for(int i = 0;i<listString.length;i++){ if(i==0){ headerFile = listString[i];//头文件信息 }else{ if(i==listString.length-1){ crcData = listString[i];//crc文件 }else{ fileData += listString[i] + "@";//文件中的数据信息 } } } /** * 进行crc文件校验 */ Boolean returnCrcData = judgeCrcFile(filePath,crcData); boolean finalResult = false; if(returnCrcData){ //解析文件 TvipReturnDataType tvipReturnDataType= getTvipSaleTicketDataVo(fileData.split("@"),filePath); if(tvipReturnDataType.getReturnBoolean()){ finalResult = dataDuplicateCheckingInsert(tvipReturnDataType.getListTvip(),filePath,fileName); } } //解析错误, if(finalResult){ //数据新增完后对原有目标目录中的文件进行删除 logger.info("删除recv文件目录 ++++++++++++++++++++"+oldfilePath+connector+fileName); ByteFileAnalysisUtilEntity.delFile(oldfilePath+connector+fileName); }else{//TODO:解析错误,需要将错误文件存放在/data/FTP/ftpchs42/transaction/errorRecv,删除recv目录下的文件 String recvPath = oldfilePath+connector+fileName; String backDataPath = newfilePath+connector+fileName; String errorRecvPath = TvipDataRetrievalRescheduledTasks.errorfilePath+new SimpleDateFormat("yyyyMMdd").format(new Date())+connector+fileName; File s1 = new File(TvipDataRetrievalRescheduledTasks.errorfilePath+new SimpleDateFormat("yyyyMMdd").format(new Date())); s1.mkdirs(); File s = new File(recvPath); if (s.exists() && s.isFile()){ FileInputStream fiStream = null; FileOutputStream foStream = null; try { fiStream = new FileInputStream(recvPath);//读文件 // TODO: 复制到目标文件的路径 foStream = new FileOutputStream(errorRecvPath);//拷贝到目标文件夹 byte[] buff=new byte[1024*1024];//自定义一个字节缓冲区 int len=0;//保存的读到的字节的个数 while((len=fiStream.read(buff))!=-1){ foStream.write(buff,0,len); } }catch (Exception e) { //handle exception e.printStackTrace(); logger.info("ERRO ++++++++++++++++++++++++ 复制日志错误文件recv至error时出错,错误文件名称"+fileName); }finally { //handle finally clause fiStream.close();//关闭读资源 foStream.close();//关闭写资源 } }else{ logger.info("ERRO ++++++++++++++++++++++++ 复制日志错误文件recv至error时出错,recv中没有该文件,文件名称"+fileName); } try { //删除recv中数据 ByteFileAnalysisUtilEntity.delFile(recvPath); //删除backData中数据 //ByteFileAnalysisUtilEntity.delFile(backDataPath); } catch (Exception e) { //handle exception e.printStackTrace(); logger.info("ERRO ++++++++++++++++++++++++ 删除recv、backData中文件时出错,错误文件名称"+fileName); } } pendingArray.remove(fileName); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } }