Forráskód Böngészése

MQ消息功能开发

hurugang 5 éve
szülő
commit
7eadeb0df6

+ 14 - 0
pom.xml

@@ -124,6 +124,20 @@
             <artifactId>json</artifactId>
             <version>20160810</version>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-activemq</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.activemq</groupId>
+            <artifactId>activemq-pool</artifactId>
+            <version>5.14.5</version>
+        </dependency>
+        <dependency>
+            <groupId>cn.hnthyy</groupId>
+            <artifactId>thmz_system</artifactId>
+            <version>0.0.1-SNAPSHOT</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 0 - 1
src/main/java/cn/hnthyy/thmz/ThmzApplication.java

@@ -1,6 +1,5 @@
 package cn.hnthyy.thmz;
 
-import cn.hnthyy.thmz.webservice.impl.CriticalWebServiceImpl;
 import cn.hnthyy.thmz.webservice.lis.CriticalMessageServiceSoapImpl;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.SpringApplication;

+ 62 - 0
src/main/java/cn/hnthyy/thmz/config/ActiveMQConfig.java

@@ -0,0 +1,62 @@
+package cn.hnthyy.thmz.config;
+
+import org.apache.activemq.ActiveMQConnectionFactory;
+import org.apache.activemq.command.ActiveMQTopic;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
+import org.springframework.jms.config.JmsListenerContainerFactory;
+
+import javax.jms.Topic;
+
+/**
+ * @author: elvin
+ */
+@Configuration
+public class ActiveMQConfig {
+//    @Value("${queueName}")
+//    private String queueName;
+
+    @Value("${sendOrderStatusChangeTopic}")
+    private String sendOrderStatusChangeTopic;
+
+    @Value("${spring.activemq.user}")
+    private String usrName;
+
+    @Value("${spring.activemq.password}")
+    private  String password;
+
+    @Value("${spring.activemq.broker-url}")
+    private  String brokerUrl;
+
+//    @Bean
+//    public Queue queue(){
+//        return new ActiveMQQueue(queueName);
+//    }
+
+    @Bean("sendOrderStatusChangeTopic")
+    public Topic topic(){
+        return new ActiveMQTopic(sendOrderStatusChangeTopic);
+    }
+
+    @Bean
+    public ActiveMQConnectionFactory connectionFactory() {
+        return new ActiveMQConnectionFactory(usrName, password, brokerUrl);
+    }
+
+    @Bean
+    public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ActiveMQConnectionFactory connectionFactory){
+        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
+        bean.setConnectionFactory(connectionFactory);
+        return bean;
+    }
+
+    @Bean
+    public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory){
+        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();        //设置为发布订阅方式, 默认情况下使用的生产消费者方式
+        bean.setPubSubDomain(true);
+        bean.setConnectionFactory(connectionFactory);
+        return bean;
+    }
+}

+ 24 - 0
src/main/java/cn/hnthyy/thmz/service/impl/thmz/MqServiceImpl.java

@@ -0,0 +1,24 @@
+package cn.hnthyy.thmz.service.impl.thmz;
+
+import cn.hnthyy.thmz.service.thmz.MqService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.jms.core.JmsMessagingTemplate;
+import org.springframework.stereotype.Service;
+
+import javax.jms.Topic;
+
+@Service
+public class MqServiceImpl implements MqService {
+
+    @Autowired
+    private JmsMessagingTemplate jms;
+
+    @Autowired
+    @Qualifier("sendOrderStatusChangeTopic")
+    private Topic topic;
+    @Override
+    public void sendOrderStatusChangeMessage(String orderNo, String oldStatus, String newStatus) {
+        jms.convertAndSend(topic, orderNo+","+oldStatus+","+newStatus);
+    }
+}

+ 15 - 0
src/main/java/cn/hnthyy/thmz/service/thmz/MqService.java

@@ -0,0 +1,15 @@
+package cn.hnthyy.thmz.service.thmz;
+
+/**
+ * MQ 服务
+ */
+public interface MqService {
+    /**
+     * 发送支付状态变更消息
+     * @param orderNo 订单编号  patient_id+,+times+,+receiptNo
+     * @param oldStatus 旧状态
+     * @param newStatus 新状态
+     * @return
+     */
+    void sendOrderStatusChangeMessage(String orderNo,String oldStatus,String newStatus);
+}

+ 25 - 0
src/main/resources/application.yml

@@ -31,6 +31,31 @@ spring:
       enabled: true
   thymeleaf:
     cache: false
+
+
+  activemq:
+    broker-url: tcp://127.0.0.1:61616
+  # 在考虑结束之前等待的时间
+  #spring.activemq.close-timeout=15s
+  # 默认代理URL是否应该在内存中。如果指定了显式代理,则忽略此值。
+    in-memory: true
+  # 是否在回滚回滚消息之前停止消息传递。这意味着当启用此命令时,消息顺序不会被保留。
+    non-blocking-redelivery: false
+  # 等待消息发送响应的时间。设置为0等待永远。
+    send-timeout: 0s
+    #账号
+    user: system
+    # 密码
+    password: manager
+    #启了连接池, 默认是不开的
+    pool:
+      enabled: true
+      max-connections: 10
+  #默认情况下activemq提供的是queue模式,若要使用topic模式需要配置下面配置
+  jms:
+    pub-sub-domain: true
+#MQ 的topic
+sendOrderStatusChangeTopic: orderStatusChange
 #配置项:开启下划线到驼峰的自动转换. 作用:将数据库字段根据驼峰规则自动注入到对象属性。
 mybatis:
   configuration: