本文介绍: 监听器收到消息后,不仅可以消费消息,还可以将当前方法返回值,在转发给下一个消息队列消息怎么会手工点击消费呢,哈哈哈,肯定是收到消息赶紧处理,必须交给机器处理啦!所以改造一下我们监听器。jms消息模型有两种。

SpringBoot整合ActiveMQ

下载安装

https://activemq.apache.org/activemq-5016003-release
下载解压即可
在这里插入图片描述
在这里插入图片描述

SpringBoot整合ActiveMQ

坐标

        <dependency>
            &lt;groupId&gt;org.springframework.boot</groupId&gt;
            <artifactId&gt;spring-boot-starter-activemq</artifactId&gt;
        </dependency&gt;

配置默认保存位置

spring:
  activemq:
    broker-url: tcp://localhost:61616
  jms:
    template:
      default-destination: itheima

生产与消费消息

在这里插入图片描述

@Service
public class MessageServiceActivemqImpl implements MessageService {

    @Autowired
    private JmsMessagingTemplate messagingTemplate;
    @Override
    public void sendMessage(String id) {
        System.out.println("待发送短信订单已经纳入队列id=" + id);
        messagingTemplate.convertAndSend("order.queue.id",id);
        // 发消息时候设定存储位置
    }

    @Override
    public String doMessage() {
        String id = messagingTemplate.receiveAndConvert("order.queue.id",String.class);
        System.out.println("已完成短信发送业务,id:"+id);
        return id;
    }
}

消息怎么会手工点击消费呢,哈哈哈,肯定是收到消息赶紧处理,必须交给机器处理啦!

实现监听类——实现消息自动消费

在这里插入图片描述

@Component
public class MessageListener {

    @JmsListener(destination = "order.queue.id")
    public void reveive(String id){
        System.out.println("已完成短信发送业务,id:"+id);
    }
}

监听器收到消息后,不仅可以消费消息,还可以将当前方法返回值,在转发给下一个消息队列
所以改造一下我们监听

监听转发消息:流程业务消息消费完转入下一个消息队列

在这里插入图片描述

@Component
public class MessageListener {

    @JmsListener(destination = "order.queue.id")
    @SendTo("order.other.queue.id")
    public String reveive(String id){
        System.out.println("已完成短信发送业务,id:"+id);
        return "new:" + id;
    }
}

jms的消息模型有两种

  1. 点对点
  2. 发布订阅
    目前使用的是点对点的模型,要想更换,做一个配置pubsubdomain: true
server:
  port: 80

spring:
  activemq:
    broker-url: tcp://localhost:61616
  jms:
    template:
      default-destination: itheima
    
    pub-sub-domain: true

原文地址:https://blog.csdn.net/shall_zhao/article/details/134755155

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_32250.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注