作者 朱兆平

发送报文时采用报文中的messageid生成报文名称,

将回执发到队列时,进行xml格式的判定
@@ -17,13 +17,13 @@ import org.dom4j.Element; @@ -17,13 +17,13 @@ import org.dom4j.Element;
17 import org.dom4j.io.SAXReader; 17 import org.dom4j.io.SAXReader;
18 import org.springframework.beans.factory.annotation.Value; 18 import org.springframework.beans.factory.annotation.Value;
19 import org.springframework.stereotype.Component; 19 import org.springframework.stereotype.Component;
  20 +import org.xml.sax.InputSource;
20 import org.xml.sax.SAXParseException; 21 import org.xml.sax.SAXParseException;
21 22
22 import javax.annotation.PostConstruct; 23 import javax.annotation.PostConstruct;
23 -import java.io.File;  
24 -import java.io.FileNotFoundException;  
25 -import java.io.IOException;  
26 -import java.io.UnsupportedEncodingException; 24 +import javax.xml.parsers.DocumentBuilder;
  25 +import javax.xml.parsers.DocumentBuilderFactory;
  26 +import java.io.*;
27 import java.util.concurrent.CountDownLatch; 27 import java.util.concurrent.CountDownLatch;
28 28
29 /** 29 /**
@@ -99,25 +99,48 @@ public class SendXml2MqThread implements Runnable{ @@ -99,25 +99,48 @@ public class SendXml2MqThread implements Runnable{
99 99
100 int i = 0; 100 int i = 0;
101 String content = FileUtils.readFileToString(xmlfile,"UTF-8"); 101 String content = FileUtils.readFileToString(xmlfile,"UTF-8");
102 - /**  
103 - * 发送消息到交换上  
104 - */  
105 - boolean success = ExSendMsg.sendMsg(  
106 - exchangeName,  
107 - exchangeType,  
108 - routingName,  
109 - queueName,  
110 - content,  
111 - mqIp,  
112 - mqPort,  
113 - mqVhost,  
114 - mqUsername,  
115 - mqPassword  
116 - );  
117 -  
118 - return success ? 1 : 0; 102 + //校验报文格式是否是XML
  103 + if (isXmlDocument(content)){
  104 + /**
  105 + * 发送消息到交换上
  106 + */
  107 + boolean success = ExSendMsg.sendMsg(
  108 + exchangeName,
  109 + exchangeType,
  110 + routingName,
  111 + queueName,
  112 + content,
  113 + mqIp,
  114 + mqPort,
  115 + mqVhost,
  116 + mqUsername,
  117 + mqPassword
  118 + );
  119 +
  120 + return success ? 1 : 0;
  121 + }else {
  122 + log.info("[{}]报文格式未通过XML校验,报文删除",content);
  123 + return 1;
  124 + }
119 125
120 } 126 }
121 127
  128 + /**
  129 + * 校验字符窜是否是XML格式
  130 + * @param rtnMsg
  131 + * @return
  132 + */
  133 + private static boolean isXmlDocument(String rtnMsg){
  134 +
  135 + boolean flag = true;
  136 + try {
  137 + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  138 + DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
  139 + builder.parse( new InputSource( new StringReader( rtnMsg )));
  140 + } catch (Exception e) {
  141 + flag = false;
  142 + }
  143 + return flag;
  144 + }
122 145
123 } 146 }
@@ -62,8 +62,8 @@ public class GetResponse extends DefaultConsumer { @@ -62,8 +62,8 @@ public class GetResponse extends DefaultConsumer {
62 this.mqResponse = new MqResponse(consumerTag,envelope,properties,new String(body, StandardCharsets.UTF_8)); 62 this.mqResponse = new MqResponse(consumerTag,envelope,properties,new String(body, StandardCharsets.UTF_8));
63 log.info("-----------获取到报文----------\n{}",mqResponse.getContent()); 63 log.info("-----------获取到报文----------\n{}",mqResponse.getContent());
64 64
65 - handleMessage(mqResponse.getContent());  
66 -// writeToReadDir(mqResponse.getContent()); 65 +// handleMessage(mqResponse.getContent());
  66 + writeToReadDir(mqResponse.getContent());
67 67
68 //写入回执目录 68 //写入回执目录
69 } 69 }
@@ -71,19 +71,20 @@ public class GetResponse extends DefaultConsumer { @@ -71,19 +71,20 @@ public class GetResponse extends DefaultConsumer {
71 public void writeToReadDir(String content)throws IOException{ 71 public void writeToReadDir(String content)throws IOException{
72 if(content !=null && !content.isEmpty()){ 72 if(content !=null && !content.isEmpty()){
73 //todo:下边过去xml文件报头代码在TCS服务器上部署的时候最好去掉.这段过滤代码是为了能转发到IMF 73 //todo:下边过去xml文件报头代码在TCS服务器上部署的时候最好去掉.这段过滤代码是为了能转发到IMF
74 - content = content.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>",""); 74 +// content = content.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>","");
75 //查找报文中的messageid作为文件名 75 //查找报文中的messageid作为文件名
76 String pattern = "<MessageID>(\\w+)</MessageID>"; 76 String pattern = "<MessageID>(\\w+)</MessageID>";
77 Pattern r = Pattern.compile(pattern); 77 Pattern r = Pattern.compile(pattern);
78 Matcher matcher = r.matcher(content); 78 Matcher matcher = r.matcher(content);
79 String fileName = this.receptDir + UUID.randomUUID().toString()+".xml"; 79 String fileName = this.receptDir + UUID.randomUUID().toString()+".xml";
80 // 新舱单部署要去掉下面更改名称代码 80 // 新舱单部署要去掉下面更改名称代码
81 -// if (matcher.find()){  
82 -// fileName = this.receptDir+ matcher.group(1)+".xml";  
83 -// }  
84 - log.info("-----------{}报文保存成功----------",fileName); 81 + if (matcher.find()){
  82 + fileName = this.receptDir+ matcher.group(1)+".xml";
  83 + }
  84 +
85 File fileToDirectory = new File(fileName); 85 File fileToDirectory = new File(fileName);
86 FileUtils.writeStringToFile(fileToDirectory,content,"UTF-8"); 86 FileUtils.writeStringToFile(fileToDirectory,content,"UTF-8");
  87 + log.info("-----------{}报文保存成功----------",fileName);
87 } 88 }
88 89
89 } 90 }
@@ -50,7 +50,7 @@ public class CustomMqGetTask { @@ -50,7 +50,7 @@ public class CustomMqGetTask {
50 @Value("${mq.exchange.routing-key}") 50 @Value("${mq.exchange.routing-key}")
51 private String routingKey; 51 private String routingKey;
52 52
53 - @Scheduled(fixedRate = 5000) 53 +// @Scheduled(fixedRate = 5000)
54 public void startTask(){ 54 public void startTask(){
55 55
56 try { 56 try {
@@ -40,7 +40,7 @@ public class TCSMqGetTask { @@ -40,7 +40,7 @@ public class TCSMqGetTask {
40 @Value("${mq.queue.read-from-mq}") 40 @Value("${mq.queue.read-from-mq}")
41 private String queueName; 41 private String queueName;
42 42
43 -// @Scheduled(fixedRate = 5000) 43 + @Scheduled(fixedRate = 5000)
44 public void startTask(){ 44 public void startTask(){
45 45
46 46
@@ -57,7 +57,7 @@ public class TCSMqSendTask { @@ -57,7 +57,7 @@ public class TCSMqSendTask {
57 */ 57 */
58 private final static int theadamount = 10; 58 private final static int theadamount = 10;
59 59
60 -// @Scheduled(fixedRate = 5000) 60 + @Scheduled(fixedRate = 5000)
61 public void startTask(){ 61 public void startTask(){
62 62
63 final SimpleDateFormat sdf = new SimpleDateFormat( 63 final SimpleDateFormat sdf = new SimpleDateFormat(