suraj bahl
suraj bahl

Reputation: 3296

Spring How to Limit number of retry in Message listener

I am using spring DefaultJmsListenerContainerFactory and annotation @JmsListener(destination="test.dev.1") to listen the message on a queue. I have set acknowledge mode to Session.CLIENT_ACKNOWLEDGE so if any exception occurs during message processing then message is redelivered. However i want to limit the number of times message is redelivered (retry)? how can i do that?

Here is my code for DefaultJmsListenerContainerFactory:

    @Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory () {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(jmsConnectionFactory());
    factory.setConcurrency("1");
    factory.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);

    return factory;
}

Upvotes: 2

Views: 10567

Answers (3)

Prabin
Prabin

Reputation: 29

    @JmsListener(destination = "${receiveQueue}", containerFactory = "myFactory")
        public void receiveMessage(Message message, String msg) throws Exception {

            try {

                // start processing the message. I have string message(msg)
               //Message object hold the header with more information about the message


                //throw new Exception("Exception has occoured while trying to process the message : " + msg);
            } catch (Exception e) {

               //in message object, WMQConstants.JMSX_DELIVERY_COUNT hold the count for, how many times the message has been retried
                int currentAttemptCount = message.getIntProperty(WMQConstants.JMSX_DELIVERY_COUNT);
                logger.debug("Current attempt count : "+currentAttemptCount);
                if (currentAttemptCount >= MAX_ATTEMPTS) {
                    logger.error("Max attampt for retry has reached for Message : \n" + message);
                    //Logger message
                    System.exit(0);
                } 
               //else throw exception. it will requeue message.
                throw e;
            }
        }

Upvotes: 0

Shek
Shek

Reputation: 1613

Somewhat out there solution only applicable when you have no control on broker side and you still want to handle this in your listener app - You can identify the messages by headers i,e correlationId or jmsID, now you have to set a logic where if the specified unique header value(s) has been delivered for certain amount of time then discard the message or log it somewhere for reference.

Upvotes: 0

Gary Russell
Gary Russell

Reputation: 174554

That is not part of the JMS specification; it is sometimes configurable as a policy on the broker.

Upvotes: 2

Related Questions