|
|
package com.tianbo.analysis.handle;
|
|
|
|
|
|
import com.rabbitmq.client.Channel;
|
|
|
import com.rabbitmq.client.Envelope;
|
|
|
import com.tianbo.analysis.service.CustomAnalysisService;
|
|
|
import com.tianbo.util.Date.DateUtil;
|
|
|
import lombok.Data;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.io.FileExistsException;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.xml.sax.InputSource;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.io.StringReader;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
/**
|
|
|
* 报文分发到舱单回执解析服务线程
|
|
|
* 分发前判定报文是否为正常合规的XML报文
|
|
|
* 合规的过滤掉XML的头部信息(为了能将原报文内容转发到IMF),
|
|
|
* 并备份一份到bakupDir目录,并转发一份到新舱单转发回执到IMF发送端监听目录下
|
|
|
* 不合规的备份到errBakDir目录
|
|
|
*/
|
|
|
|
|
|
@Component
|
|
|
@Data
|
|
|
@Slf4j
|
|
|
public class CustomHandleThead implements Runnable{
|
|
|
|
|
|
private String msg;
|
|
|
|
|
|
private Channel ackChannel;
|
|
|
|
|
|
private Envelope envelope;
|
|
|
|
|
|
//备份目录
|
|
|
@Value("${custom.receptBakDir}")
|
|
|
private String bakupDir;
|
|
|
|
|
|
@Value("${custom.errBakDir}")
|
|
|
private String errBakDir;
|
|
|
|
|
|
//回执转发目录
|
|
|
@Value("${custom.transmitDir}")
|
|
|
private String transmitDir;
|
|
|
|
|
|
private static CustomHandleThead customHandleThead;
|
|
|
|
|
|
@Autowired
|
|
|
CustomAnalysisService customAnalysisService;
|
|
|
|
|
|
public CustomHandleThead(){
|
|
|
|
|
|
}
|
|
|
|
|
|
public CustomHandleThead(String msg) {
|
|
|
this.msg = msg;
|
|
|
}
|
|
|
|
|
|
public CustomHandleThead(String msg, Channel ackChannel,Envelope envelope) {
|
|
|
this.msg = msg;
|
|
|
this.ackChannel = ackChannel;
|
|
|
this.envelope = envelope;
|
|
|
}
|
|
|
|
|
|
@PostConstruct
|
|
|
public void init() {
|
|
|
customHandleThead = this;
|
|
|
|
|
|
customHandleThead.customAnalysisService = this.customAnalysisService;
|
|
|
customHandleThead.bakupDir = this.bakupDir;
|
|
|
customHandleThead.errBakDir = this.errBakDir;
|
|
|
customHandleThead.transmitDir = this.transmitDir;
|
|
|
|
|
|
|
|
|
// 初使化时将已静态化的testService实例化
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void run() {
|
|
|
try {
|
|
|
if (StringUtils.isNotBlank(msg) && isXmlDocument(msg)){
|
|
|
//过滤报文头部
|
|
|
msg = msg.replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>","");
|
|
|
|
|
|
boolean result = customHandleThead.customAnalysisService.analysis(msg);
|
|
|
if (result){
|
|
|
ack();
|
|
|
}else {
|
|
|
ack();
|
|
|
}
|
|
|
}else {
|
|
|
//错误报文直接消费 不处理;并备份
|
|
|
ack();
|
|
|
}
|
|
|
}catch (Exception e){
|
|
|
nack();
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private void ack(){
|
|
|
try {
|
|
|
ackChannel.basicAck(envelope.getDeliveryTag(),false);
|
|
|
log.warn("tag:{}已消费",envelope.getDeliveryTag());
|
|
|
}catch (IOException e){
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void nack(){
|
|
|
try {
|
|
|
ackChannel.basicNack(envelope.getDeliveryTag(), false, true);
|
|
|
}catch (IOException e){
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
public static void bakFile(String content,String filePath){
|
|
|
try {
|
|
|
String fileName = customHandleThead.bakupDir + UUID.randomUUID().toString()+".xml";
|
|
|
log.info("-----------{}报文保存成功----------",fileName);
|
|
|
File fileToDirectory = new File(fileName);
|
|
|
FileUtils.writeStringToFile(fileToDirectory,content,"UTF-8");
|
|
|
}catch (IOException e){
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
} |
...
|
...
|
|