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
相关推荐
蓝染-惣右介22 分钟前
【若依RuoYi-Vue | 项目实战】帝可得后台管理系统(二)
java·前端·后端·vue·springboot
齐 飞26 分钟前
Java接口和抽象类的区别
java·笔记·后端
Xiu Yan27 分钟前
LeetcodeTop100 刷题总结(一)
java·数据结构·算法·链表·矩阵·哈希算法·数组
Rainyocode33 分钟前
Java为什么不直接实现Iterator接口,而是实现Iterable?
java
麻木的根号三33 分钟前
【C++初阶】探索STL之——vector
java·开发语言·c++
打鱼又晒网33 分钟前
你了解system V的ipc底层如何设计的吗?消息队列互相通信的原理是什么呢?是否经常将信号量和信号混淆呢?——问题详解
linux·运维·服务器·后端·操作系统
小羊瑞士37 分钟前
代理模式---静态代理和动态代理
java·代理模式
(⊙o⊙)~哦43 分钟前
spring boot 定时器配置
java·spring boot·后端
C—328G1 小时前
flask
后端·python·flask
伯牙碎琴1 小时前
八、TOGAF(架构治理Architecture Governance)
java·微服务·架构