CustomHandleThead.java
4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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){
}
}
}