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

标签 spring-boot jms activemq spring-jms

我喜欢使用 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/

相关文章:

java - 在自定义 Spring Validator 中无法返回值

java - 使用 JMS,如何获取特定目标的最大消息大小?

java - 将 ByteMessage 转换为字符串?

jms - 具有相同容器工厂的 @jmslistener 是否共享线程池

spring-boot - 编写测试来验证 jms 监听器中收到的消息(Spring-Boot)

java - Spring 数据休息 : Can we rename the "content" property in pagination result?

java - Spring:无法加载ApplicationContext

java - 如何在Spring Boot中将父项目的gradle属性传递给logback.xml?

java - 使用 localhost VM 创建的 ActiveMQ 不会创建 JMX Bean

Apache Camel 无法创建端点