• 使用springboot整合ActiveMQ


    结构图

    第一步:导入依赖

    <dependency>
    			  <groupId>org.springframework.boot</groupId>
    			  <artifactId>spring-boot-starter</artifactId>
    			</dependency>
    			<!-- spring boot web支持:mvc,aop... -->
    			<dependency>
    			  <groupId>org.springframework.boot</groupId>
    			  <artifactId>spring-boot-starter-web</artifactId>
    			</dependency>
    			<dependency>
    			  <groupId>org.springframework.boot</groupId>
    			  <artifactId>spring-boot-starter-test</artifactId>
    			  <scope>test</scope>
    			</dependency>
    			<dependency>
    			  <groupId>org.springframework.boot</groupId>
    			  <artifactId>spring-boot-starter-activemq</artifactId>
    			</dependency>
    

      

    第二步:创建application.yml配置文件

    //消费者的yml
    server:
      port: 8081
    spring:
      activemq:
        broker-url: tcp://127.0.0.1:61616
        user: admin
        password: admin
    //在使用发布订阅的时候的开
      #jms:
        #pub-sub-domain: true
    
    
    
    
    //生产者的yml
    server:
      port: 8080
      #activemq配置
    spring:
      activemq:
        broker-url: tcp://127.0.0.1:61616
        user: admin
        password: admin
    

      

    第三步: 创建生产者,通过JMSTemplate模板发送消息

    //队列
    
    package com.wish.producer.producermethod;
    import org.apache.activemq.command.ActiveMQQueue;
    import org.springframework.jms.core.JmsTemplate;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.Resource;
    
    @Component
    public class QueueProducer {
        //注入JMSTemplate模板
        @Resource
        private JmsTemplate jmsTemplate;
        //创建方法
        public void sendMessage() {
            //点对点,创建队列
            ActiveMQQueue queue = new ActiveMQQueue("boot_queue");
            //发送消息
            jmsTemplate.convertAndSend(queue, "生产者产生的消息~");
            jmsTemplate.setDeliveryMode(2);
            jmsTemplate.setExplicitQosEnabled(true);
            jmsTemplate.setDeliveryPersistent(true);
        }
    }
    
    
    //发布订阅
    package com.wish.producer.producermethod;
    
    import org.apache.activemq.command.ActiveMQTopic;
    import org.springframework.jms.core.JmsTemplate;
    import org.springframework.stereotype.Component;
    import javax.annotation.Resource;
    
    @Component
    public class TopicProducer {
        //注入JMSTemplate模板
        @Resource
        private JmsTemplate jmsTemplate;
        //创建方法
        public void sendMessage(){
            //发布订阅:创建主题
            ActiveMQTopic topic=new ActiveMQTopic("boot_topic");
            //springboot默认是queue
            jmsTemplate.setPubSubDomain(true);
            //发送消息
            jmsTemplate.convertAndSend(topic,"生产者产生主题的消息~");
    
        }
    }
    

      

    第四步:创建客户端访问的方法

    //队列
    package com.wish.producer.controller;
    
    import com.wish.producer.producermethod.QueueProducer;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    
    @RestController
    public class QueueController {
        @Resource
        private QueueProducer queueProducer;
    
        @RequestMapping("/sendQueueMessage")
        public String sendMessage(){
            queueProducer.sendMessage();
            return "QueueSuccess";
        }
    }
    
    
    //发布订阅
    package com.wish.producer.controller;
    
    import com.wish.producer.producermethod.TopicProducer;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    @RestController
    public class TopicController {
        @Resource
        private TopicProducer topicProducer;
    
        @RequestMapping("/sendTopicMessage")
        public String sendMessage(){
            topicProducer.sendMessage();
            return "TopicSuccess";
        }
    }
    

      

    第五步:创建消费者

     //队列
       @JmsListener(destination = "boot_queue")
        public void getMessage(TextMessage message) throws JMSException {
            System.out.println("消费者获取到消息:"+message.getText());
        }
    
    
    //发布订阅
     @Bean
     public JmsListenerContainerFactory jmsTopicListenerContainerFactory(ConnectionFactory connectionFactory){
         DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
         factory.setConnectionFactory(connectionFactory);
         //这里必须设置为true,false则表示是queue类型
         factory.setPubSubDomain(true);
         return factory;
     }
    
    
        //消费者消费  destination队列或者主题的名字
        @JmsListener(destination = "boot_topic",containerFactory = "jmsTopicListenerContainerFactory")
        public void getMessage(TextMessage message) throws JMSException {
            System.out.println("消费者获取到消息:"+message.getText());
        }
    

      

  • 相关阅读:
    ASP.NET登录记住用户名
    .NET枚举类型转为List类型
    display:inline-block 去除间隙
    sublime text 3 常用快捷键 、常用插件
    使用背景图代码
    Photo Shop 修改、维护
    前端协作流程
    Photo Shop切图
    Photo Shop 设置
    Flex 弹性布局
  • 原文地址:https://www.cnblogs.com/wishsaber/p/12312681.html
Copyright © 2020-2023  润新知