zoukankan      html  css  js  c++  java
  • springboot 整合activemq

    1、配置连接信息

     引入maven信息

    <!-- 整合消息队列ActiveMQ -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-activemq</artifactId>
            </dependency>
    
            <!-- 如果配置线程池则加入 -->
            <dependency>
                <groupId>org.messaginghub</groupId>
                <artifactId>pooled-jms</artifactId>
            </dependency>
    
    
            <!--消息队列连接池-->
            <dependency>
                <groupId>org.apache.activemq</groupId>
                <artifactId>activemq-pool</artifactId>
                <version>5.15.0</version>
            </dependency>
    

      

     2、增加自定义配置

    QueueConfig.java
    package cn.activemq;
    
    import org.apache.activemq.command.ActiveMQQueue;
    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 javax.jms.Queue;
    import javax.jms.Topic;
    
    @Configuration
    @EnableJms public class QueueConfig { @Value("${queue}") private String queueName; @Value("${topic}") private String topicName; @Bean public Queue queue(){ return new ActiveMQQueue(queueName); } @Bean public Topic topic(){ return new ActiveMQTopic(topicName); } }

      

    生产者 

    Producter.java
    package cn.activemq;
    
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jms.core.JmsMessagingTemplate;
    import org.springframework.stereotype.Component;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import javax.jms.Queue;
    import javax.jms.Topic;
    import java.util.List;
    
    /**
     *  生产者
     */
    @Component
    public class Producter {
    
        @Autowired
        private JmsMessagingTemplate jmsMessagingTemplate;
    
        @Autowired
        private Queue queue;
    
        @Autowired
        private Topic topic;
    
        public void send1(String  msg){
            jmsMessagingTemplate.convertAndSend(queue, msg);
        }
    
    
        public void sendQuene(Weixin object){
            jmsMessagingTemplate.convertAndSend(queue, object);
        }
    
        public void sendTopic(Weixin object){
            jmsMessagingTemplate.convertAndSend(topic, object);
        }
    
    }
    

      

    消费者

    Consumer.java
    package cn.activemq;
    
    
    import org.springframework.jms.annotation.JmsListener;
    import org.springframework.stereotype.Component;
    
    import java.util.List;
    
    /**
     * 消费者
     */
    @Component
    public class Consumer {
    
    
    
        @JmsListener(destination = "${queue}")
        public void receive12(String str) {
            System.out.println(str);
            System.out.println(11121);
        }
    
        @JmsListener(destination = "${topic}")
        public void receive2(Object object) {
            System.out.println(object);
            System.out.println(1112);
        }
    
    
        @JmsListener(destination = "${topic}")
        public void receive3(List list) {
            System.out.println(list.size());
            System.out.println(11144);
        }
    }
    

      

    调用方式:

        /**
         * 注入
         */
        @Autowired
        private Producter producter;    
    
    
         /**
         * 调用
         */
        producter.sendQuene("11");
    

      

  • 相关阅读:
    html显示乱码
    java面向对象的三大特性——封装
    java面向对象——类
    16-vue-cli4.0脚手架-项目中定义全局@mixin,变量,cli中配置以供单文件组件中直接使用
    02-滚动加载
    14.oss上传封装
    13-动态组件-keepalive-滚动加载-返回位置
    09-JSONP跨域实现原理
    08- application/x-www-form-urlencoded 与 application/json区别 / querystring
    11-利用Promise的图片异步加载 / Promise封装ajax,模拟axios / Promise的finally原理
  • 原文地址:https://www.cnblogs.com/pxblog/p/12227487.html
Copyright © 2011-2022 走看看