zoukankan      html  css  js  c++  java
  • SpringBoot整合ActiveMQ

    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());
        }
    }
    生命不止,折腾不息
  • 相关阅读:
    2015年9月29日html基础加强学习笔记
    2015年9月30日html表单笔记
    2015年10月1日登陆界面表单练习
    linux 常用命令
    OSI七层模型与TCP/IP层次模型
    谈谈字符串那点事
    vim 常用命令
    VB Pcode 动态调试要点
    数据类型方法
    join()和fromkeys()的用法与注意事项
  • 原文地址:https://www.cnblogs.com/steakliu/p/11596345.html
Copyright © 2011-2022 走看看