adolzi
adolzi

Reputation: 691

Redeliver message to MQ when using @JmsListener

I'm using @EnableJms and @JmsListener annotation to register a queue listener in my application, basing on this tutorial. I'm connecting to IBM MQ, getting connection factory using jndi. I have read about acknowledge modes etc. but still it's a new thing to me. And my problem is that the message is not being returned to a queue (the listener is never called again).

@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
    DefaultJmsListenerContainerFactory factory 
      = new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory());
    factory.setSessionTransacted(true);
    factory.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE); //I have tried also CLIENT_ACKNOWLEDGE
    return factory;
}


@JmsListener(containerFactory = "jmsListenerContainerFactory", destination = "myQueue")
@SendTo("secondQueue")
public String testListener(String message){
 if(true) throw new NullPointerException();
 else return message;
}

Any help would be much appreciated. I would have also a second question. From what I understand if I would like to implement any operation on a database, only way to rollback a commit (if something went wrong after this) would be to create a transaction manager? If not, I would need to detect a duplicated message.

Upvotes: 0

Views: 3335

Answers (2)

Ch Bharatkumar
Ch Bharatkumar

Reputation: 11

First set the acknowledgement mode to Session.CLIENT_ACKNOWLEDGE and when receiving the messages, if it's processed correctly then just call message.acknowledge() method or else don't call.

It will automatically stay in the queue and you don't need to resend it.

You need to use

import javax.jms.Message

Upvotes: 1

artild
artild

Reputation: 380

I created simple Spring Boot app and Docker container of IBM MQ to test your case. I found good instructions in this tutorial: https://developer.ibm.com/tutorials/mq-jms-application-development-with-spring-boot/

And in your case this environment behaves as expected: endless cycle of receive message -> NullPointerException -> return message -> ...

Than I found feature of IBM MQ called "Backout Queues & Thresholds", you'll found the explanation in this blog post: https://community.ibm.com/community/user/imwuc/browse/blogs/blogviewer?BlogKey=28814801-083d-4c80-be5f-90aaaf81cdfb

Briefly, it is possible to restrict number of times message returned to queue after exception, and after this limit send message to another queue.

May be in your case this feature used on your destination queue.

Upvotes: 0

Related Questions