作者 朱兆平

解决针对不规范报文的解析优化

1 #登录账号 1 #登录账号
2 -loginname = KKW1 2 +loginname = KAKO
3 #登录密码 3 #登录密码
4 -loginpass = KKW1 4 +loginpass = KAKO
5 #发送报文目录,相对程序目录 5 #发送报文目录,相对程序目录
6 -readDirectory = /Users/mrz/Downloads/3.CV972/1.CV报文 6 +readDirectory = /Users/mrz/Downloads/1.woowo
7 #接收存储报文目录 7 #接收存储报文目录
8 bakDirectory = xmlFromImf 8 bakDirectory = xmlFromImf
9 -#程序入库出错备份报文目录  
10 -errBakDirectory = xmlerr 9 +#是否需要发送报文,默认N不发,Y将发送readDirectory下的XML扩展名的报文
  10 +isNeedSend = Y
11 11
12 #IMF MEAT报头配置 12 #IMF MEAT报头配置
13 13
@@ -48,8 +48,20 @@ public class IMFSaveHandle { @@ -48,8 +48,20 @@ public class IMFSaveHandle {
48 48
49 public void handle(String xmlmessage){ 49 public void handle(String xmlmessage){
50 try { 50 try {
  51 + //有人发报文头部不按规范发
  52 + xmlmessage = xmlmessage.replace("<Msg>","<MSG>")
  53 + .replace("<msg>","<MSG>")
  54 + .replace("</Msg>","</MSG>")
  55 + .replace("</msg>","</MSG>");
51 Document document = DocumentHelper.parseText(xmlmessage); 56 Document document = DocumentHelper.parseText(xmlmessage);
52 T_ETL_MESSAGE message = new T_ETL_MESSAGE(); 57 T_ETL_MESSAGE message = new T_ETL_MESSAGE();
  58 +
  59 + message.setSndr(XMLXPath.getSingleValueByPath(document,SNDR));
  60 + message.setRcvr(XMLXPath.getSingleValueByPath(document,RCVR));
  61 + message.setType(XMLXPath.getSingleValueByPath(document,TYPE));
  62 + message.setStyp(XMLXPath.getSingleValueByPath(document,STYP));
  63 + message.setSeqn(XMLXPath.getSingleValueByPath(document,SEQN));
  64 +
53 String ddtm = XMLXPath.getSingleValueByPath(document,DDTM); 65 String ddtm = XMLXPath.getSingleValueByPath(document,DDTM);
54 Date ddtmDate = DateUtil.formatByyyyyMMddHHmmss(ddtm); 66 Date ddtmDate = DateUtil.formatByyyyyMMddHHmmss(ddtm);
55 message.setDdtm(ddtmDate); 67 message.setDdtm(ddtmDate);
@@ -58,11 +70,7 @@ public class IMFSaveHandle { @@ -58,11 +70,7 @@ public class IMFSaveHandle {
58 message.setAppid("W"); 70 message.setAppid("W");
59 message.setEtltim(new Date()); 71 message.setEtltim(new Date());
60 72
61 - message.setSndr(XMLXPath.getSingleValueByPath(document,SNDR));  
62 - message.setRcvr(XMLXPath.getSingleValueByPath(document,RCVR));  
63 - message.setType(XMLXPath.getSingleValueByPath(document,TYPE));  
64 - message.setStyp(XMLXPath.getSingleValueByPath(document,STYP));  
65 - message.setSeqn(XMLXPath.getSingleValueByPath(document,SEQN)); 73 +
66 74
67 message.setContent(xmlmessage); 75 message.setContent(xmlmessage);
68 76
@@ -34,7 +34,7 @@ public class IMF_Reader extends Thread{ @@ -34,7 +34,7 @@ public class IMF_Reader extends Thread{
34 } 34 }
35 35
36 try { 36 try {
37 - Thread.sleep(30000L); 37 + Thread.sleep(500L);
38 } catch (InterruptedException var3) { 38 } catch (InterruptedException var3) {
39 var3.printStackTrace(); 39 var3.printStackTrace();
40 40
@@ -20,6 +20,7 @@ public class IMF_Sender extends Thread{ @@ -20,6 +20,7 @@ public class IMF_Sender extends Thread{
20 public static boolean isrunning; 20 public static boolean isrunning;
21 private IMFClient client; 21 private IMFClient client;
22 private String content; 22 private String content;
  23 +
23 public IMF_Sender(IMFClient client) { 24 public IMF_Sender(IMFClient client) {
24 this.client = client; 25 this.client = client;
25 } 26 }
@@ -80,4 +81,12 @@ public class IMF_Sender extends Thread{ @@ -80,4 +81,12 @@ public class IMF_Sender extends Thread{
80 isrunning=false; 81 isrunning=false;
81 logger.info("<<<<<<<<<<<发送线程结束>>>>>>>>>>>>"); 82 logger.info("<<<<<<<<<<<发送线程结束>>>>>>>>>>>>");
82 } 83 }
  84 +
  85 + public String getContent() {
  86 + return content;
  87 + }
  88 +
  89 + public void setContent(String content) {
  90 + this.content = content;
  91 + }
83 } 92 }
@@ -19,34 +19,43 @@ public class IMF_Task { @@ -19,34 +19,43 @@ public class IMF_Task {
19 public static boolean LOGIN_OK = false; 19 public static boolean LOGIN_OK = false;
20 public static boolean isSuc = true; 20 public static boolean isSuc = true;
21 public static IMFClient client = null; 21 public static IMFClient client = null;
  22 + public static String loginname;
  23 + public static String loginpass;
  24 + public static String isNeedSend = "N";
  25 +
22 26
23 @Scheduled(fixedRate = 5000) 27 @Scheduled(fixedRate = 5000)
24 private static void start() throws Exception { 28 private static void start() throws Exception {
25 PropertyConfigurator.configure("config/log4j.properties"); 29 PropertyConfigurator.configure("config/log4j.properties");
26 client = IMFClientFactory.createInstance(); 30 client = IMFClientFactory.createInstance();
27 - String loginname = FileTool.readProperties("loginname");  
28 - String loginpass = FileTool.readProperties("loginpass"); 31 + loginname= FileTool.readProperties("loginname");
  32 + loginpass= FileTool.readProperties("loginpass");
  33 + isNeedSend= FileTool.readProperties("isNeedSend");
29 34
30 35
31 - if (client != null) {  
32 - IMF_Reader reader = new IMF_Reader(client);  
33 - if (!IMF_Reader.isrunning) {  
34 - reader.start();  
35 - logger.info("*********KAKO读取线程已开启***********");  
36 - } else {  
37 - // logger.info("*********KAKO读取线程已开启-不再启动线程*********");  
38 - } 36 + //登录
  37 + if (!LOGIN_OK) {
  38 + loginIMF(client, loginname, loginpass, "config/imf_config.properties");
  39 + }
39 40
  41 + //启动读取线程
  42 + if (client != null) {
  43 + IMF_Reader reader = new IMF_Reader(client);
  44 + if (!IMF_Reader.isrunning) {
  45 + reader.start();
  46 + logger.info("*********读取线程已开启***********");
  47 + } else {
  48 + // logger.info("*********读取线程已开启-不再启动线程*********");
  49 + }
  50 +
  51 + if("Y".equals(isNeedSend)){
40 IMF_Sender kako_sender = new IMF_Sender(client); 52 IMF_Sender kako_sender = new IMF_Sender(client);
41 if(!IMF_Sender.isrunning) { 53 if(!IMF_Sender.isrunning) {
42 -// kako_sender.start(); 54 + kako_sender.start();
43 } 55 }
44 } 56 }
45 57
46 - if (!LOGIN_OK) {  
47 - loginIMF(client, loginname, loginpass, "config/imf_config.properties");  
48 - }  
49 - 58 + }
50 59
51 } 60 }
52 61
@@ -54,6 +63,7 @@ public class IMF_Task { @@ -54,6 +63,7 @@ public class IMF_Task {
54 if (!msg.equals(null) && !msg.isEmpty()){ 63 if (!msg.equals(null) && !msg.isEmpty()){
55 if(LOGIN_OK){ 64 if(LOGIN_OK){
56 IMF_Sender sender = new IMF_Sender(client,msg); 65 IMF_Sender sender = new IMF_Sender(client,msg);
  66 + sender.setContent(msg);
57 sender.start(); 67 sender.start();
58 } 68 }
59 69
@@ -20,6 +20,10 @@ public final class DateUtil { @@ -20,6 +20,10 @@ public final class DateUtil {
20 } 20 }
21 21
22 public static Date formatByyyyyMMddHHmmss(String dateStr) throws DateTimeParseException{ 22 public static Date formatByyyyyMMddHHmmss(String dateStr) throws DateTimeParseException{
  23 + //毫秒级的去掉
  24 + if(dateStr.length()>14){
  25 + dateStr= dateStr.substring(0,14);
  26 + }
23 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); 27 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
24 LocalDateTime dateTime = LocalDateTime.parse(dateStr, formatter); 28 LocalDateTime dateTime = LocalDateTime.parse(dateStr, formatter);
25 return convertLDTToDate(dateTime); 29 return convertLDTToDate(dateTime);
1 1
2 #服务端口 2 #服务端口
3 server.port=7003 3 server.port=7003
  4 +server.servlet.context-path=${SERVER_CONTEXTPATH:}
4 #eureka主机名,会在控制页面中显示 5 #eureka主机名,会在控制页面中显示
5 eureka.instance.hostname=localhost 6 eureka.instance.hostname=localhost
6 #eureka服务器页面中status的请求路径 7 #eureka服务器页面中status的请求路径
@@ -9,6 +10,10 @@ eureka.instance.status-page-url=http://localhost:7003/index @@ -9,6 +10,10 @@ eureka.instance.status-page-url=http://localhost:7003/index
9 eureka.client.service-url.defaultZone=http://10.50.3.82:19527/eureka/ 10 eureka.client.service-url.defaultZone=http://10.50.3.82:19527/eureka/
10 #服务名 11 #服务名
11 spring.application.name=imf-warehouse-reader 12 spring.application.name=imf-warehouse-reader
  13 +eureka.instance.prefer-ip-address=true
  14 +eureka.client.registry-fetch-interval-seconds=30
  15 +eureka.instance.lease-renewal-interval-in-seconds=15
  16 +eureka.instance.lease-expiration-duration-in-seconds=45
12 17
13 #spring.datasource.name=CGOETL 18 #spring.datasource.name=CGOETL
14 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource 19 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
  1 +package com.tianbo.warehouse;
  2 +
  3 +import com.tianbo.warehouse.handle.IMFSaveHandle;
  4 +
  5 +public class handleTest {
  6 + private static String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Msg> <META> <SNDR>NDLR</SNDR> <RCVR></RCVR> <SEQN>172-33519102</SEQN> <DDTM>20181229124530224</DDTM> <TYPE>HYXX</TYPE> <STYP>NDLR</STYP> </META> <DECLAREPREPAREMASTERXMLBODY> <Head> <MessageID>CN_MT2201_1P0_460470678920X_20181229124530224</MessageID> <FunctionCode>9</FunctionCode> <MessageType>MT2201MASTER</MessageType> <SenderID>460470678920X_DXPENT0000460002_20181229124530224</SenderID> <ReceiverID>4604_20181229124530224</ReceiverID> <SendTime>20181229124530224</SendTime> <Version>1.0</Version> </Head> <Declaration> <Carrier> <ID>CV</ID> </Carrier> <ORG> <ID>CGO</ID> </ORG> <DES> <ID>CDG</ID> </DES> <BorderTransportMeans> <JourneyID>9732/2019-01-01</JourneyID> </BorderTransportMeans> <Consignment> <TransportContractDocument> <ID>172-33519102</ID> </TransportContractDocument> <LoadingLocation> <ID>CGO/4604</ID> <LoadingDate>2018-12-29 12:45</LoadingDate> </LoadingLocation> <UnloadingLocation> <ID>CGO/4604</ID> </UnloadingLocation> <TransportSplitIndicator>0</TransportSplitIndicator> <ConsignmentPackaging> <QuantityQuantity>337</QuantityQuantity> </ConsignmentPackaging> <TotalGrossMassMeasure>3192</TotalGrossMassMeasure> <PreQuantityQuantity>337</PreQuantityQuantity> <PreTotalGrossMassMeasure>3192</PreTotalGrossMassMeasure> <CustomsStatus>001</CustomsStatus> <FreightPayment> <MethodCode>PP</MethodCode> </FreightPayment> <ProductName>IMITATION JEWELRY</ProductName> <PrepareTime>2018-12-29 12:45</PrepareTime> <CustomsCode>4604</CustomsCode> <AgentName>ZRP</AgentName> <AgentCompany>郑州睿鹏物流有限公司</AgentCompany> <NameOfgoods>IMITATION JEWELRY</NameOfgoods> <DeliveryStation>001</DeliveryStation> <UNnumber></UNnumber> <Category></Category> <Consignee> <Name>QUALITAIR SEA INTERNATIONAL</Name> <Address> <Line>4 RUE DU MEUNIER BP 19622 95724 ROISSY CDG</Line> <CityName>CDG</CityName> <CountryCode>FR</CountryCode> <ZipCode></ZipCode> <PROVINCECODE></PROVINCECODE> <PROVINCENAME></PROVINCENAME> <Deltaname></Deltaname> <TelePhone>33 1 34 38 58 13</TelePhone> <Fax></Fax> <CNECUSID>EUROPEAN VAT NUMBER+FR93392293635</CNECUSID> <CNEAEO></CNEAEO> <Unlodingcode>CDG</Unlodingcode> </Address> </Consignee> <Consignor> <name>ON TIME EXPRESS LIMITED</name> <Address> <Line>ROOM 1102 FINANCIAL STREET HAI LUN CENTER NO 440 HAI LUN ROAD</Line> <CountryCode>CN</CountryCode> <ZipCode></ZipCode> <Deltaname></Deltaname> <TelePhone>862163642582</TelePhone> <Fax></Fax> <SHPAEO></SHPAEO> <SHPCUSID>USCI+91310000717852489D</SHPCUSID> </Address> </Consignor> </Consignment> </Declaration> </DECLAREPREPAREMASTERXMLBODY></Msg>";
  7 +
  8 +
  9 +
  10 + public static void main(String[] args) {
  11 +
  12 + IMFSaveHandle handle = new IMFSaveHandle();
  13 + handle.init();
  14 + handle.handle(xml);
  15 +
  16 + }
  17 +
  18 +
  19 +}