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

 

 

 

posted @ 2019-11-21 18:23  夏风中的Young_Uncle  阅读(411)  评论(0编辑  收藏  举报