如何让 OpenMQ 重新传送消息?

How do I get OpenMQ to redeliver messages?

我异步监听了一个 OpenMQ 队列。如果我在消费一条消息的过程中出现了异常,有没有办法让OpenMQ重新把那条消息推送给我?

@Bean
public JmsListenerContainerFactory jmsQueueListenerContainerFactory() {
    DefaultJmsListenerContainerFactory jmsListenerContainerFactory = new DefaultJmsListenerContainerFactory();
    jmsListenerContainerFactory.setConnectionFactory(connectionFactory());
    jmsListenerContainerFactory.setPubSubDomain(false);
    jmsListenerContainerFactory.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
    return jmsListenerContainerFactory;
}
MessageConsumer receiver = session.createConsumer(destination);
receiver.setMessageListener(new MessageListener() {
    public void onMessage(Message message) {
        TextMessage text = (TextMessage) message;
        System.out.println("Received message: " + message.getText());

        //The connection timed out when saving the message to the database
        repository.save(text);
    }
});

未退出的原因是您已确认模式设置为 AUTO_ACKNOWLEDGE

jmsListenerContainerFactory.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);

将其更改为 CLIENT_ACKNOWLEDGE,如下所示:

jmsListenerContainerFactory.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);

使用 message.acknowledge() 提交消息。

使用 session.recover() 撤消消息。