springboot整合activeMQ

本文介绍了如何在SpringBoot应用中整合ActiveMQ,包括yml文件的配置,定义消费者和生产者服务,以及模板处理类的创建,并提供了测试类进行验证。

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

active MQ的队列名称

在这里插入图片描述

1.yml文件配置

spring:
jms:
pub-sub-domain: false #配置消息类型,如果true则表示topic消息,如果false表示Queue消息
activemq:
user: admin
password: admin
broker-url: tcp://127.0.0.1:61616

2.编写消费者服务类
@Service
public class MessageConsumerService {

**@JmsListener(destination = "mldn.msg.queue")  //destination表示队列名称**
public void receiveMessage(String text) {
	//进行消息接收处理
	System.err.println("【*** 接受消息***】" + text);
}

}

3.编写生产者服务类

1)生产者接口
public interface IMessageProduceService {

public void sendMessage(String msg);

}
2)生产者实现
@Service
public class MessageProducerServiceImpl implements IMessageProduceService {

@Resource
private JmsMessagingTemplate jmsMessagingTemplate;
@Resource
private Queue queue;
@Override
public void sendMessage(String msg) {
	// TODO Auto-generated method stub
	this.jmsMessagingTemplate.convertAndSend(this.queue,msg);

}

}
4.编写模板处理类
//建立模板处理类
@Configuration
@EnableJms
public class ActiveMQConfig {

@Bean
public Queue queue() {
	return new ActiveMQQueue("mldn.msg.queue");  //传入队列名称
}

}

5.编写测试类
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestActiveMQ {

@Resource
private IMessageProduceService iMessageProduceService;

@Test
public void testSend() throws Exception{
	for(int x=0;x<100;x++) {
		this.iMessageProduceService.sendMessage("mldn- " + x);
	}
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值