java - 如何为 ActiveMQ 队列创建 Spring Boot 消费者?
问题描述
我正在学习 ActiveMQ,到目前为止,我已经制作了一个简单的 Spring Boot 生产者+消费者应用程序(出于本问题的目的,将其称为App1 ),它与 ActiveMQ 的本地实例通信并且一切都按预期工作。
现在我正在尝试运行一个不同的 Spring Boot 应用程序(在同一台计算机上,但在确保App1没有运行之后),它只有一个消费者(没有生产者),但是当我启动这个应用程序时,队列中的消息(我使用修改后的App1,我在其中删除了应用程序的消费者部分)不会被拾取。在App1中,消息一发布,消费者就打印出 system.out 打印语句,但在这个仅限消费者的应用程序中并非如此。下面是我的监听器组件类:
package com.demo.listener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class Consumer {
@JmsListener(destination = "testqueue")
public void consume(String message) {
System.out.println("Picked up message: " + message);
}
}
为了实现所需的行为,我需要进行哪些更改?
应用 application.properties
程序 1文件:
spring.activemq.in-memory=false
spring.activemq.pool.enabled=false
server.port=9000
activemq.broker-url=tcp://localhost:61616
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
security.basic.enabled=false
management.security.enabled=false
App1 JmsConfig 类
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsTemplate;
@Configuration
public class JmsConfig {
@Value("${activemq.broker-url}")
private String brokerUrl;
@Bean
public Queue queue() {
return new ActiveMQQueue("testqueue");
}
@Bean
public ActiveMQConnectionFactory activeMQConnectionFactory() {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
factory.setBrokerURL(brokerUrl);
return factory;
}
@Bean
public JmsTemplate jmsTemplate() {
return new JmsTemplate(activeMQConnectionFactory());
}
}
App1生产者类
import javax.jms.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/rest/publish")
public class ProducerResource {
@Autowired
JmsTemplate jmsTemplate;
@Autowired
Queue queue;
@GetMapping("/{message}")
public String publishMessage(@PathVariable("message") final String message) {
jmsTemplate.convertAndSend(queue, message);
return "Published successfully";
}
}
App1消费者类与我在仅消费者应用程序(如上所列)中使用的类相同。
解决方案
对于您的消费者应用程序,您确实需要为您的消费者 JMS 模板添加池连接工厂和 JMS 消息侦听器工厂以开始接收消息。
@Configuration
@EnableJms
public class ConsumerConfig {
@Value("${activemqbrokerurl}")
private String brokerUrl;
@Bean
public ActiveMQConnectionFactory activeMQConnectionFactory() {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
activeMQConnectionFactory.setBrokerURL(brokerUrl);
return activeMQConnectionFactory;
}
@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(activeMQConnectionFactory());
factory.setConcurrency("{#setDesiredConcurrency}");
return factory;
}
}
Spring 的 MessagListenerContainer 应该用于消息消费。这提供了 MDB 的所有功能——高效的 JMS 使用和消息侦听器的池化——但不需要完整的 EJB 容器。
您可以使用 activemq-pool org.apache.activemq.pool.PooledConnectionFactory 为您的消费者集合有效地汇集连接和会话,或者您可以使用 Spring JMS org.springframework.jms.connection.CachingConnectionFactory 来实现相同的目的影响。
你可以在这里阅读更多关于它的信息
推荐阅读
- python - 何时使用 SQLalchemy 核心与 SQLalchemy ORM?
- java - super.onBackPressed(); 导致图像返回重复,因为我调用了两次。我该如何重写它来解决这个问题?
- python-3.x - 将相似用户划分为社交组
- android - 如何使用 Kotlin 从 Android 中的片段访问另一个片段?
- bash - 使用插入的Postgres文本到数组文本
- python-3.x - 为什么 TCS MockVita 显示此 python 代码的运行时错误
- javascript - Geeting 下拉选项,顶部有建议
- javascript - 使用chartjs在条上拖动时缩放不起作用
- pycharm - 如何像 Spyder/Matlab 一样布局 pycharm?
- c++builder - C ++ Builder包含来自其他文件夹的头文件