GetResponse.java
3.7 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
package com.tianbo.analysis.rabbitmq;
import com.rabbitmq.client.*;
import com.tianbo.analysis.bean.SpringBeanUtitl;
import com.tianbo.analysis.handle.CustomHandleThead;
import com.tianbo.analysis.service.CustomAnalysisService;
import com.tianbo.analysis.task.XMLThreadPoolFactory;
import com.tianbo.util.RabitMq.MqResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.regex.*;
/**
* 获取MQ队列信息并保存到本地存储文件夹
*/
@Slf4j
public class GetResponse extends DefaultConsumer {
private MqResponse mqResponse;
private String receptDir;
private static GetResponse getResponse;
/**
* 初始化线程池
*/
private static ThreadPoolExecutor threadPool = XMLThreadPoolFactory.instance();
public GetResponse(Channel channel) {
super(channel);
}
public GetResponse(Channel channel,String dir) {
super(channel);
this.receptDir = dir;
}
/**
* 处理回来的信息
* @param consumerTag
* @param envelope
* @param properties
* @param body
* @throws IOException
*/
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
// System.err.println("-----------consume message----------");
// System.err.println("consumerTag: " + consumerTag);
// System.err.println("envelope: " + envelope);
// System.err.println("properties: " + properties);
// System.err.println("body: " + new String(body));
this.mqResponse = new MqResponse(consumerTag,envelope,properties,new String(body, StandardCharsets.UTF_8));
log.info("-----------获取到报文----------\n{}",mqResponse.getContent());
handleMessage(mqResponse.getContent());
// writeToReadDir(mqResponse.getContent());
//写入回执目录
}
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\"?>","");
//查找报文中的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);
File fileToDirectory = new File(fileName);
FileUtils.writeStringToFile(fileToDirectory,content,"UTF-8");
}
}
private void handleMessage(String message){
CustomHandleThead customResponseHandleThread = new CustomHandleThead();
customResponseHandleThread.setMsg(message);
try{
threadPool.execute(customResponseHandleThread);
}catch (RejectedExecutionException e){
e.printStackTrace();
log.error("线程池已满");
}catch (Exception e){
e.printStackTrace();
log.error("线程池异常");
}
}
}