作者 朱兆平

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

将回执发到队列时,进行xml格式的判定
... ... @@ -17,13 +17,13 @@ import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.util.concurrent.CountDownLatch;
/**
... ... @@ -99,25 +99,48 @@ public class SendXml2MqThread implements Runnable{
int i = 0;
String content = FileUtils.readFileToString(xmlfile,"UTF-8");
/**
* 发送消息到交换上
*/
boolean success = ExSendMsg.sendMsg(
exchangeName,
exchangeType,
routingName,
queueName,
content,
mqIp,
mqPort,
mqVhost,
mqUsername,
mqPassword
);
return success ? 1 : 0;
//校验报文格式是否是XML
if (isXmlDocument(content)){
/**
* 发送消息到交换上
*/
boolean success = ExSendMsg.sendMsg(
exchangeName,
exchangeType,
routingName,
queueName,
content,
mqIp,
mqPort,
mqVhost,
mqUsername,
mqPassword
);
return success ? 1 : 0;
}else {
log.info("[{}]报文格式未通过XML校验,报文删除",content);
return 1;
}
}
/**
* 校验字符窜是否是XML格式
* @param rtnMsg
* @return
*/
private static boolean isXmlDocument(String rtnMsg){
boolean flag = true;
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
builder.parse( new InputSource( new StringReader( rtnMsg )));
} catch (Exception e) {
flag = false;
}
return flag;
}
}
... ...
... ... @@ -62,8 +62,8 @@ public class GetResponse extends DefaultConsumer {
this.mqResponse = new MqResponse(consumerTag,envelope,properties,new String(body, StandardCharsets.UTF_8));
log.info("-----------获取到报文----------\n{}",mqResponse.getContent());
handleMessage(mqResponse.getContent());
// writeToReadDir(mqResponse.getContent());
// handleMessage(mqResponse.getContent());
writeToReadDir(mqResponse.getContent());
//写入回执目录
}
... ... @@ -71,19 +71,20 @@ public class GetResponse extends DefaultConsumer {
public void writeToReadDir(String content)throws IOException{
if(content !=null && !content.isEmpty()){
//todo:下边过去xml文件报头代码在TCS服务器上部署的时候最好去掉.这段过滤代码是为了能转发到IMF
content = content.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>","");
// content = content.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>","");
//查找报文中的messageid作为文件名
String pattern = "<MessageID>(\\w+)</MessageID>";
Pattern r = Pattern.compile(pattern);
Matcher matcher = r.matcher(content);
String fileName = this.receptDir + UUID.randomUUID().toString()+".xml";
// 新舱单部署要去掉下面更改名称代码
// if (matcher.find()){
// fileName = this.receptDir+ matcher.group(1)+".xml";
// }
log.info("-----------{}报文保存成功----------",fileName);
if (matcher.find()){
fileName = this.receptDir+ matcher.group(1)+".xml";
}
File fileToDirectory = new File(fileName);
FileUtils.writeStringToFile(fileToDirectory,content,"UTF-8");
log.info("-----------{}报文保存成功----------",fileName);
}
}
... ...
... ... @@ -50,7 +50,7 @@ public class CustomMqGetTask {
@Value("${mq.exchange.routing-key}")
private String routingKey;
@Scheduled(fixedRate = 5000)
// @Scheduled(fixedRate = 5000)
public void startTask(){
try {
... ...
... ... @@ -40,7 +40,7 @@ public class TCSMqGetTask {
@Value("${mq.queue.read-from-mq}")
private String queueName;
// @Scheduled(fixedRate = 5000)
@Scheduled(fixedRate = 5000)
public void startTask(){
... ...
... ... @@ -57,7 +57,7 @@ public class TCSMqSendTask {
*/
private final static int theadamount = 10;
// @Scheduled(fixedRate = 5000)
@Scheduled(fixedRate = 5000)
public void startTask(){
final SimpleDateFormat sdf = new SimpleDateFormat(
... ...