Spring如何限制消息侦听器中的重试次数

Spring如何限制消息侦听器中的重试次数,spring,jms,spring-jms,Spring,Jms,Spring Jms,我正在使用spring DefaultJmsListenerContainerFactory和annotation@JmsListener(destination=“test.dev.1”)侦听队列上的消息。我已将确认模式设置为Session.CLIENT\u acknowledge,因此,如果在消息处理过程中出现任何异常,则消息将重新传递。但是,我想限制邮件重新传递的次数(重试)?我该怎么做 以下是我为DefaultJmsListenerContainerFactory编写的代码: @

我正在使用spring DefaultJmsListenerContainerFactory和annotation@JmsListener(destination=“test.dev.1”)侦听队列上的消息。我已将确认模式设置为Session.CLIENT\u acknowledge,因此,如果在消息处理过程中出现任何异常,则消息将重新传递。但是,我想限制邮件重新传递的次数(重试)?我该怎么做

以下是我为DefaultJmsListenerContainerFactory编写的代码:

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

    return factory;
}

这不是JMS规范的一部分;它有时可配置为代理上的策略。

有点过时的解决方案仅适用于当您在代理端没有控制权且仍希望在侦听器应用程序中处理此问题时,您可以通过标头(即correlationId或jmsID)来识别消息,现在,您必须设置一个逻辑,如果指定的唯一头值已传递了一定时间,则丢弃消息或将其记录在某个位置以供参考

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