1

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);
    }
});
Justin Bertram
  • 29,372
  • 4
  • 21
  • 43
winston
  • 37
  • 6
  • What is the acknowledge mode of the session? – JoshMc Mar 13 '20 at 02:16
  • Thanks for reminding. I change the acknowledge to `CLIENT_ACKNOWLEDGE`. And invoke message.acknowledge() when process success, and session.recover() when exception. This will fulfill my purpose. However, I encountered a new problem, I did not find how to set the maximum number of resends, and the interval time. @JoshMc – winston Mar 13 '20 at 08:59
  • Hi Winston, if you would please edit your question to add the code showing the session setup with the original non-working acknowledge mode (I assume `AUTO_ACKNOWLEDGE`), then I think I can write a quick answer so you can give credit for the help I provided you in correcting the problem that your question was written about. – JoshMc Mar 13 '20 at 20:30
  • As for the question on how to set the maximum number of resends and interval time, this is a different question and this site is one question one answer per post, so it is better for you to add a new question on that subject. – JoshMc Mar 13 '20 at 20:31
  • @JoshMc Ok, thanks. – winston Mar 14 '20 at 01:22
  • Please edit your question to add the code showing the session setup with the original non-working acknowledge mode (I assume AUTO_ACKNOWLEDGE). That way my answer will make since. – JoshMc Mar 14 '20 at 06:25

1 Answers1

1

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.

JoshMc
  • 10,239
  • 2
  • 19
  • 38