spring boot ActiveMQ 支持topic和队列
1、mq 配置
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.core.JmsMessagingTemplate;
import javax.jms.ConnectionFactory;
@Configuration
@EnableJms
public class MqConfig {
@SuppressWarnings("SpringJavaAutowiringInspection") //加这个注解让IDE 不报: Could not autowire
@Autowired
private ConnectionFactory connectionFactory;
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerTopic( ) {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setPubSubDomain(true);
bean.setConnectionFactory(connectionFactory);
return bean;
}
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerQueue( ) {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setConnectionFactory(connectionFactory);
return bean;
}
@Bean
public JmsMessagingTemplate jmsMessagingTemplate( ){
return new JmsMessagingTemplate(connectionFactory);
}
1、mq 消费者
@JmsListener(destination = "test.queue", containerFactory = "jmsListenerContainerQueue")
public void receiveQueueTest(String text) {
log.info("receiveQueueTest->"+text);
System.out.println("receiveQueueTestConsumer收到的报文为:"+text);
}
@JmsListener(destination = "test.topic",containerFactory = "jmsListenerContainerTopic")
public void receiveTopicTest(String text) {
log.info("receiveTopicTest->"+text);
System.out.println("receiveQueueTestConsumer收到的报文为:"+text);
}
2、生成者
@Service("producer")
public class MqProducer {
@Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private JmsTemplate jmsTemplate;
// 发送消息,destination是发送到的队列,message是待发送的消息
public void sendMessage(final String destination, final String message) {
jmsMessagingTemplate.convertAndSend(destination, message);
}
public void sendMessageQueue(final String queue, final String message) {
Destination destination = new ActiveMQQueue(queue);
jmsMessagingTemplate.convertAndSend(destination, message);
// jmsTemplate.send(destination, new MessageCreator() {
//
// public Message createMessage(Session session) throws JMSException {
//// ObjectMessage objMessage = session.createObjectMessage(message);
// TextMessage textMessage = session.createTextMessage(message);
// return textMessage;
// }
//
// });
}
public void sendMessageTopic(final String topic, final String message) {
Destination destination = new ActiveMQTopic(topic);
jmsMessagingTemplate.convertAndSend(destination,message);
// jmsTemplate.send(destination, new MessageCreator() {
// public Message createMessage(Session session) throws JMSException {
//// ObjectMessage objMessage = session.createObjectMessage(message);
//// return objMessage;
// TextMessage textMessage = session.createTextMessage(message);
// return textMessage;
// }
// });
}
}
分享到:
相关推荐
通过这个示例,你可以了解到如何在Spring Boot应用中集成ActiveMQ,使用Queue和Topic进行消息传递,并了解连接池和消息确认机制。这些技术在分布式系统、微服务架构中广泛用于实现异步通信和解耦。
本项目基于Spring boot这一平台,整合流行的开源消息队列中间件ActiveMQ,实现一个向ActiveMQ添加和读取消息的功能。分别实现生产者-消费者模式和发布-订阅模式,作为java编程发送消息和消费消息的基础示例。 源码...
在本示例中,我们将探讨如何使用Maven构建项目,结合Spring框架来实现ActiveMQ的Topic队列模式。 Topic队列模式是ActiveMQ中的一种发布/订阅模型。与点对点(Queue)模型不同,Topic模式允许多个消费者同时订阅同一...
总结,通过Spring Boot与ActiveMQ的整合,我们可以轻松实现消息队列和主题的创建、消息的手动确认以及重发机制,从而构建健壮的分布式系统。在实际应用中,可以根据业务需求调整配置,以达到最佳的性能和可靠性。
1. **添加依赖**:在`pom.xml`文件中引入ActiveMQ的Spring Boot starter依赖: ```xml <groupId>org.springframework.boot <artifactId>spring-boot-starter-activemq ``` 2. **配置ActiveMQ**:在`...
- 结合Spring Boot的自动配置能力,可以极大地简化ActiveMQ的集成过程,只需少量配置就能快速启动和运行。 7. **监控和管理**: - ActiveMQ提供了Web控制台,可以通过浏览器进行监控和管理,查看队列状态、消息...
Spring Boot 2.5已经提供了对ActiveMQ的支持,因此我们可以使用Spring Boot的starter artifact来简化配置,例如添加`spring-boot-starter-artemis`或`spring-boot-starter-jms`依赖。 接下来,我们需要配置ActiveMQ...
本文将深入探讨如何将Spring与ActiveMQ进行整合,以提升系统的可扩展性和解耦性。 一、Spring与ActiveMQ整合基础 1. **消息中间件概念**:消息中间件是一种软件,它在不同的应用之间传递消息,实现了应用之间的...
如果使用Spring Boot,可以简化配置,只需在`application.properties`中添加ActiveMQ的相关配置,如`spring.activemq.broker-url`,然后通过`@Autowired`注解注入`JmsTemplate`和`ConnectionFactory`即可。...
在Spring Boot应用中整合ActiveMQ和WebSocket,可以创建一个实时通信系统,使后端服务能够高效地推送消息到前端客户端。以下将详细解释这个过程的关键知识点: 1. **ActiveMQ**:Apache ActiveMQ是一个开源的消息...
此外,ActiveMQ支持多种协议和特性,如topic、持久化、事务消息等,可以根据项目需求进一步探索和利用。 这个简单的Demo展示了如何在Spring Boot中集成ActiveMQ进行消息接收。通过这种方式,你可以构建出一个可靠的...
8. **Spring Boot与ActiveMQ**:在Spring Boot应用中,可以利用 starters 来简化配置,通过`spring.activemq.broker-url`和`spring.activemq.user`等属性快速设置ActiveMQ连接。 9. **消息确认**:ActiveMQ支持两种...
ActiveMQ是Apache软件基金会开发的一款开源消息中间件,它遵循Java Message Service (JMS) 规范,提供了高效、可靠的异步消息传递服务...这两个版本的实现对于学习和实践中理解消息队列和ActiveMQ的使用都非常有价值。
本例子程序的主题是"activeMQ与spring整合开发",这意味着它将展示如何在Spring应用中集成ActiveMQ,以便利用消息队列进行异步通信和解耦组件。以下是一些关键知识点: 1. **Spring集成ActiveMQ的基本配置**:在...
6. **测试与运行**:创建测试用例或启动 Spring Boot 应用,验证消息发送和接收功能是否正常。 通过以上步骤,我们就成功地将 Spring 与 ActiveMQ 整合起来,实现了简单的消息发送和接收功能。在实际应用中,还可以...
这通常包括`spring-boot-starter-activemq`和`org.apache.activemq:activemq-client`,以及可能的MQTT客户端库,如`org.eclipse.paho:org.eclipse.paho.client.mqttv3`。 2. 配置ActiveMQ:在`application....
2. **配置Spring**:创建Spring配置文件,配置JMS连接工厂、目的地(Queue或Topic)、JMS模板和监听容器。 ```xml <bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"> ...
- 在项目`pom.xml`中添加Spring Boot对ActiveMQ的支持依赖: ```xml <groupId>org.springframework.boot <artifactId>spring-boot-starter-activemq ``` 3. **手动创建连接** - 如果需要手动创建连接,...
将ActiveMQ与Spring整合,可以方便地在Spring应用中使用消息队列。 **ActiveMQ整合Spring的核心知识点:** 1. **Spring JMS支持**: Spring提供了JMS模块,通过`org.springframework.jms`包中的类和接口,简化了...
8. **监控与管理**:ActiveMQ提供了Web控制台,可以通过浏览器访问来监控消息代理的状态,查看队列和主题中的消息数量,以及进行管理操作。 9. **扩展到分布式系统**:这个Demo作为一个单项目,但其设计思路可以...