正在显示
14 个修改的文件
包含
504 行增加
和
106 行删除
| @@ -2,7 +2,7 @@ | @@ -2,7 +2,7 @@ | ||
| 2 | web: | 2 | web: | 
| 3 | upload-path: upload/ | 3 | upload-path: upload/ | 
| 4 | server: | 4 | server: | 
| 5 | - port: 10002 | 5 | + port: 10003 | 
| 6 | servlet: | 6 | servlet: | 
| 7 | context-path: ${SERVER_CONTEXTPATH:} | 7 | context-path: ${SERVER_CONTEXTPATH:} | 
| 8 | spring: | 8 | spring: | 
| @@ -22,7 +22,7 @@ spring: | @@ -22,7 +22,7 @@ spring: | ||
| 22 | static-locations: classpath:/META-INF/resources/,classpath:/static,classpath:/resources/,classpath:/public/,file:${web.upload-path} | 22 | static-locations: classpath:/META-INF/resources/,classpath:/static,classpath:/resources/,classpath:/public/,file:${web.upload-path} | 
| 23 | 23 | ||
| 24 | application: | 24 | application: | 
| 25 | - name: WLTP-NMMS-CUSTOMMESSAGEPROCESS | 25 | + name: WLTP-NMMS-MQ-CUSTOMMESSAGEPROCESS | 
| 26 | 26 | ||
| 27 | jackson: | 27 | jackson: | 
| 28 | serialization: | 28 | serialization: | 
| @@ -37,12 +37,20 @@ spring: | @@ -37,12 +37,20 @@ spring: | ||
| 37 | #eureka主机名,会在控制页面中显示 | 37 | #eureka主机名,会在控制页面中显示 | 
| 38 | #DEV环境关闭注册。 | 38 | #DEV环境关闭注册。 | 
| 39 | features: | 39 | features: | 
| 40 | - enabled: false | 40 | + enabled: true | 
| 41 | discovery: | 41 | discovery: | 
| 42 | - enabled: false | 42 | + enabled: true | 
| 43 | service-registry: | 43 | service-registry: | 
| 44 | auto-registration: | 44 | auto-registration: | 
| 45 | - enabled: false | 45 | + enabled: true | 
| 46 | +#feign的配置,连接超时及读取超时配置 | ||
| 47 | +feign: | ||
| 48 | + client: | ||
| 49 | + config: | ||
| 50 | + default: | ||
| 51 | + connectTimeout: 6000 | ||
| 52 | + readTimeout: 60000 | ||
| 53 | + loggerLevel: basic | ||
| 46 | 54 | ||
| 47 | eureka: | 55 | eureka: | 
| 48 | instance: | 56 | instance: | 
| @@ -51,15 +59,12 @@ eureka: | @@ -51,15 +59,12 @@ eureka: | ||
| 51 | prefer-ip-address: true | 59 | prefer-ip-address: true | 
| 52 | instance-id: ${spring.cloud.client.ip-address}:${server.port} | 60 | instance-id: ${spring.cloud.client.ip-address}:${server.port} | 
| 53 | hostname: ${spring.cloud.client.ip-address} | 61 | hostname: ${spring.cloud.client.ip-address} | 
| 54 | - health-check-url-path: /actuator/health | ||
| 55 | lease-renewal-interval-in-seconds: 15 | 62 | lease-renewal-interval-in-seconds: 15 | 
| 56 | lease-expiration-duration-in-seconds: 45 | 63 | lease-expiration-duration-in-seconds: 45 | 
| 57 | - metadata-map: | ||
| 58 | - startup: ${random.int} | ||
| 59 | client: | 64 | client: | 
| 60 | #eureka注册中心服务器地址 | 65 | #eureka注册中心服务器地址 | 
| 61 | service-url: | 66 | service-url: | 
| 62 | - defaultZone: http://10.50.3.82:19527/eureka/ | 67 | + defaultZone: http://192.168.1.53:12345/eureka/ | 
| 63 | registry-fetch-interval-seconds: 30 | 68 | registry-fetch-interval-seconds: 30 | 
| 64 | 69 | ||
| 65 | #debug配置,debug或者为true的时候,logback才会记录和写入日志文件 | 70 | #debug配置,debug或者为true的时候,logback才会记录和写入日志文件 | 
| @@ -112,11 +117,11 @@ mq: | @@ -112,11 +117,11 @@ mq: | ||
| 112 | #发送mq队列名称 | 117 | #发送mq队列名称 | 
| 113 | send-to-mq: send | 118 | send-to-mq: send | 
| 114 | #读取mq队列名称 | 119 | #读取mq队列名称 | 
| 115 | - read-from-mq: receive | 120 | + read-from-mq: response.agent | 
| 116 | exchange: | 121 | exchange: | 
| 117 | #TCS回执发送到此交换,交换再把回执消息同步到其他配置的回执订阅队列 | 122 | #TCS回执发送到此交换,交换再把回执消息同步到其他配置的回执订阅队列 | 
| 118 | #交换名称 | 123 | #交换名称 | 
| 119 | - name: air.rcv.broadcast | 124 | + name: cus.rcv.broadcast | 
| 120 | #交换类型 | 125 | #交换类型 | 
| 121 | type: fanout | 126 | type: fanout | 
| 122 | #routing key名称,此处为空,所有绑定交换的队列都被广播 | 127 | #routing key名称,此处为空,所有绑定交换的队列都被广播 | 
| @@ -125,7 +130,7 @@ mq: | @@ -125,7 +130,7 @@ mq: | ||
| 125 | connection: | 130 | connection: | 
| 126 | ip: 218.28.199.134 | 131 | ip: 218.28.199.134 | 
| 127 | port: 8004 | 132 | port: 8004 | 
| 128 | - vHost: AIRTRANS | 133 | + vHost: NMMS | 
| 129 | username: tianbo | 134 | username: tianbo | 
| 130 | password: vmvnv1v2VV | 135 | password: vmvnv1v2VV | 
| 131 | # ip: 192.168.1.63 | 136 | # ip: 192.168.1.63 | 
| @@ -11,9 +11,9 @@ | @@ -11,9 +11,9 @@ | ||
| 11 | <relativePath/> <!-- lookup parent from repository --> | 11 | <relativePath/> <!-- lookup parent from repository --> | 
| 12 | </parent> | 12 | </parent> | 
| 13 | <groupId>com.tianbo</groupId> | 13 | <groupId>com.tianbo</groupId> | 
| 14 | - <artifactId>customs</artifactId> | 14 | + <artifactId>customs-with-mq</artifactId> | 
| 15 | <version>1.0-SNAPSHOT</version> | 15 | <version>1.0-SNAPSHOT</version> | 
| 16 | - <name>customMessageProcess</name> | 16 | + <name>customMessageWithMQ</name> | 
| 17 | <description>process customs xml files with RabbitMQ</description> | 17 | <description>process customs xml files with RabbitMQ</description> | 
| 18 | <properties> | 18 | <properties> | 
| 19 | <druid.version>1.1.9</druid.version> | 19 | <druid.version>1.1.9</druid.version> | 
| @@ -32,6 +32,10 @@ | @@ -32,6 +32,10 @@ | ||
| 32 | <version>1.3.2</version> | 32 | <version>1.3.2</version> | 
| 33 | </dependency> | 33 | </dependency> | 
| 34 | <dependency> | 34 | <dependency> | 
| 35 | + <groupId>org.springframework.cloud</groupId> | ||
| 36 | + <artifactId>spring-cloud-starter-openfeign</artifactId> | ||
| 37 | + </dependency> | ||
| 38 | + <dependency> | ||
| 35 | <groupId>org.springframework.boot</groupId> | 39 | <groupId>org.springframework.boot</groupId> | 
| 36 | <artifactId>spring-boot-starter-websocket</artifactId> | 40 | <artifactId>spring-boot-starter-websocket</artifactId> | 
| 37 | <exclusions> | 41 | <exclusions> | 
| @@ -97,6 +101,12 @@ | @@ -97,6 +101,12 @@ | ||
| 97 | <artifactId>util</artifactId> | 101 | <artifactId>util</artifactId> | 
| 98 | <version>1.0-SNAPSHOT</version> | 102 | <version>1.0-SNAPSHOT</version> | 
| 99 | </dependency> | 103 | </dependency> | 
| 104 | + <dependency> | ||
| 105 | + <groupId>com.tianbo</groupId> | ||
| 106 | + <artifactId>util</artifactId> | ||
| 107 | + <version>1.0-SNAPSHOT</version> | ||
| 108 | + <scope>compile</scope> | ||
| 109 | + </dependency> | ||
| 100 | <!--util依赖--> | 110 | <!--util依赖--> | 
| 101 | 111 | ||
| 102 | <!--<!–IMF需要的包–>--> | 112 | <!--<!–IMF需要的包–>--> | 
| @@ -12,6 +12,7 @@ import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfigurat | @@ -12,6 +12,7 @@ import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfigurat | ||
| 12 | import org.springframework.boot.web.client.RestTemplateBuilder; | 12 | import org.springframework.boot.web.client.RestTemplateBuilder; | 
| 13 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; | 13 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; | 
| 14 | import org.springframework.cloud.client.loadbalancer.LoadBalanced; | 14 | import org.springframework.cloud.client.loadbalancer.LoadBalanced; | 
| 15 | +import org.springframework.cloud.openfeign.EnableFeignClients; | ||
| 15 | import org.springframework.context.annotation.Bean; | 16 | import org.springframework.context.annotation.Bean; | 
| 16 | import org.springframework.scheduling.annotation.EnableScheduling; | 17 | import org.springframework.scheduling.annotation.EnableScheduling; | 
| 17 | import org.springframework.transaction.annotation.EnableTransactionManagement; | 18 | import org.springframework.transaction.annotation.EnableTransactionManagement; | 
| @@ -25,6 +26,7 @@ import java.time.Duration; | @@ -25,6 +26,7 @@ import java.time.Duration; | ||
| 25 | HibernateJpaAutoConfiguration.class}) | 26 | HibernateJpaAutoConfiguration.class}) | 
| 26 | @EnableScheduling | 27 | @EnableScheduling | 
| 27 | @EnableDiscoveryClient | 28 | @EnableDiscoveryClient | 
| 29 | +@EnableFeignClients | ||
| 28 | @EnableTransactionManagement | 30 | @EnableTransactionManagement | 
| 29 | //@MapperScan("com.tianbo.analysis.dao") | 31 | //@MapperScan("com.tianbo.analysis.dao") | 
| 30 | public class MQBootApplication { | 32 | public class MQBootApplication { | 
| 1 | +package com.tianbo.analysis.handle; | ||
| 2 | + | ||
| 3 | +import com.rabbitmq.client.Channel; | ||
| 4 | +import com.rabbitmq.client.Envelope; | ||
| 5 | +import com.tianbo.analysis.service.CustomAnalysisService; | ||
| 6 | +import com.tianbo.util.Date.DateUtil; | ||
| 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.springframework.beans.factory.annotation.Autowired; | ||
| 13 | +import org.springframework.beans.factory.annotation.Value; | ||
| 14 | +import org.springframework.stereotype.Component; | ||
| 15 | +import org.xml.sax.InputSource; | ||
| 16 | + | ||
| 17 | +import javax.annotation.PostConstruct; | ||
| 18 | +import javax.xml.parsers.DocumentBuilder; | ||
| 19 | +import javax.xml.parsers.DocumentBuilderFactory; | ||
| 20 | +import java.io.File; | ||
| 21 | +import java.io.IOException; | ||
| 22 | +import java.io.StringReader; | ||
| 23 | +import java.util.UUID; | ||
| 24 | + | ||
| 25 | +/** | ||
| 26 | + * 报文分发到舱单回执解析服务线程 | ||
| 27 | + * 分发前判定报文是否为正常合规的XML报文 | ||
| 28 | + * 合规的过滤掉XML的头部信息(为了能将原报文内容转发到IMF), | ||
| 29 | + * 并备份一份到bakupDir目录,并转发一份到新舱单转发回执到IMF发送端监听目录下 | ||
| 30 | + * 不合规的备份到errBakDir目录 | ||
| 31 | + */ | ||
| 32 | + | ||
| 33 | +@Component | ||
| 34 | +@Data | ||
| 35 | +@Slf4j | ||
| 36 | +public class CustomHandleThead implements Runnable{ | ||
| 37 | + | ||
| 38 | + private String msg; | ||
| 39 | + | ||
| 40 | + private Channel ackChannel; | ||
| 41 | + | ||
| 42 | + private Envelope envelope; | ||
| 43 | + | ||
| 44 | + //备份目录 | ||
| 45 | + @Value("${custom.receptBakDir}") | ||
| 46 | + private String bakupDir; | ||
| 47 | + | ||
| 48 | + @Value("${custom.errBakDir}") | ||
| 49 | + private String errBakDir; | ||
| 50 | + | ||
| 51 | + //回执转发目录 | ||
| 52 | + @Value("${custom.transmitDir}") | ||
| 53 | + private String transmitDir; | ||
| 54 | + | ||
| 55 | + private static CustomHandleThead customHandleThead; | ||
| 56 | + | ||
| 57 | + @Autowired | ||
| 58 | + CustomAnalysisService customAnalysisService; | ||
| 59 | + | ||
| 60 | + public CustomHandleThead(){ | ||
| 61 | + | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + public CustomHandleThead(String msg) { | ||
| 65 | + this.msg = msg; | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + public CustomHandleThead(String msg, Channel ackChannel,Envelope envelope) { | ||
| 69 | + this.msg = msg; | ||
| 70 | + this.ackChannel = ackChannel; | ||
| 71 | + this.envelope = envelope; | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + @PostConstruct | ||
| 75 | + public void init() { | ||
| 76 | + customHandleThead = this; | ||
| 77 | + | ||
| 78 | + customHandleThead.customAnalysisService = this.customAnalysisService; | ||
| 79 | + customHandleThead.bakupDir = this.bakupDir; | ||
| 80 | + customHandleThead.errBakDir = this.errBakDir; | ||
| 81 | + customHandleThead.transmitDir = this.transmitDir; | ||
| 82 | + | ||
| 83 | + | ||
| 84 | + // 初使化时将已静态化的testService实例化 | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + @Override | ||
| 88 | + public void run() { | ||
| 89 | + try { | ||
| 90 | + if (StringUtils.isNotBlank(msg) && isXmlDocument(msg)){ | ||
| 91 | + //过滤报文头部 | ||
| 92 | + msg = msg.replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>",""); | ||
| 93 | + | ||
| 94 | + boolean result = customHandleThead.customAnalysisService.analysis(msg); | ||
| 95 | + if (result){ | ||
| 96 | + ack(); | ||
| 97 | + }else { | ||
| 98 | + ack(); | ||
| 99 | + } | ||
| 100 | + }else { | ||
| 101 | + //错误报文直接消费 不处理;并备份 | ||
| 102 | + ack(); | ||
| 103 | + } | ||
| 104 | + }catch (Exception e){ | ||
| 105 | + nack(); | ||
| 106 | + e.printStackTrace(); | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + private void ack(){ | ||
| 112 | + try { | ||
| 113 | + ackChannel.basicAck(envelope.getDeliveryTag(),false); | ||
| 114 | + log.warn("tag:{}已消费",envelope.getDeliveryTag()); | ||
| 115 | + }catch (IOException e){ | ||
| 116 | + e.printStackTrace(); | ||
| 117 | + } | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + private void nack(){ | ||
| 121 | + try { | ||
| 122 | + ackChannel.basicNack(envelope.getDeliveryTag(), false, true); | ||
| 123 | + }catch (IOException e){ | ||
| 124 | + e.printStackTrace(); | ||
| 125 | + } | ||
| 126 | + } | ||
| 127 | + | ||
| 128 | + | ||
| 129 | + | ||
| 130 | + private static boolean isXmlDocument(String rtnMsg) { | ||
| 131 | + boolean flag = true; | ||
| 132 | + try { | ||
| 133 | + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); | ||
| 134 | + DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); | ||
| 135 | + builder.parse(new InputSource(new StringReader(rtnMsg))); | ||
| 136 | + } catch (Exception e) { | ||
| 137 | + flag = false; | ||
| 138 | + } | ||
| 139 | + return flag; | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + public static void bakFile(String content,String filePath){ | ||
| 143 | + try { | ||
| 144 | + String fileName = customHandleThead.bakupDir + UUID.randomUUID().toString()+".xml"; | ||
| 145 | + log.info("-----------{}报文保存成功----------",fileName); | ||
| 146 | + File fileToDirectory = new File(fileName); | ||
| 147 | + FileUtils.writeStringToFile(fileToDirectory,content,"UTF-8"); | ||
| 148 | + }catch (IOException e){ | ||
| 149 | + | ||
| 150 | + } | ||
| 151 | + | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | + | ||
| 155 | +} | 
| 1 | +package com.tianbo.analysis.handle; | ||
| 2 | + | ||
| 3 | +import com.rabbitmq.client.AMQP; | ||
| 4 | +import com.rabbitmq.client.Channel; | ||
| 5 | +import com.rabbitmq.client.DefaultConsumer; | ||
| 6 | +import com.rabbitmq.client.Envelope; | ||
| 7 | +import com.tianbo.analysis.task.XMLThreadPoolFactory; | ||
| 8 | +import lombok.extern.slf4j.Slf4j; | ||
| 9 | + | ||
| 10 | +import java.io.IOException; | ||
| 11 | +import java.nio.charset.StandardCharsets; | ||
| 12 | +import java.util.concurrent.RejectedExecutionException; | ||
| 13 | +import java.util.concurrent.ThreadPoolExecutor; | ||
| 14 | + | ||
| 15 | +@Slf4j | ||
| 16 | +public class RabbitGetMessage extends DefaultConsumer { | ||
| 17 | + | ||
| 18 | + private Channel ackChannel; | ||
| 19 | + /** | ||
| 20 | + * 初始化线程池 | ||
| 21 | + */ | ||
| 22 | + private static ThreadPoolExecutor threadPool = XMLThreadPoolFactory.instance(); | ||
| 23 | + | ||
| 24 | + public RabbitGetMessage(Channel channel) { | ||
| 25 | + super(channel); | ||
| 26 | + this.ackChannel= channel; | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + | ||
| 30 | + @Override | ||
| 31 | + public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { | ||
| 32 | + String message = new String(body, StandardCharsets.UTF_8); | ||
| 33 | +// log.info("消费状态:consumerTag->{},envelope->{},properties->{},body->{}",consumerTag,envelope,properties,message); | ||
| 34 | + | ||
| 35 | +// ackChannel.basicAck(envelope.getDeliveryTag(),false); | ||
| 36 | + | ||
| 37 | + //todo:多线程处理消息 | ||
| 38 | + handleMessage(message,ackChannel,envelope); | ||
| 39 | + | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + private void handleMessage(String message,Channel channel,Envelope envelope){ | ||
| 43 | + CustomHandleThead customResponseHandleThread = new CustomHandleThead(message,channel,envelope); | ||
| 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.model; | ||
| 2 | - | ||
| 3 | -import lombok.Data; | ||
| 4 | - | ||
| 5 | -@Data | ||
| 6 | -public class CustomReception { | ||
| 7 | - //回执报头 | ||
| 8 | - private String messageID; | ||
| 9 | - private String functionCode; | ||
| 10 | - private String messageType; | ||
| 11 | - private String senderID; | ||
| 12 | - private String receiverID; | ||
| 13 | - private String sendTime; | ||
| 14 | - private String version; | ||
| 15 | - | ||
| 16 | - //一般业务回执 | ||
| 17 | - private String flightNo; | ||
| 18 | - private String flightDate; | ||
| 19 | - private String wayBillMaster; | ||
| 20 | - private String wayBillSecond; | ||
| 21 | - private String responseCode; | ||
| 22 | - private String responseText; | ||
| 23 | - | ||
| 24 | - //国际转运回执 | ||
| 25 | - private String importFlightDate; | ||
| 26 | - private String importFlightNo; | ||
| 27 | - private String importWaybillMaster; | ||
| 28 | - private String exportFlightDate; | ||
| 29 | - private String exportFlightNo; | ||
| 30 | - private String exportWaybillMaster; | ||
| 31 | - | ||
| 32 | - public CustomReception(String messageType, | ||
| 33 | - String flightNo, | ||
| 34 | - String flightDate, | ||
| 35 | - String wayBillMaster, | ||
| 36 | - String wayBillSecond, | ||
| 37 | - String responseCode, | ||
| 38 | - String responseText, | ||
| 39 | - String messageID, | ||
| 40 | - String sendTime, | ||
| 41 | - String senderID, | ||
| 42 | - String receiverID, | ||
| 43 | - String version, | ||
| 44 | - String functionCode){ | ||
| 45 | - this.messageType = messageType; | ||
| 46 | - this.flightNo = flightNo; | ||
| 47 | - this.flightDate = flightDate; | ||
| 48 | - this.wayBillMaster = wayBillMaster; | ||
| 49 | - this.wayBillSecond = wayBillSecond; | ||
| 50 | - this.responseCode = responseCode; | ||
| 51 | - this.responseText = responseText; | ||
| 52 | - this.messageID = messageID; | ||
| 53 | - this.sendTime = sendTime; | ||
| 54 | - this.senderID =senderID; | ||
| 55 | - this.receiverID = receiverID; | ||
| 56 | - this.version = version; | ||
| 57 | - this.functionCode = functionCode; | ||
| 58 | - } | ||
| 59 | - | ||
| 60 | - public CustomReception(String messageType, | ||
| 61 | - String importFlightNo, | ||
| 62 | - String importFlightDate, | ||
| 63 | - String importWaybillMaster, | ||
| 64 | - String transResponseCode, | ||
| 65 | - String transResponseText, | ||
| 66 | - String messageID, | ||
| 67 | - String sendTime, | ||
| 68 | - String senderID, | ||
| 69 | - String receiverID, | ||
| 70 | - String version, | ||
| 71 | - String functionCode){ | ||
| 72 | - this.messageType = messageType; | ||
| 73 | - this.importFlightNo = importFlightNo; | ||
| 74 | - this.importFlightDate = importFlightDate; | ||
| 75 | - this.importWaybillMaster = importWaybillMaster; | ||
| 76 | - this.responseCode = transResponseCode; | ||
| 77 | - this.responseText = transResponseText; | ||
| 78 | - this.messageID = messageID; | ||
| 79 | - this.sendTime = sendTime; | ||
| 80 | - this.senderID =senderID; | ||
| 81 | - this.receiverID = receiverID; | ||
| 82 | - this.version = version; | ||
| 83 | - this.functionCode = functionCode; | ||
| 84 | - } | ||
| 85 | -} | 
| 1 | +package com.tianbo.analysis.model; | ||
| 2 | + | ||
| 3 | +import com.rabbitmq.client.Channel; | ||
| 4 | +import com.rabbitmq.client.Connection; | ||
| 5 | +import com.tianbo.analysis.handle.RabbitGetMessage; | ||
| 6 | +import com.tianbo.util.RabitMq.ConnectionUtil; | ||
| 7 | +import lombok.extern.slf4j.Slf4j; | ||
| 8 | + | ||
| 9 | +import java.io.IOException; | ||
| 10 | +import java.util.concurrent.TimeoutException; | ||
| 11 | + | ||
| 12 | +@Slf4j | ||
| 13 | +public class RabbitMQ { | ||
| 14 | + | ||
| 15 | + public static String status = "not runing"; | ||
| 16 | + | ||
| 17 | + private String queueName; | ||
| 18 | + | ||
| 19 | + private String exchangeName; | ||
| 20 | + | ||
| 21 | + private String routingKey; | ||
| 22 | + | ||
| 23 | + private String mqIp; | ||
| 24 | + | ||
| 25 | + private int mqPort; | ||
| 26 | + | ||
| 27 | + private String mqVhost; | ||
| 28 | + | ||
| 29 | + private String mqUsername; | ||
| 30 | + | ||
| 31 | + private String mqPassword; | ||
| 32 | + | ||
| 33 | + public String getQueueName() { | ||
| 34 | + return queueName; | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + public void setQueueName(String queueName) { | ||
| 38 | + this.queueName = queueName; | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + public String getExchangeName() { | ||
| 42 | + return exchangeName; | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + public void setExchangeName(String exchangeName) { | ||
| 46 | + this.exchangeName = exchangeName; | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + public String getRoutingKey() { | ||
| 50 | + return routingKey; | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + public void setRoutingKey(String routingKey) { | ||
| 54 | + this.routingKey = routingKey; | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + public String getMqIp() { | ||
| 58 | + return mqIp; | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + public void setMqIp(String mqIp) { | ||
| 62 | + this.mqIp = mqIp; | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + public int getMqPort() { | ||
| 66 | + return mqPort; | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + public void setMqPort(int mqPort) { | ||
| 70 | + this.mqPort = mqPort; | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + public String getMqVhost() { | ||
| 74 | + return mqVhost; | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + public void setMqVhost(String mqVhost) { | ||
| 78 | + this.mqVhost = mqVhost; | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + public String getMqUsername() { | ||
| 82 | + return mqUsername; | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + public void setMqUsername(String mqUsername) { | ||
| 86 | + this.mqUsername = mqUsername; | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + public String getMqPassword() { | ||
| 90 | + return mqPassword; | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + public void setMqPassword(String mqPassword) { | ||
| 94 | + this.mqPassword = mqPassword; | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + public RabbitMQ(String mqIp, int mqPort, String mqVhost, String mqUsername, String mqPassword, String queueName) { | ||
| 98 | + this.mqIp = mqIp; | ||
| 99 | + this.mqPort = mqPort; | ||
| 100 | + this.mqVhost = mqVhost; | ||
| 101 | + this.mqUsername = mqUsername; | ||
| 102 | + this.mqPassword = mqPassword; | ||
| 103 | + this.queueName = queueName; | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + public void getResponseFromMq() { | ||
| 107 | + try { | ||
| 108 | + // 获取到连接以及mq通道 | ||
| 109 | + Connection connection = ConnectionUtil.getConnection( | ||
| 110 | + mqIp, | ||
| 111 | + mqPort, | ||
| 112 | + mqVhost, | ||
| 113 | + mqUsername, | ||
| 114 | + mqPassword); | ||
| 115 | + // 从连接中创建通道 | ||
| 116 | + Channel channel = connection.createChannel(); | ||
| 117 | + // 声明(创建)队列 | ||
| 118 | +// channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC,true); | ||
| 119 | +// channel.queueDeclare(queueName, true, false, false, null); | ||
| 120 | + channel.queueBind(queueName,exchangeName,routingKey); | ||
| 121 | + channel.basicQos(15); | ||
| 122 | + channel.basicConsume(queueName, false, new RabbitGetMessage(channel)); | ||
| 123 | + status = "runing"; | ||
| 124 | + }catch (IOException e){ | ||
| 125 | + status = "not runing"; | ||
| 126 | + e.printStackTrace(); | ||
| 127 | + log.error("MQ服务器连接失败,连接不上服务器:{},异常退出",e.toString()); | ||
| 128 | + }catch (TimeoutException e){ | ||
| 129 | + status = "not runing"; | ||
| 130 | + e.printStackTrace(); | ||
| 131 | + log.error("MQ服务器连接失败,连接超时:{},异常退出",e.toString()); | ||
| 132 | + }catch (Exception e){ | ||
| 133 | + e.printStackTrace(); | ||
| 134 | + status = "not runing"; | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + } | ||
| 138 | +} | 
| 1 | package com.tianbo.analysis.rabbitmq; | 1 | package com.tianbo.analysis.rabbitmq; | 
| 2 | 2 | ||
| 3 | import com.rabbitmq.client.*; | 3 | import com.rabbitmq.client.*; | 
| 4 | +import com.tianbo.analysis.bean.SpringBeanUtitl; | ||
| 5 | +import com.tianbo.analysis.handle.CustomHandleThead; | ||
| 6 | +import com.tianbo.analysis.service.CustomAnalysisService; | ||
| 7 | +import com.tianbo.analysis.task.XMLThreadPoolFactory; | ||
| 4 | import com.tianbo.util.RabitMq.MqResponse; | 8 | import com.tianbo.util.RabitMq.MqResponse; | 
| 5 | import lombok.extern.slf4j.Slf4j; | 9 | import lombok.extern.slf4j.Slf4j; | 
| 6 | import org.apache.commons.io.FileUtils; | 10 | import org.apache.commons.io.FileUtils; | 
| 11 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 12 | +import org.springframework.stereotype.Component; | ||
| 13 | + | ||
| 7 | import java.io.File; | 14 | import java.io.File; | 
| 8 | import java.io.IOException; | 15 | import java.io.IOException; | 
| 9 | import java.nio.charset.StandardCharsets; | 16 | import java.nio.charset.StandardCharsets; | 
| 10 | import java.util.UUID; | 17 | import java.util.UUID; | 
| 18 | +import java.util.concurrent.RejectedExecutionException; | ||
| 19 | +import java.util.concurrent.ThreadPoolExecutor; | ||
| 11 | import java.util.regex.*; | 20 | import java.util.regex.*; | 
| 12 | 21 | ||
| 13 | /** | 22 | /** | 
| @@ -22,12 +31,18 @@ public class GetResponse extends DefaultConsumer { | @@ -22,12 +31,18 @@ public class GetResponse extends DefaultConsumer { | ||
| 22 | 31 | ||
| 23 | private static GetResponse getResponse; | 32 | private static GetResponse getResponse; | 
| 24 | 33 | ||
| 34 | + /** | ||
| 35 | + * 初始化线程池 | ||
| 36 | + */ | ||
| 37 | + private static ThreadPoolExecutor threadPool = XMLThreadPoolFactory.instance(); | ||
| 38 | + | ||
| 25 | public GetResponse(Channel channel) { | 39 | public GetResponse(Channel channel) { | 
| 26 | super(channel); | 40 | super(channel); | 
| 27 | } | 41 | } | 
| 28 | public GetResponse(Channel channel,String dir) { | 42 | public GetResponse(Channel channel,String dir) { | 
| 29 | super(channel); | 43 | super(channel); | 
| 30 | this.receptDir = dir; | 44 | this.receptDir = dir; | 
| 45 | + | ||
| 31 | } | 46 | } | 
| 32 | /** | 47 | /** | 
| 33 | * 处理回来的信息 | 48 | * 处理回来的信息 | 
| @@ -46,7 +61,9 @@ public class GetResponse extends DefaultConsumer { | @@ -46,7 +61,9 @@ public class GetResponse extends DefaultConsumer { | ||
| 46 | // System.err.println("body: " + new String(body)); | 61 | // System.err.println("body: " + new String(body)); | 
| 47 | this.mqResponse = new MqResponse(consumerTag,envelope,properties,new String(body, StandardCharsets.UTF_8)); | 62 | this.mqResponse = new MqResponse(consumerTag,envelope,properties,new String(body, StandardCharsets.UTF_8)); | 
| 48 | log.info("-----------获取到报文----------\n{}",mqResponse.getContent()); | 63 | log.info("-----------获取到报文----------\n{}",mqResponse.getContent()); | 
| 49 | - writeToReadDir(mqResponse.getContent()); | 64 | + | 
| 65 | + handleMessage(mqResponse.getContent()); | ||
| 66 | +// writeToReadDir(mqResponse.getContent()); | ||
| 50 | 67 | ||
| 51 | //写入回执目录 | 68 | //写入回执目录 | 
| 52 | } | 69 | } | 
| @@ -71,4 +88,18 @@ public class GetResponse extends DefaultConsumer { | @@ -71,4 +88,18 @@ public class GetResponse extends DefaultConsumer { | ||
| 71 | 88 | ||
| 72 | } | 89 | } | 
| 73 | 90 | ||
| 91 | + private void handleMessage(String message){ | ||
| 92 | + CustomHandleThead customResponseHandleThread = new CustomHandleThead(); | ||
| 93 | + customResponseHandleThread.setMsg(message); | ||
| 94 | + try{ | ||
| 95 | + threadPool.execute(customResponseHandleThread); | ||
| 96 | + }catch (RejectedExecutionException e){ | ||
| 97 | + e.printStackTrace(); | ||
| 98 | + log.error("线程池已满"); | ||
| 99 | + }catch (Exception e){ | ||
| 100 | + e.printStackTrace(); | ||
| 101 | + log.error("线程池异常"); | ||
| 102 | + } | ||
| 103 | + } | ||
| 104 | + | ||
| 74 | } | 105 | } | 
| @@ -46,8 +46,8 @@ public class ReadMessage { | @@ -46,8 +46,8 @@ public class ReadMessage { | ||
| 46 | String exchangeName = "amq.topic"; | 46 | String exchangeName = "amq.topic"; | 
| 47 | String routingKey = "consumer.#"; | 47 | String routingKey = "consumer.#"; | 
| 48 | // 声明(创建)队列 | 48 | // 声明(创建)队列 | 
| 49 | - channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC,true); | ||
| 50 | - channel.queueDeclare(queueName, true, false, false, null); | 49 | +// channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC,true); | 
| 50 | +// channel.queueDeclare(queueName, true, false, false, null); | ||
| 51 | channel.queueBind(queueName,exchangeName,routingKey); | 51 | channel.queueBind(queueName,exchangeName,routingKey); | 
| 52 | status = "runing"; | 52 | status = "runing"; | 
| 53 | channel.basicConsume(queueName, true, new GetResponse(channel,dir)); | 53 | channel.basicConsume(queueName, true, new GetResponse(channel,dir)); | 
| 1 | +package com.tianbo.analysis.service; | ||
| 2 | + | ||
| 3 | +import org.springframework.cloud.openfeign.FeignClient; | ||
| 4 | +import org.springframework.web.bind.annotation.PostMapping; | ||
| 5 | +import org.springframework.web.bind.annotation.RequestParam; | ||
| 6 | + | ||
| 7 | +@FeignClient(name = "wltp-nmms-custom-reciption-resolver") | ||
| 8 | +public interface CustomAnalysisService { | ||
| 9 | + | ||
| 10 | + @PostMapping("/cust/analysis") | ||
| 11 | + boolean analysis(@RequestParam(value = "xml",required = true) String xml); | ||
| 12 | +} | 
| 1 | +package com.tianbo.analysis.task; | ||
| 2 | + | ||
| 3 | + | ||
| 4 | +import com.tianbo.analysis.model.RabbitMQ; | ||
| 5 | +import com.tianbo.analysis.rabbitmq.ReadMessage; | ||
| 6 | +import com.tianbo.util.Date.DateUtil; | ||
| 7 | +import lombok.extern.slf4j.Slf4j; | ||
| 8 | +import org.springframework.beans.factory.annotation.Value; | ||
| 9 | +import org.springframework.scheduling.annotation.Scheduled; | ||
| 10 | +import org.springframework.stereotype.Component; | ||
| 11 | + | ||
| 12 | +import java.text.SimpleDateFormat; | ||
| 13 | +import java.util.Date; | ||
| 14 | +import java.util.concurrent.ThreadPoolExecutor; | ||
| 15 | + | ||
| 16 | +/** | ||
| 17 | + * 新舱单从mq服务器的tcs回执队列中获取回执报文并落地保存 | ||
| 18 | + * 回执解析定时任务 | ||
| 19 | + * 从mq获取报文 落地保存 | ||
| 20 | + */ | ||
| 21 | +@Slf4j | ||
| 22 | +@Component | ||
| 23 | +public class CustomMqGetTask { | ||
| 24 | + | ||
| 25 | + //报文保存目录 | ||
| 26 | + @Value("${mq.dir.saveXml-dir}") | ||
| 27 | + private String receptDir; | ||
| 28 | + | ||
| 29 | + @Value("${mq.connection.ip}") | ||
| 30 | + private String mqIp; | ||
| 31 | + | ||
| 32 | + @Value("${mq.connection.port}") | ||
| 33 | + private int mqPort; | ||
| 34 | + | ||
| 35 | + @Value("${mq.connection.vHost}") | ||
| 36 | + private String mqVhost; | ||
| 37 | + | ||
| 38 | + @Value("${mq.connection.username}") | ||
| 39 | + private String mqUsername; | ||
| 40 | + | ||
| 41 | + @Value("${mq.connection.password}") | ||
| 42 | + private String mqPassword; | ||
| 43 | + | ||
| 44 | + @Value("${mq.queue.read-from-mq}") | ||
| 45 | + private String queueName; | ||
| 46 | + | ||
| 47 | + @Value("${mq.exchange.name}") | ||
| 48 | + private String exchangeName; | ||
| 49 | + | ||
| 50 | + @Value("${mq.exchange.routing-key}") | ||
| 51 | + private String routingKey; | ||
| 52 | + | ||
| 53 | + @Scheduled(fixedRate = 5000) | ||
| 54 | + public void startTask(){ | ||
| 55 | + | ||
| 56 | + try { | ||
| 57 | + //从mq获取回执 | ||
| 58 | + RabbitMQ readMessage = new RabbitMQ(mqIp,mqPort,mqVhost,mqUsername,mqPassword,queueName); | ||
| 59 | + readMessage.setExchangeName(exchangeName); | ||
| 60 | + readMessage.setRoutingKey(routingKey); | ||
| 61 | + if(!"runing".equals(RabbitMQ.status)){ | ||
| 62 | + log.info("开始连接MQ服务器"); | ||
| 63 | + readMessage.getResponseFromMq(); | ||
| 64 | + log.info("MQ通道建立成功"); | ||
| 65 | + } | ||
| 66 | + }catch (Exception e){ | ||
| 67 | + e.printStackTrace(); | ||
| 68 | + log.error("程序出错",e); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | +// log.info("获取消息结束"); | ||
| 72 | + | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | +} | 
| @@ -43,7 +43,7 @@ public class NmmsMqGetTask { | @@ -43,7 +43,7 @@ public class NmmsMqGetTask { | ||
| 43 | @Value("${mq.queue.read-from-mq}") | 43 | @Value("${mq.queue.read-from-mq}") | 
| 44 | private String queueName; | 44 | private String queueName; | 
| 45 | 45 | ||
| 46 | - @Scheduled(fixedRate = 5000) | 46 | +// @Scheduled(fixedRate = 5000) | 
| 47 | public void startTask(){ | 47 | public void startTask(){ | 
| 48 | 48 | ||
| 49 | 49 | 
| @@ -59,7 +59,7 @@ public class NmmsMqSendTask { | @@ -59,7 +59,7 @@ public class NmmsMqSendTask { | ||
| 59 | */ | 59 | */ | 
| 60 | private final static int theadamount = 10; | 60 | private final static int theadamount = 10; | 
| 61 | 61 | ||
| 62 | - @Scheduled(fixedRate = 5000) | 62 | +// @Scheduled(fixedRate = 5000) | 
| 63 | public void startTask(){ | 63 | public void startTask(){ | 
| 64 | 64 | ||
| 65 | final SimpleDateFormat sdf = new SimpleDateFormat( | 65 | final SimpleDateFormat sdf = new SimpleDateFormat( | 
| @@ -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(10, 100, | 14 | + threadPool = new ThreadPoolExecutor(15, 240, | 
| 15 | 0L, TimeUnit.MILLISECONDS, | 15 | 0L, TimeUnit.MILLISECONDS, | 
| 16 | - new LinkedBlockingQueue<Runnable>(1024), | 16 | + new LinkedBlockingQueue<Runnable>(), | 
| 17 | xmlThreadFactory, | 17 | xmlThreadFactory, | 
| 18 | new ThreadPoolExecutor.AbortPolicy()); | 18 | new ThreadPoolExecutor.AbortPolicy()); | 
| 19 | } | 19 | } | 
- 
请 注册 或 登录 后发表评论