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

    一、添加依赖

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

    二、添加配置

    #activemq
    spring.activemq.broker-url=tcp://10.100.1.99:61616
    spring.activemq.in-memory=true
    spring.activemq.user=admin
    spring.activemq.password=admin
    spring.activemq.pool.enabled=false

    三、生产者和消费者

    Producer

    package com.dc.sb.web.activemq;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jms.core.JmsMessagingTemplate;
    import org.springframework.stereotype.Component;
    
    import javax.jms.Destination;
    
    /**
     * @author DUCHONG
     * @since 2018-12-19 11:32
     **/
    @Component
    public class Producer {
    
        private static final Logger logger = LoggerFactory.getLogger(Producer.class);
    
        @Autowired
        private JmsMessagingTemplate messagingTemplate;
    
    
        public void sendMessage(Destination destination,String text){
            messagingTemplate.convertAndSend(destination,text);
        }
    
    }

    Consumer

    package com.dc.sb.web.activemq;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jms.annotation.JmsListener;
    import org.springframework.jms.core.JmsMessagingTemplate;
    import org.springframework.stereotype.Component;
    
    /**
     * @author DUCHONG
     * @since 2018-12-19 11:44
     **/
    @Component
    public class Consumer {
    
        private static final Logger logger = LoggerFactory.getLogger(Consumer.class);
    
        @Autowired
        private JmsMessagingTemplate messagingTemplate;
    
        @JmsListener(destination = "helloQueue")
        public void receiveMessage(String text){
            System.out.println("Listener....receive msg....."+text);
        }
    }

    Controller

    package com.dc.sb.web.activemq;
    
    import org.apache.activemq.command.ActiveMQQueue;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.jms.Destination;
    
    /**
     * @author DUCHONG
     * @since 2018-12-19 13:57
     **/
    
    @RestController
    public class ActiveMQController {
    
        private static final Logger logger = LoggerFactory.getLogger(ActiveMQController.class);
    
        @Autowired
        private Producer producer;
    
        @RequestMapping("/send/msg")
        public String sendQueueMsg(){
            Destination destination=new ActiveMQQueue("helloQueue");
            String text="Hello ActiveMQ!";
    
            for (int i=0;i<6;i++){
                producer.sendMessage(destination,text);
            }
    
            return "send over";
        }
    
    }

    四、结果

    github地址

  • 相关阅读:
    二分查找和线性查找(二)
    自定义数组java(一)
    MATLAB中load和imread的读取方式区别
    图像Demosaic算法及其matlab实现
    Matlab GUI学习总结
    matlab gui matlab gui 鼠标点击显示图像颜色值
    【图像处理】RGB Bayer Color分析
    MATLAB图像处理_Bayer图像处理 & RGB Bayer Color分析
    IPC图像处理项目流程图
    matlab图像处理程序大集合
  • 原文地址:https://www.cnblogs.com/geekdc/p/10143971.html
Copyright © 2011-2022 走看看