SpringBoot整合ActiveMQ

匿名 (未验证) 提交于 2019-12-03 00:11:01

SpringBoot能很好的整合ActiveMQ,底层封装了很多繁琐的操作,让我们能够很简单的进行配置就能使用MQ

整合流程:

  队列模式(queue):

  1.加入依赖到pom.xml

 <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-activemq</artifactId>  </dependency>

  2.配置yml

server:   port: 7777  spring:   activemq:     broker-url: tcp://127.0.0.1:61616 #MQ服务器地址     user: admin     password: admin   jms:     pub-sub-domain: false  #false = queue  true = topic ,不写默认为队列  #定义队列名称 myqueue: queue-test

  3.编写配置类bean,目的是灵活的获取信息

@Component @EnableJms   public class configBean {      @Value("${myqueue}")   //获取队列的名字     private String myQueue;      @Bean   //作为一个bean,方便注入     public Queue queue(){         return new ActiveMQQueue(myQueue);  //加入队列     } }

  4.编写生产者代码

@Component public class Producer {     @Autowired     private JmsMessagingTemplate jmsMessagingTemplate; //SpringBoot提供的模板,功能更加丰富      @Autowired     private Queue queue; //自动注入configBean里面的Queue   //手动投递,点以下投一下     public void produceMsg(){         jmsMessagingTemplate.convertAndSend(queue,"boot-test"+ UUID.randomUUID().toString().substring(0,17).replace("-",""));     }      //间隔定投     @Scheduled(fixedDelay = 3000) //三秒钟往队列里投递一次消息,加上此注解需要在主启动类上加上启动注解@EnableScheduling     public void produceMsgScheduled(){         jmsMessagingTemplate.convertAndSend(queue,"间隔定投  "+UUID.randomUUID().toString().substring(0,17).replace("-",""));         System.out.println("定时投递消息  ");     } }

  5.消费者接收消息

    消费者这边的配置和生产者差不多一样,pom.xml一样,yml修改server.port就行,其余一样

@Component public class Consumer {      @JmsListener(destination = "${myqueue}") //监听注解,时刻监听生产者是否发了消息过来     public void receiveMsg(TextMessage textMessage) throws JMSException {         System.out.println("接收到的消息为:  "+textMessage.getText());     } }

 

  主题模式(Topic):

        主题模式只需要将queue改为Topic就行

   1.yml

 

server:   port: 7777  spring:   activemq:     broker-url: tcp://127.0.0.1:61616 #MQ服务器地址     user: admin     password: admin   jms:     pub-sub-domain: true  #false = queue  true = topic ,不写默认为队列  #定义主题名称 myTopic: topic-test

 

  2.config

@Component @EnableJms public class configBean {      @Value("${myTopic}")     private String topic;      @Bean     public Topic topic(){ //主题         return new ActiveMQTopic(topic);     } }

  3.Producer

 

 

@Component public class Producer {     @Autowired     private JmsMessagingTemplate jmsMessagingTemplate;      @Autowired     private Topic topic; //config里面的Topic      public void produceMsg(){         jmsMessagingTemplate.convertAndSend(topic,"boot-test"+ UUID.randomUUID().toString().substring(0,17).replace("-",""));     }      //间隔定投     @Scheduled(fixedDelay = 3000) //三秒钟往队列里投递一次消息     public void produceMsgScheduled(){         jmsMessagingTemplate.convertAndSend(topic,"间隔定投  "+UUID.randomUUID().toString().substring(0,17).replace("-",""));         System.out.println("定时投递消息  ");     } }

 

  3.Consmer

@Component public class Consumer {      @JmsListener(destination = "${myTopic}")     public void receiveMsg(TextMessage textMessage) throws JMSException {         System.out.println("接收到的消息为:  "+textMessage.getText());     } }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!