作者 朱兆平

增加直接从mq获取报文并解析的处理方案

@@ -49,10 +49,11 @@ spring: @@ -49,10 +49,11 @@ spring:
49 type: com.alibaba.druid.pool.DruidDataSource 49 type: com.alibaba.druid.pool.DruidDataSource
50 #oracle 50 #oracle
51 driver-class-name: oracle.jdbc.OracleDriver 51 driver-class-name: oracle.jdbc.OracleDriver
52 - url: jdbc:oracle:thin:@10.50.3.69:1521:CGODB  
53 -# url: jdbc:oracle:thin:@192.168.1.253:1522:ORCLL 52 +# url: jdbc:oracle:thin:@10.50.3.69:1521:CGODB
  53 + url: jdbc:oracle:thin:@192.168.1.253:1522:ORCLL
54 username: CGONMS 54 username: CGONMS
55 - password: 1q2w3e4r 55 +# password: 1q2w3e4r
  56 + password: vmvnv1v2
56 #spring datasource mysql,注意编码配置,缺少数据库编码配置容易引起中文入库乱码 57 #spring datasource mysql,注意编码配置,缺少数据库编码配置容易引起中文入库乱码
57 # url: jdbc:mysql://127.0.0.1:3307/statistics?useUnicode=true&characterEncoding=utf8 58 # url: jdbc:mysql://127.0.0.1:3307/statistics?useUnicode=true&characterEncoding=utf8
58 # username: root 59 # username: root
@@ -113,7 +114,7 @@ pagehelper: @@ -113,7 +114,7 @@ pagehelper:
113 114
114 #debug配置,debug或者为true的时候,logback才会记录和写入日志文件 115 #debug配置,debug或者为true的时候,logback才会记录和写入日志文件
115 trace: false 116 trace: false
116 -debug: false 117 +debug: true
117 118
118 logging: 119 logging:
119 file: 120 file:
@@ -151,4 +152,36 @@ devops: @@ -151,4 +152,36 @@ devops:
151 singlewindow-tcs-recept: D:\TCSSingleWindow\recive 152 singlewindow-tcs-recept: D:\TCSSingleWindow\recive
152 tianbo-tcs-recept: D:\Data\Receive 153 tianbo-tcs-recept: D:\Data\Receive
153 cfps-subscribe-dir: D:\系统部署\imf_Warehouse_reader\xmlFromImf 154 cfps-subscribe-dir: D:\系统部署\imf_Warehouse_reader\xmlFromImf
154 -#10079  
  155 +mq:
  156 + # 从新舱单服务器读取申报业务报文发送到业务报文发送队列 {nmmsXml-mq};从回执队列{tcs-mq}读取报文保存到本地服务器回执报文接收目录
  157 + # 从tcs服务器读取海关回执报文发送到回执报文发送队列 {tcs-mq};从新舱单报文申报队列{nmmsXml-mq}读取报文保存到本地服务器申报报文发送目录
  158 + dir:
  159 + #报文读取目录,读取本地文件发送到MQ [目录结尾要带/]
  160 + sendXml-dir: /Users/mrz/Downloads/rdp_temp/logs/回执报文样例/
  161 + #保存从mq取到的回执报文到本地回执目录 [目录结尾要带/]
  162 + saveXml-dir: /Users/mrz/Downloads/rdp_temp/logs/transmit/
  163 + queue:
  164 + #发送mq队列名称
  165 + send-to-mq: send
  166 + #读取mq队列名称
  167 + read-from-mq: response.agent
  168 + exchange:
  169 + #TCS回执发送到此交换,交换再把回执消息同步到其他配置的回执订阅队列
  170 + #交换名称
  171 + name: cus.rcv.broadcast
  172 + #交换类型
  173 + type: fanout
  174 + #routing key名称,此处为空,所有绑定交换的队列都被广播
  175 + routing-key:
  176 +
  177 + connection:
  178 + ip: 218.28.199.134
  179 + port: 8004
  180 + vHost: NMMS
  181 + username: tianbo
  182 + password: vmvnv1v2VV
  183 +# ip: 192.168.1.63
  184 +# port: 5672
  185 +# vHost: NMMS
  186 +# username: mrz
  187 +# password: vmvnv1v2
@@ -23,7 +23,6 @@ import java.time.Duration; @@ -23,7 +23,6 @@ import java.time.Duration;
23 @EnableDiscoveryClient 23 @EnableDiscoveryClient
24 @EnableTransactionManagement 24 @EnableTransactionManagement
25 @MapperScan("com.tianbo.analysis.dao") 25 @MapperScan("com.tianbo.analysis.dao")
26 -@ComponentScan({"com.tianbo.analysis"})  
27 public class BootApplication { 26 public class BootApplication {
28 27
29 public static void main(String[] args) { 28 public static void main(String[] args) {
  1 +package com.tianbo.analysis.handle;
  2 +
  3 +import com.tianbo.analysis.model.*;
  4 +import com.tianbo.analysis.tools.AWBTools;
  5 +import com.tianbo.util.Date.DateUtil;
  6 +import com.tianbo.util.XML.XMLXPath;
  7 +import lombok.Data;
  8 +import lombok.extern.slf4j.Slf4j;
  9 +import org.apache.commons.io.FileExistsException;
  10 +import org.apache.commons.io.FileUtils;
  11 +import org.apache.commons.lang.StringUtils;
  12 +import org.dom4j.Document;
  13 +import org.dom4j.DocumentException;
  14 +import org.dom4j.Element;
  15 +import org.dom4j.io.SAXReader;
  16 +import org.springframework.beans.factory.annotation.Value;
  17 +import org.springframework.stereotype.Component;
  18 +import org.xml.sax.SAXParseException;
  19 +
  20 +import javax.annotation.PostConstruct;
  21 +import java.io.ByteArrayInputStream;
  22 +import java.io.File;
  23 +import java.io.FileNotFoundException;
  24 +import java.io.UnsupportedEncodingException;
  25 +import java.util.UUID;
  26 +
  27 +/**
  28 + * @author mrz
  29 + * @e
  30 + * 海关新舱单回执解析xml文件
  31 + */
  32 +@Data
  33 +@Component
  34 +@Slf4j
  35 +public class CustomResponseHandleThread implements Runnable{
  36 +
  37 + //解析出错转移目录
  38 + @Value("${custom.errBakDir}")
  39 + private String errBakDir;
  40 +
  41 + @Value("${custom.receptBakDir}")
  42 + private String receptBakDir;
  43 +
  44 + private String responseMessage;
  45 +
  46 + private static CustomResponseHandleThread customXmlHandle;
  47 +
  48 + /**
  49 + * 单一窗口回执的技术回执
  50 + */
  51 + public final static String DONT_NEED = "<Code>0</Code><Text>接收成功</Text>";
  52 +
  53 + //海关新舱单回执报头
  54 + public final static String MessageID = "//Manifest/Head/MessageID";
  55 + public final static String FunctionCode = "//Manifest/Head/FunctionCode";
  56 + public final static String MessageType = "//Manifest/Head/MessageType";
  57 + public final static String SenderID = "//Manifest/Head/SenderID";
  58 + public final static String ReceiverID = "//Manifest/Head/ReceiverID";
  59 + public final static String SendTime = "//Manifest/Head/SendTime";
  60 + public final static String Version = "//Manifest/Head/Version";
  61 +
  62 +
  63 + /**
  64 + * 海关普通业务新舱单回执报体
  65 + */
  66 + public final static String JourneyID = "//Manifest/Response/BorderTransportMeans/JourneyID";
  67 + public final static String WaybillMaster = "//Manifest/Response/Consignment/TransportContractDocument/ID";
  68 + public final static String WaybillSecond = "//Manifest/Response/Consignment/AssociatedTransportDocument/ID";
  69 + public final static String ResponseCode = "//ResponseType/Code";
  70 + public final static String ResponseText = "//ResponseType/Text";
  71 +
  72 + /**
  73 + * 海关国际转运业务回执报体
  74 + */
  75 + public final static String ImportJourneyID = "//Manifest/Response/ImportInformation/BorderTransportMeans/JourneyID";
  76 + public final static String ImportWaybillMaster = "//Manifest/Response/ImportInformation/Consignment/TransportContractDocument/ID";
  77 + public final static String ExportJourneyID = "//Manifest/Response/ExportInformation/BorderTransportMeans/JourneyID";
  78 + public final static String ExportWaybillMaster = "//Manifest/Response/ExportInformation/Consignment/TransportContractDocument/ID";
  79 +// public final static String TransResponseCode = "//Manifest/Response/ResponseType/Code";
  80 +// public final static String TransResponseText = "//Manifest/Response/ResponseType/Text";
  81 + public final static String TransResponseCode = "//ResponseType/Code";
  82 + public final static String TransResponseText = "//ResponseType/Text";
  83 +
  84 +
  85 + //通过@PostConstruct实现初始化bean之前进行的操作,解决service调用空指针问题
  86 + @PostConstruct
  87 + public void init() {
  88 + customXmlHandle = this;
  89 +
  90 + customXmlHandle.errBakDir = this.errBakDir;
  91 + customXmlHandle.receptBakDir = this.receptBakDir;
  92 +
  93 + // 初使化时将已静态化的testService实例化
  94 + }
  95 +
  96 + @Override
  97 + public void run() {
  98 + log.info("线程开始");
  99 + String today = DateUtil.getTodayBy_yyyyMMdd();
  100 + String errDirByDay = customXmlHandle.errBakDir + "/" + today;
  101 + String successDir = customXmlHandle.receptBakDir + "/" + today;
  102 + if(responseMessage.contains(DONT_NEED)){
  103 + log.warn("{}报文为技术回执,剪切到错误备份目录,不解析",responseMessage);
  104 +
  105 + errBak(responseMessage,errDirByDay);
  106 + log.info("报文为技术回执备份成功..开始解析下一文件,当前线程即将结束");
  107 + }
  108 + else
  109 + {
  110 + try{
  111 +
  112 + int i =handelXmlDocument();
  113 + //操作成功,则转移剪切解析文件到备份目录,否则转移到error目录备份
  114 + if(i>0){
  115 + errBak(responseMessage,successDir);
  116 + log.info("报文解析入库成功");
  117 + }else {
  118 + errBak(responseMessage,errDirByDay);
  119 + }
  120 + }catch (Exception e){
  121 + log.error("报文备份异常",e);
  122 + errBak(responseMessage,errDirByDay);
  123 + e.printStackTrace();
  124 + }
  125 +
  126 + }
  127 + log.info("线程结束");
  128 + }
  129 + /**
  130 + *
  131 + *
  132 + * @return
  133 + * @throws DocumentException
  134 + * @throws UnsupportedEncodingException
  135 + * @throws SAXParseException
  136 + * @throws FileNotFoundException
  137 + */
  138 + private int handelXmlDocument() throws DocumentException,UnsupportedEncodingException,SAXParseException,FileNotFoundException {
  139 +
  140 + int i = 0;
  141 + String secondSplit = "_";
  142 + SAXReader saxReader = new SAXReader();
  143 + Document document = saxReader.read(new ByteArrayInputStream(responseMessage.getBytes("UTF-8")));
  144 + Element contentRoot = document.getRootElement();
  145 +
  146 + String flightNo = "UNKONW";
  147 + String flightDate = "20101010";
  148 +
  149 + //开始解析
  150 + String msgType = XMLXPath.getSingleValueByPath(document,MessageType);
  151 + String journeyid = XMLXPath.getSingleValueByPath(document,JourneyID);
  152 +// XMLXPath.getSingleValueByPath(document, )
  153 +
  154 +
  155 + String awbA = XMLXPath.getSingleValueByPath(document,WaybillMaster);
  156 + //全格式的分单 如 17212345678_ADBD
  157 + String awbH = XMLXPath.getSingleValueByPath(document,WaybillSecond);
  158 + String resCode = XMLXPath.getSingleValueByPath(document,ResponseCode);
  159 + String resText = XMLXPath.getSingleValueByPath(document,ResponseText);
  160 + String messageId = XMLXPath.getSingleValueByPath(document, MessageID);
  161 + String sendTime = XMLXPath.getSingleValueByPath(document,SendTime);
  162 + String sendId = XMLXPath.getSingleValueByPath(document,SenderID);
  163 + String reciveId = XMLXPath.getSingleValueByPath(document,ReceiverID);
  164 + String version = XMLXPath.getSingleValueByPath(document,Version);
  165 + String functionCode = XMLXPath.getSingleValueByPath(document,FunctionCode);
  166 +
  167 +
  168 +
  169 + CustomReception customReception = new CustomReception( msgType,
  170 + flightNo,
  171 + flightDate,
  172 + awbA,
  173 + awbH,
  174 + resCode,
  175 + resText,
  176 + messageId,
  177 + sendTime,
  178 + sendId,
  179 + reciveId,
  180 + version,
  181 + functionCode);
  182 +
  183 + /**
  184 + * 如果回执中没有携带航班信息节点,说明是出错报文
  185 + * 到发送日志表根据messageid 找到相应的发送日志报文的航班及运单信息,再进行解析
  186 + */
  187 + if(!StringUtils.isEmpty(journeyid)){
  188 +
  189 + String[] flightList = journeyid.split("/");
  190 + if(flightList.length > 0){
  191 + flightNo = flightList[0];
  192 + flightDate = flightList[1];
  193 + customReception.setFlightNo(flightNo);
  194 + customReception.setFlightDate(flightDate);
  195 + }
  196 + }else {
  197 + CUSTOMSMESSAGE customsmessage = new CUSTOMSMESSAGE();
  198 + customReception = customsmessage.getWaybillInfoByCutomResponse(customReception);
  199 + }
  200 +
  201 + switch (msgType){
  202 + case "MT9999":
  203 + if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
  204 + PREPARESECONDARY preparesecondary = new PREPARESECONDARY(customReception);
  205 + int pre_i = preparesecondary.secondAnalysisReception();
  206 +
  207 + if (pre_i>0){
  208 + i=1;
  209 + }else {
  210 + ARRIVEDSECONDARY arrivedsecondary = new ARRIVEDSECONDARY(customReception);
  211 + int arr_i=arrivedsecondary.secondAnalysisReception();
  212 +
  213 + if (arr_i>0){
  214 + i= 1;
  215 + }else {
  216 + Originmanifestsecondary originmanifestsecondary = new Originmanifestsecondary(customReception);
  217 + int org_i=originmanifestsecondary.secondAnalysisReception();
  218 + if (org_i>0){
  219 + i=1;
  220 + }
  221 + }
  222 + }
  223 + }else{
  224 + ORIGINMANIFESTMASTER originmanifestmaster = new ORIGINMANIFESTMASTER(customReception);
  225 + PREPAREMASTER preparemaster= new PREPAREMASTER(customReception);
  226 + ARRIVEDMASTER arrivedmaster9999 = new ARRIVEDMASTER(customReception);
  227 + if(originmanifestmaster.masterAnalysisReception()>0){
  228 + i=1;
  229 + }else if(preparemaster.masterAnalysisReception()>0){
  230 + i=1;
  231 + }else {
  232 + i=arrivedmaster9999.masterAnalysisReception();
  233 + }
  234 + }
  235 + AgentXmlHandle agentXmlHandle=new AgentXmlHandle();
  236 + agentXmlHandle.Http_resolver(customReception);
  237 + break;
  238 + case "MT3201":
  239 + if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
  240 + ARRIVEDSECONDARY arrivedsecondary = new ARRIVEDSECONDARY(customReception);
  241 + i=arrivedsecondary.secondAnalysisReception();
  242 + }else {
  243 + ARRIVEDMASTER arrivedmaster = new ARRIVEDMASTER(customReception);
  244 + i=arrivedmaster.masterAnalysisReception();
  245 + }
  246 + break;
  247 + case "MT5202":
  248 + if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
  249 + TALLYSECONDARY tallysecondary = new TALLYSECONDARY(customReception);
  250 + i=tallysecondary.secondAnalysisReception();
  251 + }else {
  252 + TALLYMASTER tallymaster= new TALLYMASTER(customReception);
  253 + i = tallymaster.masterAnalysisReception();
  254 + }
  255 + break;
  256 + case "MT5201":
  257 + if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
  258 + TALLYSECONDARY tallysecondary = new TALLYSECONDARY(customReception);
  259 + i=tallysecondary.secondAnalysisReception();
  260 + }else {
  261 + TALLYMASTER tallymaster= new TALLYMASTER(customReception);
  262 + i = tallymaster.masterAnalysisReception();
  263 + }
  264 + break;
  265 + case "MT4201":
  266 + DEPARTURESLOADING departuresloading = new DEPARTURESLOADING(customReception);
  267 + i=departuresloading.masterAnalysisReception();
  268 + break;
  269 + case "MT1201":
  270 + if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
  271 + Originmanifestsecondary originmanifestsecondary = new Originmanifestsecondary(customReception);
  272 + i=originmanifestsecondary.secondAnalysisReception();
  273 + }else {
  274 + ORIGINMANIFESTMASTER originmanifestmaster1201 = new ORIGINMANIFESTMASTER(customReception);
  275 + i = originmanifestmaster1201.masterAnalysisReception();
  276 + }
  277 + break;
  278 + case "MT2201":
  279 + if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
  280 + PREPARESECONDARY preparesecondary = new PREPARESECONDARY(customReception);
  281 + i = preparesecondary.secondAnalysisReception();
  282 + }else {
  283 + PREPAREMASTER preparemaster1201= new PREPAREMASTER(customReception);
  284 + i = preparemaster1201.masterAnalysisReception();
  285 + }
  286 + break;
  287 + case "MT6202":
  288 + INPORTALLOCATE inportallocate = new INPORTALLOCATE(customReception);
  289 + i = inportallocate.masterAnalysisReception();
  290 + break;
  291 + case "MT3202":
  292 + ALLOCATEARRIVAL allocatearrival = new ALLOCATEARRIVAL(customReception);
  293 + i= allocatearrival.masterAnalysisReception();
  294 + break;
  295 + case "MT8205":
  296 + i = transXmlHandel(document,customReception);
  297 + break;
  298 + case "MT8202":
  299 + MANIFESTLOAD manifestload = new MANIFESTLOAD(customReception);
  300 + i= manifestload.secondAnalysisReception();
  301 + break;
  302 + case "MT8203":
  303 + MANIFESTLOSTCHANGE manifestlostchange = new MANIFESTLOSTCHANGE(customReception);
  304 + i= manifestlostchange.secondAnalysisReception();
  305 + break;
  306 + default:
  307 + break;
  308 +
  309 + }
  310 +
  311 + updateCustomMessage(customReception);
  312 + return i;
  313 +
  314 + }
  315 +
  316 + /**
  317 + * 国际转运回执处理,特殊业务
  318 + * @param document
  319 + * @param customReception
  320 + * @return
  321 + */
  322 + private int transXmlHandel(Document document,CustomReception customReception){
  323 +
  324 + String importJourneyID = XMLXPath.getSingleValueByPath(document,ImportJourneyID);
  325 + String importFlightNo = "UNKONW";
  326 + String importFlightDate = "20101010";
  327 + if(!StringUtils.isEmpty(importJourneyID)){
  328 + importFlightNo = AWBTools.splitFlightAndDate(importJourneyID)[0];
  329 + importFlightDate = AWBTools.splitFlightAndDate(importJourneyID)[1];
  330 + }
  331 +
  332 +
  333 + String importWaybillMaster= XMLXPath.getSingleValueByPath(document,ImportWaybillMaster);
  334 + String transResponseCode=XMLXPath.getSingleValueByPath(document,TransResponseCode);
  335 + String transResponseText=XMLXPath.getSingleValueByPath(document,TransResponseText);
  336 + CustomReception transCustomReception = new CustomReception( customReception.getMessageType(),
  337 + importFlightNo,
  338 + importFlightDate,
  339 + importWaybillMaster,
  340 + transResponseCode,
  341 + transResponseText,
  342 + customReception.getMessageID(),
  343 + customReception.getSendTime(),
  344 + customReception.getSenderID(),
  345 + customReception.getReceiverID(),
  346 + customReception.getVersion(),
  347 + customReception.getFunctionCode());
  348 + INTERNATIONALTRANSIT internationaltransit = new INTERNATIONALTRANSIT(transCustomReception);
  349 +
  350 + int i=internationaltransit.masterAnalysisReception();
  351 +
  352 + return i;
  353 +
  354 + }
  355 +
  356 + private int updateCustomMessage(CustomReception customReception){
  357 + CUSTOMSMESSAGE customsmessage = new CUSTOMSMESSAGE(customReception);
  358 + return customsmessage.updateMessageByMessageid();
  359 + }
  360 +
  361 + private void errBak(String responseText,String dir){
  362 + try {
  363 +
  364 +
  365 + File errfile = new File(dir + "/" + UUID.randomUUID().toString()+".xml");
  366 + FileUtils.writeStringToFile(errfile,responseText,"UTF-8");
  367 + log.info("数据库未找到与回执适配的信息备份报文");
  368 + }catch (FileExistsException e){
  369 + e.printStackTrace();
  370 + log.error("备份文件失败");
  371 + }catch (Exception e){
  372 + e.printStackTrace();
  373 + }
  374 + }
  375 +
  376 +}
@@ -470,7 +470,7 @@ public class ORIGINMANIFESTMASTER extends WlptBaseModel { @@ -470,7 +470,7 @@ public class ORIGINMANIFESTMASTER extends WlptBaseModel {
470 public int masterAnalysisReception() { 470 public int masterAnalysisReception() {
471 //更新主单回执 471 //更新主单回执
472 int i = originmanifestmasterMapper.updateRECEIPTION(this); 472 int i = originmanifestmasterMapper.updateRECEIPTION(this);
473 - //获取单autoid 473 + //获取单autoid
474 List<ORIGINMANIFESTMASTER> originmanifestmasterList = originmanifestmasterMapper.selectAutoIdByAwb(this); 474 List<ORIGINMANIFESTMASTER> originmanifestmasterList = originmanifestmasterMapper.selectAutoIdByAwb(this);
475 if(!originmanifestmasterList.isEmpty()){ 475 if(!originmanifestmasterList.isEmpty()){
476 ORIGINMANIFESTMASTER originMaster = originmanifestmasterList.get(0); 476 ORIGINMANIFESTMASTER originMaster = originmanifestmasterList.get(0);
  1 +package com.tianbo.analysis.model;
  2 +
  3 +import com.rabbitmq.client.BuiltinExchangeType;
  4 +import com.rabbitmq.client.Channel;
  5 +import com.rabbitmq.client.Connection;
  6 +import com.rabbitmq.client.Consumer;
  7 +import com.tianbo.analysis.service.rabbit.RabbitGetMessage;
  8 +import com.tianbo.util.RabitMq.ConnectionUtil;
  9 +import lombok.extern.slf4j.Slf4j;
  10 +
  11 +import java.io.IOException;
  12 +import java.util.concurrent.TimeoutException;
  13 +
  14 +@Slf4j
  15 +public class RabbitMQ {
  16 +
  17 + public static String status = "not runing";
  18 +
  19 + private String queueName;
  20 +
  21 + private String exchangeName;
  22 +
  23 + private String routingKey;
  24 +
  25 + private String mqIp;
  26 +
  27 + private int mqPort;
  28 +
  29 + private String mqVhost;
  30 +
  31 + private String mqUsername;
  32 +
  33 + private String mqPassword;
  34 +
  35 + public String getQueueName() {
  36 + return queueName;
  37 + }
  38 +
  39 + public void setQueueName(String queueName) {
  40 + this.queueName = queueName;
  41 + }
  42 +
  43 + public String getExchangeName() {
  44 + return exchangeName;
  45 + }
  46 +
  47 + public void setExchangeName(String exchangeName) {
  48 + this.exchangeName = exchangeName;
  49 + }
  50 +
  51 + public String getRoutingKey() {
  52 + return routingKey;
  53 + }
  54 +
  55 + public void setRoutingKey(String routingKey) {
  56 + this.routingKey = routingKey;
  57 + }
  58 +
  59 + public String getMqIp() {
  60 + return mqIp;
  61 + }
  62 +
  63 + public void setMqIp(String mqIp) {
  64 + this.mqIp = mqIp;
  65 + }
  66 +
  67 + public int getMqPort() {
  68 + return mqPort;
  69 + }
  70 +
  71 + public void setMqPort(int mqPort) {
  72 + this.mqPort = mqPort;
  73 + }
  74 +
  75 + public String getMqVhost() {
  76 + return mqVhost;
  77 + }
  78 +
  79 + public void setMqVhost(String mqVhost) {
  80 + this.mqVhost = mqVhost;
  81 + }
  82 +
  83 + public String getMqUsername() {
  84 + return mqUsername;
  85 + }
  86 +
  87 + public void setMqUsername(String mqUsername) {
  88 + this.mqUsername = mqUsername;
  89 + }
  90 +
  91 + public String getMqPassword() {
  92 + return mqPassword;
  93 + }
  94 +
  95 + public void setMqPassword(String mqPassword) {
  96 + this.mqPassword = mqPassword;
  97 + }
  98 +
  99 + public RabbitMQ(String mqIp, int mqPort, String mqVhost, String mqUsername, String mqPassword,String queueName) {
  100 + this.mqIp = mqIp;
  101 + this.mqPort = mqPort;
  102 + this.mqVhost = mqVhost;
  103 + this.mqUsername = mqUsername;
  104 + this.mqPassword = mqPassword;
  105 + this.queueName = queueName;
  106 + }
  107 +
  108 + public void getResponseFromMq() {
  109 + try {
  110 + // 获取到连接以及mq通道
  111 + Connection connection = ConnectionUtil.getConnection(
  112 + mqIp,
  113 + mqPort,
  114 + mqVhost,
  115 + mqUsername,
  116 + mqPassword);
  117 + // 从连接中创建通道
  118 + Channel channel = connection.createChannel();
  119 + // 声明(创建)队列
  120 +// channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC,true);
  121 +// channel.queueDeclare(queueName, true, false, false, null);
  122 + channel.queueBind(queueName,exchangeName,routingKey);
  123 + channel.basicQos(10);
  124 + channel.basicConsume(queueName, false, new RabbitGetMessage(channel));
  125 + status = "runing";
  126 + }catch (IOException e){
  127 + status = "not runing";
  128 + e.printStackTrace();
  129 + log.error("MQ服务器连接失败,连接不上服务器:{},异常退出",e.toString());
  130 + }catch (TimeoutException e){
  131 + status = "not runing";
  132 + e.printStackTrace();
  133 + log.error("MQ服务器连接失败,连接超时:{},异常退出",e.toString());
  134 + }
  135 + }
  136 +}
  1 +package com.tianbo.analysis.service.rabbit;
  2 +
  3 +import com.rabbitmq.client.*;
  4 +import com.tianbo.analysis.handle.CustomResponseHandleThread;
  5 +import com.tianbo.analysis.task.XMLThreadPoolFactory;
  6 +import com.tianbo.util.RabitMq.MqResponse;
  7 +import lombok.extern.slf4j.Slf4j;
  8 +
  9 +import java.io.IOException;
  10 +import java.nio.charset.StandardCharsets;
  11 +import java.util.concurrent.RejectedExecutionException;
  12 +import java.util.concurrent.ThreadPoolExecutor;
  13 +
  14 +@Slf4j
  15 +public class RabbitGetMessage extends DefaultConsumer {
  16 +
  17 + private Channel ackChannel;
  18 + /**
  19 + * 初始化线程池
  20 + */
  21 + private static ThreadPoolExecutor threadPool = XMLThreadPoolFactory.instance();
  22 +
  23 + public RabbitGetMessage(Channel channel) {
  24 + super(channel);
  25 + this.ackChannel= channel;
  26 + }
  27 +
  28 +
  29 + @Override
  30 + public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
  31 + String message = new String(body, StandardCharsets.UTF_8);
  32 + log.info("消费状态:consumerTag->{},envelope->{},properties->{},body->{}",consumerTag,envelope,properties,message);
  33 + ackChannel.basicAck(envelope.getDeliveryTag(),false);
  34 + //过滤报文头部
  35 +// message = message.replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>","");
  36 + //todo:多线程处理消息
  37 + handleMessage(message);
  38 +
  39 + }
  40 +
  41 + private void handleMessage(String message){
  42 + CustomResponseHandleThread customResponseHandleThread = new CustomResponseHandleThread();
  43 + customResponseHandleThread.setResponseMessage(message);
  44 + try{
  45 + threadPool.execute(customResponseHandleThread);
  46 + }catch (RejectedExecutionException e){
  47 + e.printStackTrace();
  48 + log.error("线程池已满");
  49 + }catch (Exception e){
  50 + e.printStackTrace();
  51 + log.error("线程池异常");
  52 + }
  53 + }
  54 +
  55 +}
  1 +package com.tianbo.analysis.task;
  2 +
  3 +import com.tianbo.analysis.model.RabbitMQ;
  4 +import lombok.extern.slf4j.Slf4j;
  5 +import org.springframework.beans.factory.annotation.Value;
  6 +import org.springframework.scheduling.annotation.Scheduled;
  7 +import org.springframework.stereotype.Component;
  8 +
  9 +@Slf4j
  10 +@Component
  11 +public class MqGetMessageTask {
  12 +
  13 + @Value("${mq.connection.ip}")
  14 + private String mqIp;
  15 +
  16 + @Value("${mq.connection.port}")
  17 + private int mqPort;
  18 +
  19 + @Value("${mq.connection.vHost}")
  20 + private String mqVhost;
  21 +
  22 + @Value("${mq.connection.username}")
  23 + private String mqUsername;
  24 +
  25 + @Value("${mq.connection.password}")
  26 + private String mqPassword;
  27 +
  28 + @Value("${mq.queue.read-from-mq}")
  29 + private String queueName;
  30 +
  31 + @Value("${mq.exchange.name}")
  32 + private String exchangeName;
  33 +
  34 + @Value("${mq.exchange.routing-key}")
  35 + private String routingKey;
  36 +
  37 + private final static String MQ_RUNING_STATE= "runing";
  38 +
  39 + @Scheduled(fixedRate = 5000)
  40 + public void startTask(){
  41 + RabbitMQ rabbit = new RabbitMQ(mqIp,mqPort,mqVhost,mqUsername,mqPassword,queueName);
  42 + rabbit.setExchangeName(exchangeName);
  43 + rabbit.setRoutingKey(routingKey);
  44 + if (!MQ_RUNING_STATE.equals(RabbitMQ.status)){
  45 + rabbit.getResponseFromMq();
  46 + }
  47 + log.info("解析程序执行中。。。");
  48 + }
  49 +
  50 +
  51 +
  52 +}
@@ -38,7 +38,7 @@ public class TaskAnalysis { @@ -38,7 +38,7 @@ public class TaskAnalysis {
38 */ 38 */
39 private final static int theadamount = 64; 39 private final static int theadamount = 64;
40 40
41 - @Scheduled(fixedRate = 5000) 41 +// @Scheduled(fixedRate = 5000)
42 public void startTask(){ 42 public void startTask(){
43 final SimpleDateFormat sdf = new SimpleDateFormat( 43 final SimpleDateFormat sdf = new SimpleDateFormat(
44 "yyyy-MM-dd HH:mm:ss"); 44 "yyyy-MM-dd HH:mm:ss");
@@ -11,9 +11,9 @@ public class XMLThreadPoolFactory { @@ -11,9 +11,9 @@ public class XMLThreadPoolFactory {
11 public static ThreadPoolExecutor instance(){ 11 public static ThreadPoolExecutor instance(){
12 if (threadPool==null){ 12 if (threadPool==null){
13 XMLThreadFactory xmlThreadFactory = new XMLThreadFactory("xml"); 13 XMLThreadFactory xmlThreadFactory = new XMLThreadFactory("xml");
14 - threadPool = new ThreadPoolExecutor(64, 128,  
15 - 0L, TimeUnit.MILLISECONDS,  
16 - new LinkedBlockingQueue<Runnable>(1024), 14 + threadPool = new ThreadPoolExecutor(10, 128,
  15 + 60L, TimeUnit.SECONDS,
  16 + new LinkedBlockingQueue<Runnable>(),
17 xmlThreadFactory, 17 xmlThreadFactory,
18 new ThreadPoolExecutor.AbortPolicy()); 18 new ThreadPoolExecutor.AbortPolicy());
19 } 19 }