Springboot中使用activemq

  1. 引入ActiveMQ的SpringBoot插件

    复制代码
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-activemq</artifactId>
         </dependency>
  2. application中增加activemq的配置

    spring:

    activemq:
    #ActiveMQ服务器地址
    broker-url: tcp://127.0.0.1:61616
    user: admin
    password: admin
    jms:
    #false == Queue ; true == Topic ; 默认为false
    pub-sub-domain: false

  3. Java代码

发送消息的代码:

java 复制代码
@RestController
public class TestController {

    @Autowired
    private JmsProducer jmsProducer;

    @GetMapping("/send")
    public String sendMessage(@RequestParam String msg) {
        jmsProducer.sendMessage("test.queue", msg);
        return "Message sent: " + msg;
    }
}

接收消息的代码:

java 复制代码
package com.tntxia.springboottest.springbootdemo.service;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;

@Service
public class JmsConsumer {

    @JmsListener(destination = "test.queue")
    public void receiveMessage(String message) {
        System.out.println("Received message: " + message);
    }
}

在浏览器输入

localhost:8080/send?msg=hello

可以发送一条消息过去

相关推荐
有个傻瓜2 小时前
分页 total 对不上、列表还少几条:MyBatis-Plus 一对多 LEFT JOIN 分页错乱复盘与四种写法实测
spring boot·sql优化·mybatis-plus·分页查询·踩坑复盘
千寻技术帮13 小时前
微信小程序+Spring Boot 烘焙商城系统(附完整源码+数据库+部署文档)50053
数据库·spring boot·微信小程序·烘焙商城
CoderYanger16 小时前
Java EE 进阶:11. SpringBoot 统一功能处理(第二弹):DispatcherServlet 源码分析+适配器模式
java·数据库·hive·spring boot·面试·java-ee·mybatis
她的男孩17 小时前
状态默认填了 DRAFT,同步后却变成 '''DRAFT'':Forge Admin 清洗 PG 的 COLUMN_DEFAULT
spring boot·低代码·postgresql
考虑考虑1 天前
记一次@Async生产事故
spring boot·后端·spring
小荷才露尖尖角,早有蜻蜓立上头1 天前
SpringBoot 定时任务
java·spring boot·后端
码云社区2 天前
Spring Boot上门按摩服务系统源码:上门到店服务目录与门店协同方案
java·spring boot·后端·上门服务·上门按摩·上门小程序
她的男孩2 天前
会签选了两个人,待办却生成一条谁也办不了:Forge Admin 按人拆开自选审批人
spring boot·低代码·工作流引擎
头发还在的女程序员2 天前
Spring Boot云快充协议接入:Netty报文分帧、长度检查与校验处理设计
java·spring boot·后端·充电桩·充电桩小程序·云快充
captain3762 天前
SpringCloud(12)-Gateway
spring boot·spring cloud·gateway