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
相关推荐
kunicoding4 分钟前
多线程编程核心概念详解:从线程基础到并发安全
java·jvm·安全
snow@li25 分钟前
SpringBoot:核心注解全景梳理 / @xxx 叫“注解”(Annotation)
java·spring
devilnumber29 分钟前
避坑_IDEA 提取方法(Extract Method)工具
java·ide·intellij-idea
鬼手点金43 分钟前
Agent 的学习机制:后训练、上下文学习与外部化学习
java·人工智能·git·学习·自动化·nanogpt
小陈不好吃1 小时前
equals 与 hashCode 的契约关系:从 HashMap 到 Lombok 注解
java·hashmap·lombok
tianbin9111 小时前
大模型训练与推理:API中转站在企业AI落地中的隐藏价值
大数据·后端·ai
xiaogai_gai1 小时前
金蝶收料通知单查询策略实战教程:从源系统拉取到中间层落地的完整路径
java·前端·数据库
打工仔折腾 AI1 小时前
用 Dashlet 把极空间 NAS 服务入口整理成统一导航首页的实践
java·开发语言·人工智能·后端·python·rpc·ai agent 实战
Song_RsHt2 小时前
Gateway(下):过滤器与统一鉴权
spring boot·spring·gateway