SpringBoot整合ActiveMQ

本文详细介绍了如何在SpringBoot项目中整合ActiveMQ,包括依赖配置、消息生产和消费的实现,以及如何配置主题和队列,最后展示了如何发送Map消息和使用外部消息服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot整合ActiveMQ

1.第一步 加入依赖配置,也即Maven坐标

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

2.第二步 由于SpringBoot内置了消息服务,所以可以直接测试了

(1)生产消息

@RestController
public class QueueController {
	
	@Autowired
	private JmsMessagingTemplate jmsMessagingTemplate;

	@RequestMapping("/quenen")
	public void quenen(String text) {
		jmsMessagingTemplate.convertAndSend("hgx", text);
	}

	@RequestMapping("/topic")
	public void topic(String text) {
		jmsMessagingTemplate.convertAndSend("hgx2", text);
	}
	
	
}

(2)消费消息

@Component
public class Consumer {
	@JmsListener(destination = "hgx")
	public void readMessage(String text) {
		System.out.println("接收到消息:" + text);
	}
	
	@JmsListener(destination = "hgx")
	public void readMessage2(String text) {
		System.out.println("接收到消息2:" + text);
	}
	
	@JmsListener(destination = "hgx2")
	public void topic(String text) {
		System.out.println("接收到消息2topic:" + text);
	}
}

3.SpringBoot整合ActiveMQ默认发送quenen(即点对点形式),如果要发送Topic(发布订阅消息形式),需要配置以下信息

spring.jms.pub-sub-domain=true

4.效果如下

(1)点对点
在这里插入图片描述

(2)发布订阅

在这里插入图片描述

5.发送Map消息

	@RequestMapping("/sendmap")
	public void sendMap(){
		Map map=new HashMap<>();
		map.put("mobile", "13900001111");
		map.put("content", "恭喜获得10元代金券");		
		jmsMessagingTemplate.convertAndSend("itcast_map",map);
	}

	@JmsListener(destination="itcast_map")
	public void readMap(Map map){
		System.out.println(map);		
	}

6.使用外部消息服务

需指定如下配置

spring.activemq.broker-url=tcp://192.168.25.135:61616
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值