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
相关推荐
pengyu9 分钟前
【Java设计原则与模式之系统化精讲:壹】 | 编程世界的道与术(实战指导篇)
java·后端·设计模式
日月星辰Ace12 分钟前
JVM 垃圾回收简介
java
陈随易13 分钟前
一行代码,将网页元素变成图片!比 html2canvas 快 93 倍的截图神器来了!
前端·后端·程序员
Kookoos13 分钟前
性能剖析:在 ABP 框架中集成 MiniProfiler 实现性能可视化诊断
后端·c#·.net·abp vnext·miniprofiler
掉头发的王富贵15 分钟前
Arthas神器入门:动态调试Java应用,轻松搞定生产环境Bug!
java·后端·debug
汪子熙19 分钟前
解密 Fabric 体系 —— 架构与实践全解析
后端
Java陈序员20 分钟前
再见 Navicat!一款开源的 Web 数据库管理工具!
java·react.js·docker
oraen21 分钟前
一篇文章让你在根本上理解遗传算法,用牛刀杀鸡-使用遗传撕力扣
后端
程序员爱钓鱼22 分钟前
Go语言并发模型与模式:Worker Pool 模式
后端·go·排序算法
Victor35623 分钟前
MySQL(66)如何进行慢查询日志分析?
后端