Springboot 2.2.1 与activeMq 集成2 topic 发布者,订阅者

在ProducerService接口中,增加下面这个方法

/**
* 功能描述:消息发布者
* @param msg
*/
void publish(String msg);

在实现类:ProducerServiceImpl 添加:
//=======发布订阅相关代码=========

@Autowired
private Topic topic;


@Override
public void publish(String msg) {
jmsTemplate.convertAndSend(topic, msg);
}


controller中:OrderController

@GetMapping("publish")
public String publish(String msg){
producerService.publish(msg);
return "publish";
}






//订阅者 监听发布
package com.active.mq.listener;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 * 订阅者监听
 *
 * @author kql
 */
@Component
public class TopicSub {
//这里指定监听的消息 video.topic 也可自定义监听

    @JmsListener(destination = "video.topic", containerFactory = "jmsListenerContainerTopic")
    public void receive1(String text) {
        System.out.println("video.topic 消费者:receive1=" + text);
    }


    @JmsListener(destination = "video.topic", containerFactory = "jmsListenerContainerTopic")
    public void receive2(String text) {
        System.out.println("video.topic 消费者:receive2=" + text);
    }


    @JmsListener(destination = "video.topic", containerFactory = "jmsListenerContainerTopic")
    public void receive3(String text) {
        System.out.println("video.topic 消费者:receive3=" + text);
    }


}

http://localhost:8080/publish?msg=订阅  

猜你喜欢

转载自www.cnblogs.com/woshuaile/p/11907361.html