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
相关推荐
CodeAllen嵌入式1 天前
嵌入式C/C++:栈与堆内存管理
java·c语言·c++
李姆斯1 天前
技术方案评审没人听?别人抓不住重点?你不妨这样做!
前端·后端·面试
毕设源码-钟学长1 天前
【开题答辩全过程】以 基于springboot的健身房ERP系统设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
To Be Clean Coder1 天前
【Spring源码】getBean源码实战(七)——BeanPostProcessor与初始化方法
java·后端·spring
汪小成1 天前
Go CLI 入口设计:参数解析、错误处理与项目分层实战
后端·go
+VX:Fegn08951 天前
计算机毕业设计|基于springboot + vue民宿平台管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·课程设计
窗边鸟1 天前
小白日记之Java循环结构
java
毕设源码-赖学姐1 天前
【开题答辩全过程】以 高校教学管理系统的设计与实现为例,包含答辩的问题和答案
java
superman超哥1 天前
Rust 复制语义(Copy Trait)与移动语义的区别:类型系统的精确控制
开发语言·后端·rust·编程语言·移动语义·rust复制语义·copy trait
怒放吧德德1 天前
RocketMQ从实战到源码:初识RocketMQ
java·后端·rocketmq