zoukankan      html  css  js  c++  java
  • Springboot 2.2.1 与activeMq 集成2 queue 消息

    生产者代码

    package com.active.mq.service;
    
    import javax.jms.Destination;
    /**
     * 消息生产
     *
     * @author kql
     *
     */
    public interface ProducerService {
        /**
         * 使用指定消息队列发送
         * 
         * @param destination
         * @param message
         */
        void sendMsg(Destination destination, final String message);
    
    
        /**
         * 使用已经配置好的消息队列发送消息  就是启动类配置的队列
         * @param message
         */
         void sendMessage(final String message);
    
    
        
    }
    package com.active.mq.service.impl;
    
    import com.active.mq.service.ProducerService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jms.core.JmsMessagingTemplate;
    import org.springframework.stereotype.Service;
    
    import javax.jms.Destination;
    import javax.jms.Queue;
    import javax.jms.Topic;
    
    /**
     * 消息生产实现类
     *
     * @author kql
     */
    @Service
    public class ProducerServiceImpl implements ProducerService {
        @Autowired
        private JmsMessagingTemplate jmsTemplate;
    
        @Autowired
        private Queue queue;
    
        @Override
        public void sendMsg(Destination destination, String message) {
    
            jmsTemplate.convertAndSend(destination, message);
        }
    
        @Override
        public void sendMessage(String message) {
            jmsTemplate.convertAndSend(this.queue, message);
    
        }
    
    
    
    
    }
    package com.active.mq.controller;
    
    import com.active.mq.service.ProducerService;
    import org.apache.activemq.command.ActiveMQQueue;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.jms.Destination;
    
    /**
     * 消息生产类
     * @author  kql
     */
    @RestController
    public class OrderController {
        @Autowired
        private ProducerService producerService;
    
        @GetMapping("/order")
        private String order(String msg) {
            Destination destination = new ActiveMQQueue("order.queue");
            producerService.sendMsg(destination, msg);
            return "order";
        }
    
        @GetMapping("common")
        public String common(String msg){
            producerService.sendMessage(msg);
            return "common";
        }
    
       
    
    
    }

    //消费者监听

    package com.active.mq.listener;
    
    import org.springframework.jms.annotation.JmsListener;
    import org.springframework.stereotype.Component;
    
    /**
     * 监听
     * @author kql
     */
    @Component
    public class OrderConsumer {
        /**
         * 监听指定消息队列
         *
         * 队列为: order.queue  的消息
         *
         *
         * @param text
         */
        @JmsListener(destination = "order.queue")
        public void receiveQueue(String text) {
            System.out.println("[ OrderConsumer收到的报文 : " + text + " ]");
        }
    
        /**
         * 监听指定消息队列
         *
         * 队列为: test.queue 的消息
         *
         *
         * @param text
         */
        @JmsListener(destination = "test.queue")
        public void receiveQueue1(String text) {
            System.out.println("[ OrderConsumer1收到的报文 : " + text + " ]");
        }
    
    }

    //两种生产测试

    http://localhost:8080/common?msg=111

    http://localhost:8080/order?msg=222

  • 相关阅读:
    协程-Greenlet
    协程简介-异步IO
    关于__name__=='__main__
    进程池-限制同一时间在CPU上运行的进程数
    STM32丰富的资料网站(库函数手册)
    STM32CubeMX入门之点亮板载LED
    STM32CUBEMX忘记配置sys中的debug导致程序只能下载一次的问题
    GX Works2 存储器空间或桌面堆栈不足 解决方案
    Lua安装报错:编译 Lua 报错:error: readline/readline.h: No such file or directory 问题解决办法
    Ubuntu18.04不能联网问题
  • 原文地址:https://www.cnblogs.com/woshuaile/p/11907338.html
Copyright © 2011-2022 走看看