springboot 整合 activemq(一)

springboot 整合 activemq(一)
  1. 在pom文件中引入 webscoket 的maven依赖

      <!--消息中间件-->
           <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.7.0</version>
           </dependency>
  2. 添加如下配置在application.properties配置文件

    spring.messages.basename= i18n/Messages,i18n/Pages
    # 配置消息的类型,如果是true则表示为topic消息,如果为false表示Queue消息
    spring.jms.pub-sub-domain=true
    spring.activemq.user=uinnova
    spring.activemq.password=uinnova@2015
    spring.activemq.broker-url=tcp://47.93.81.139:61616
  3. 消息生产者

    package com.momoda.statistics.common.jms;


    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jms.core.JmsMessagingTemplate;
    import org.springframework.stereotype.Service;

    import javax.jms.Destination;

    @Service
    public class Producer {


       @Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
       private JmsMessagingTemplate jmsTemplate;

       // 发送消息,destination是发送到的队列,message是待发送的消息
       public void sendMessage(Destination destination, final String message) {
           jmsTemplate.convertAndSend(destination, message);
       }
    }
  4. 消息消费者

    package com.momoda.statistics.common.jms;

    import com.momoda.statistics.common.webscoket.WebSocketServer;
    import org.springframework.jms.annotation.JmsListener;
    import org.springframework.stereotype.Component;

    import java.io.IOException;

    @Component
    public class Consumer {

       // 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
       @JmsListener(destination = "mytest.queue")
       public void receiveQueue(String text) {
           System.out.println("Consumer收到的queue报文为:" + text);
       }

       // 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
       @JmsListener(destination = "mytest.topic")
       public void receiveTopic(String text) {
           System.out.println("Consumer收到的topic报文为:" + text);
       }

       @JmsListener(destination = "20170109153114611633853")
       public void receiveSceneId(String text) throws IOException {
           System.out.println("Consumer收到的topic报文为:" + text);
           WebSocketServer.sendInfo(text);
       }
       @JmsListener(destination = "di.topic")
       public void receiveOfDiTopic(String text) throws IOException {
           System.out.println("Consumer收到的topic报文为:" + text);
           WebSocketServer.sendInfo(text);
       }


    }
  5. 测试

    package com.momoda.statistics.common.jms;

    import com.momoda.statistics.BaseTest;
    import org.apache.activemq.command.ActiveMQQueue;
    import org.apache.activemq.command.ActiveMQTopic;
    import org.junit.Test;
    import org.springframework.beans.factory.annotation.Autowired;

    import javax.jms.Destination;

    public class SpringbootJmsApplicationTests extends BaseTest {


       @Autowired
       private Producer producer;

       @Test
       public void contextLoadsQueue() throws InterruptedException {
           Destination destination = new ActiveMQQueue("mytest.queue");
       for(int i=0; i&lt;2; i++){
           producer.sendMessage(destination, "myname is chhliu!!!");
       }
       }

       @Test
       public void contextLoadsTopic() throws InterruptedException {
           Destination destination = new ActiveMQTopic("mytest.topic");
       for(int i=0; i&lt;2; i++){
           producer.sendMessage(destination, "myname is chhliu!!!");
       }
       }



    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值