springjmsopenmqopenjms

How do I get OpenMQ to redeliver messages?


I listened to an OpenMQ queue asynchronously. If I have an exception in the process of consuming a message, is there a way to get OpenMQ to push that message to me again?

@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);
    }
});

Solution

  • The reason it is not backed out is that you have acknowledged mode set to AUTO_ACKNOWLEDGE.

    jmsListenerContainerFactory.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
    

    Change this to CLIENT_ACKNOWLEDGE like below:

    jmsListenerContainerFactory.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
    

    Use message.acknowledge() to commit the message.

    Use session.recover() to back out the message.