gpt4 book ai didi

spring-boot - 如何在 Spring Boot 中从 ActiveMQ 队列中读取未决消息

转载 作者:行者123 更新时间:2023-12-04 01:35:58 24 4
gpt4 key购买 nike

我喜欢使用 Spring 引导读取 ActiveMQ 队列中的未决(未确认)消息。怎么做?

到目前为止,我可以在消息发送到队列时读取消息:

@JmsListener(destination = "LOCAL.TEST", 
containerFactory = "myJmsListenerContainerFactory")
public void receiveMessage(final Message jsonMessage) throws JMSException {
String messageData = null;
// jsonMessage.acknowledge(); // dont consume message (for testing)
LOGGER.info("=== Received message {}", jsonMessage);
}

为 mq 连接使用标准配置:

@Bean
public ActiveMQConnectionFactory getActiveMQConnectionFactory() {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
activeMQConnectionFactory.setBrokerURL(BROKER_URL + ":" + BROKER_PORT);
return activeMQConnectionFactory;
}

和一个标准的 ListenerContainerFactory:

@Bean
public DefaultJmsListenerContainerFactory myJmsListenerContainerFactory() {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(getActiveMQConnectionFactory());
factory.setConcurrency("1-1");
return factory;
}

但是如果我使用

手动发送一条消息,这只会记录一条消息
@Autowired
private JmsTemplate jmsTemplate;

public void send(String destination, String message) {
LOGGER.info("sending message='{}' to destination='{}'", message, destination);
jmsTemplate.convertAndSend(destination, message);
}

使用标准模板

@Bean
public JmsTemplate jmsTemplate() {
JmsTemplate template = new JmsTemplate();
template.setConnectionFactory(getActiveMQConnectionFactory());
return template;
}

我无法读取之前发送的仍在队列中的消息(因为我没有 .acknowledge() 它们)...

最佳答案

JMS supports "browsing" messages这似乎是您想要的功能。因此,您应该更改您的 Spring 应用程序以使用 QueueBrowser 而不是实际使用消息。

关于spring-boot - 如何在 Spring Boot 中从 ActiveMQ 队列中读取未决消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49816837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com