作者 朱兆平

本地读取报文解析处理

@@ -65,3 +65,6 @@ feign.httpclient.connection-timeout=3000 @@ -65,3 +65,6 @@ feign.httpclient.connection-timeout=3000
65 wharf.interface.host=http://10.50.3.66:1983/ 65 wharf.interface.host=http://10.50.3.66:1983/
66 #新舱单运单查询数据接口,注意尾部带/符号 66 #新舱单运单查询数据接口,注意尾部带/符号
67 nmms.interface.host=http://10.50.3.82:8081/ 67 nmms.interface.host=http://10.50.3.82:8081/
  68 +#新舱单网站内网接口,注意尾部带/符号
  69 +nmms.site.host=https://nmms.zzcargo.com:8443/
  70 +custom.receptDirectory= bw/read/
@@ -48,6 +48,13 @@ @@ -48,6 +48,13 @@
48 <groupId>org.springframework.cloud</groupId> 48 <groupId>org.springframework.cloud</groupId>
49 <artifactId>spring-cloud-starter-openfeign</artifactId> 49 <artifactId>spring-cloud-starter-openfeign</artifactId>
50 </dependency> 50 </dependency>
  51 + <!--util依赖-->
  52 + <dependency>
  53 + <groupId>com.tianbo</groupId>
  54 + <artifactId>util</artifactId>
  55 + <version>1.0-SNAPSHOT</version>
  56 + <scope>compile</scope>
  57 + </dependency>
51 58
52 <dependency> 59 <dependency>
53 <groupId>mysql</groupId> 60 <groupId>mysql</groupId>
  1 +package com.sy.task;
  2 +
  3 +import com.sy.bwAnalysis.AnalysisRoute;
  4 +import com.sy.logic.LiftBar;
  5 +import com.sy.utils.XMLThreadPoolFactory;
  6 +import com.tianbo.util.Date.DateUtil;
  7 +import com.tianbo.util.IO.FileTool;
  8 +import org.apache.commons.io.FileUtils;
  9 +import org.springframework.beans.factory.annotation.Value;
  10 +import org.springframework.scheduling.annotation.Scheduled;
  11 +import org.springframework.stereotype.Component;
  12 +
  13 +import java.io.File;
  14 +import java.text.SimpleDateFormat;
  15 +import java.util.Date;
  16 +import java.util.List;
  17 +import java.util.concurrent.CountDownLatch;
  18 +import java.util.concurrent.ThreadPoolExecutor;
  19 +
  20 +/**
  21 + * 回执解析定时任务
  22 + */
  23 +@Component
  24 +
  25 +public class TaskAnalysis {
  26 + private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TaskAnalysis.class);
  27 + //回执读取目录
  28 + @Value("${custom.receptDirectory}")
  29 + private String receptDir;
  30 +
  31 + /**
  32 + * 线程数量
  33 + */
  34 + private final static int theadamount = 16;
  35 +
  36 + @Scheduled(fixedRate = 5000)
  37 + public void startTask(){
  38 + final SimpleDateFormat sdf = new SimpleDateFormat(
  39 + "yyyy-MM-dd HH:mm:ss");
  40 +
  41 + final String startTime = sdf.format(new Date());
  42 +
  43 + //读取目录
  44 + String readDir = receptDir;
  45 +
  46 + //初始化线程池
  47 + ThreadPoolExecutor threadPool = XMLThreadPoolFactory.instance("file-read");
  48 +
  49 + try {
  50 + File fileDirectory = new File(readDir);
  51 + List<File> files = FileTool.readDirectoryFiles(fileDirectory);
  52 + //文件数量大于50个,每次只解析前50个
  53 + if (files!=null && !files.isEmpty() && files.size()>theadamount){
  54 + CountDownLatch latch = new CountDownLatch(theadamount);
  55 + log.trace("本地解析报文任务开始{},剩余处理文件数量:{}",startTime,files.size());
  56 + for (int i=0;i<theadamount;i++){
  57 + threadJbob(files.get(i),latch,"",threadPool);
  58 + }
  59 + latch.await();
  60 + }
  61 + //文件数量小于50个,全部一次解析完
  62 + else if (files!=null && !files.isEmpty() && files.size()<theadamount){
  63 + CountDownLatch latch = new CountDownLatch(files.size());
  64 + log.trace("本地解析报文任务开始{},剩余处理文件数量文件数量:{}",startTime,files.size());
  65 + for (int i=0;i<files.size();i++){
  66 + threadJbob(files.get(i),latch,"",threadPool);
  67 + }
  68 + latch.await();
  69 + }
  70 +
  71 + }catch (Exception e){
  72 + e.printStackTrace();
  73 + log.error("获取目录文件出错",e);
  74 + }
  75 +
  76 + log.info("本地解析报文任务结束{}",sdf.format(new Date()));
  77 +
  78 + }
  79 +
  80 + private void threadJbob(File file,CountDownLatch latch,String transToCfps,ThreadPoolExecutor threadPool){
  81 + try{
  82 + AnalysisRoute analysisRoute=new AnalysisRoute();
  83 + String message = com.sy.utils.FileTool.readfile(file,"UTF-8");
  84 + analysisRoute.setMessage(message);
  85 + threadPool.execute(analysisRoute);
  86 + FileUtils.moveFileToDirectory(file,new File("bw/handled/"),true);
  87 + }catch (Exception e){
  88 + log.error("线程解析出错{}",e.toString());
  89 + }
  90 +
  91 + }
  92 +
  93 +}
@@ -46,6 +46,11 @@ public final class FileTool { @@ -46,6 +46,11 @@ public final class FileTool {
46 return fileToString; 46 return fileToString;
47 } 47 }
48 48
  49 + public static String readfile(File file,String encode) throws IOException{
  50 + String fileToString = CharasetCheck.getUTF8String(FileUtils.readFileToString(file, encode));
  51 + return fileToString;
  52 + }
  53 +
49 public static List<File> readDirectoryFiles(File dir){ 54 public static List<File> readDirectoryFiles(File dir){
50 List<File> files = (List<File>)FileUtils.listFiles(dir,new String[]{"xml"},false); 55 List<File> files = (List<File>)FileUtils.listFiles(dir,new String[]{"xml"},false);
51 return files; 56 return files;