SpringBoot整合ActiveMQ

SpringBoot整合ActiveMQ

文章目录

下载与安装

https://activemq.apache.org/activemq-5016003-release

下载后解压即可

SpringBoot整合ActiveMQ

导坐标

java 复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>

改配置,默认的保存位置

java 复制代码
spring:
  activemq:
    broker-url: tcp://localhost:61616
  jms:
    template:
      default-destination: itheima

生产与消费消息

java 复制代码
@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;
    }
}

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

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

java 复制代码
@Component
public class MessageListener {

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

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

所以改造一下我们的监听器

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

java 复制代码
@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. 发布订阅
    目前使用的是点对点的模型,要想更换,做一个配置pub-sub-domain: true
java 复制代码
server:
  port: 80

spring:
  activemq:
    broker-url: tcp://localhost:61616
  jms:
    template:
      default-destination: itheima
    
    pub-sub-domain: true
相关推荐
DKPT2 分钟前
Java设计模式之行为型模式(责任链模式)介绍与说明
java·笔记·学习·观察者模式·设计模式
L_autinue_Star18 分钟前
手写vector容器:C++模板实战指南(从0到1掌握泛型编程)
java·c语言·开发语言·c++·学习·stl
王中阳Go28 分钟前
面试完第一反应是想笑
后端·go
晨岳31 分钟前
CentOS 安装 JDK+ NGINX+ Tomcat + Redis + MySQL搭建项目环境
java·redis·mysql·nginx·centos·tomcat
执笔诉情殇〆38 分钟前
前后端分离(java) 和 Nginx在服务器上的完整部署方案(redis、minio)
java·服务器·redis·nginx·minio
YuTaoShao42 分钟前
【LeetCode 热题 100】24. 两两交换链表中的节点——(解法一)迭代+哨兵
java·算法·leetcode·链表
Livingbody1 小时前
10分钟实现基于Ubuntu25.04本地推理ERNIE模型
后端
神仙别闹1 小时前
基于ASP.NET+SQL Server实现(Web)企业进销存管理系统
前端·后端·asp.net
5171 小时前
django中如何使用Django REST Framework
后端·python·django
程序员的世界你不懂1 小时前
(20)Java+Playwright自动化测试- 操作鼠标拖拽 - 上篇
java·python·计算机外设