简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM