作者 朱兆平

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

#登录账号
loginname = KKW1
loginname = KAKO
#登录密码
loginpass = KKW1
loginpass = KAKO
#发送报文目录,相对程序目录
readDirectory = /Users/mrz/Downloads/3.CV972/1.CV报文
readDirectory = /Users/mrz/Downloads/1.woowo
#接收存储报文目录
bakDirectory = xmlFromImf
#程序入库出错备份报文目录
errBakDirectory = xmlerr
#是否需要发送报文,默认N不发,Y将发送readDirectory下的XML扩展名的报文
isNeedSend = Y
#IMF MEAT报头配置
... ...
... ... @@ -48,8 +48,20 @@ public class IMFSaveHandle {
public void handle(String xmlmessage){
try {
//有人发报文头部不按规范发
xmlmessage = xmlmessage.replace("<Msg>","<MSG>")
.replace("<msg>","<MSG>")
.replace("</Msg>","</MSG>")
.replace("</msg>","</MSG>");
Document document = DocumentHelper.parseText(xmlmessage);
T_ETL_MESSAGE message = new T_ETL_MESSAGE();
message.setSndr(XMLXPath.getSingleValueByPath(document,SNDR));
message.setRcvr(XMLXPath.getSingleValueByPath(document,RCVR));
message.setType(XMLXPath.getSingleValueByPath(document,TYPE));
message.setStyp(XMLXPath.getSingleValueByPath(document,STYP));
message.setSeqn(XMLXPath.getSingleValueByPath(document,SEQN));
String ddtm = XMLXPath.getSingleValueByPath(document,DDTM);
Date ddtmDate = DateUtil.formatByyyyyMMddHHmmss(ddtm);
message.setDdtm(ddtmDate);
... ... @@ -58,11 +70,7 @@ public class IMFSaveHandle {
message.setAppid("W");
message.setEtltim(new Date());
message.setSndr(XMLXPath.getSingleValueByPath(document,SNDR));
message.setRcvr(XMLXPath.getSingleValueByPath(document,RCVR));
message.setType(XMLXPath.getSingleValueByPath(document,TYPE));
message.setStyp(XMLXPath.getSingleValueByPath(document,STYP));
message.setSeqn(XMLXPath.getSingleValueByPath(document,SEQN));
message.setContent(xmlmessage);
... ...
... ... @@ -34,7 +34,7 @@ public class IMF_Reader extends Thread{
}
try {
Thread.sleep(30000L);
Thread.sleep(500L);
} catch (InterruptedException var3) {
var3.printStackTrace();
... ...
... ... @@ -20,6 +20,7 @@ public class IMF_Sender extends Thread{
public static boolean isrunning;
private IMFClient client;
private String content;
public IMF_Sender(IMFClient client) {
this.client = client;
}
... ... @@ -80,4 +81,12 @@ public class IMF_Sender extends Thread{
isrunning=false;
logger.info("<<<<<<<<<<<发送线程结束>>>>>>>>>>>>");
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
... ...
... ... @@ -19,34 +19,43 @@ public class IMF_Task {
public static boolean LOGIN_OK = false;
public static boolean isSuc = true;
public static IMFClient client = null;
public static String loginname;
public static String loginpass;
public static String isNeedSend = "N";
@Scheduled(fixedRate = 5000)
private static void start() throws Exception {
PropertyConfigurator.configure("config/log4j.properties");
client = IMFClientFactory.createInstance();
String loginname = FileTool.readProperties("loginname");
String loginpass = FileTool.readProperties("loginpass");
loginname= FileTool.readProperties("loginname");
loginpass= FileTool.readProperties("loginpass");
isNeedSend= FileTool.readProperties("isNeedSend");
if (client != null) {
IMF_Reader reader = new IMF_Reader(client);
if (!IMF_Reader.isrunning) {
reader.start();
logger.info("*********KAKO读取线程已开启***********");
} else {
// logger.info("*********KAKO读取线程已开启-不再启动线程*********");
}
//登录
if (!LOGIN_OK) {
loginIMF(client, loginname, loginpass, "config/imf_config.properties");
}
//启动读取线程
if (client != null) {
IMF_Reader reader = new IMF_Reader(client);
if (!IMF_Reader.isrunning) {
reader.start();
logger.info("*********读取线程已开启***********");
} else {
// logger.info("*********读取线程已开启-不再启动线程*********");
}
if("Y".equals(isNeedSend)){
IMF_Sender kako_sender = new IMF_Sender(client);
if(!IMF_Sender.isrunning) {
// kako_sender.start();
kako_sender.start();
}
}
if (!LOGIN_OK) {
loginIMF(client, loginname, loginpass, "config/imf_config.properties");
}
}
}
... ... @@ -54,6 +63,7 @@ public class IMF_Task {
if (!msg.equals(null) && !msg.isEmpty()){
if(LOGIN_OK){
IMF_Sender sender = new IMF_Sender(client,msg);
sender.setContent(msg);
sender.start();
}
... ...
... ... @@ -20,6 +20,10 @@ public final class DateUtil {
}
public static Date formatByyyyyMMddHHmmss(String dateStr) throws DateTimeParseException{
//毫秒级的去掉
if(dateStr.length()>14){
dateStr= dateStr.substring(0,14);
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
LocalDateTime dateTime = LocalDateTime.parse(dateStr, formatter);
return convertLDTToDate(dateTime);
... ...
#服务端口
server.port=7003
server.servlet.context-path=${SERVER_CONTEXTPATH:}
#eureka主机名,会在控制页面中显示
eureka.instance.hostname=localhost
#eureka服务器页面中status的请求路径
... ... @@ -9,6 +10,10 @@ eureka.instance.status-page-url=http://localhost:7003/index
eureka.client.service-url.defaultZone=http://10.50.3.82:19527/eureka/
#服务名
spring.application.name=imf-warehouse-reader
eureka.instance.prefer-ip-address=true
eureka.client.registry-fetch-interval-seconds=30
eureka.instance.lease-renewal-interval-in-seconds=15
eureka.instance.lease-expiration-duration-in-seconds=45
#spring.datasource.name=CGOETL
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
... ...
package com.tianbo.warehouse;
import com.tianbo.warehouse.handle.IMFSaveHandle;
public class handleTest {
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>";
public static void main(String[] args) {
IMFSaveHandle handle = new IMFSaveHandle();
handle.init();
handle.handle(xml);
}
}
... ...