View Javadoc

1   /*
2    * $Id: JmsXRedeliveryHandler.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.jms;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.MuleRuntimeException;
16  import org.mule.config.i18n.MessageFactory;
17  import org.mule.transport.jms.i18n.JmsMessages;
18  
19  import java.text.MessageFormat;
20  
21  import javax.jms.JMSException;
22  import javax.jms.Message;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  /**
28   * A redelivery handler which relies on JMS provider's redelivery count facilities.
29   * @see org.mule.transport.jms.JmsConstants#JMS_X_DELIVERY_COUNT
30   */
31  public class JmsXRedeliveryHandler extends AbstractRedeliveryHandler
32  {
33      /**
34       * logger used by this class
35       */
36      protected static final Log logger = LogFactory.getLog(JmsXRedeliveryHandler.class);
37  
38      /**
39       * process the redelivered message. If the Jms receiver should process the
40       * message, it should be returned. Otherwise the connector should throw a
41       * <code>MessageRedeliveredException</code> to indicate that the message should
42       * be handled by the connector Exception Handler.
43       * 
44       */
45      @Override
46      public void handleRedelivery(Message message) throws JMSException, MuleException
47      {
48          final int connectorRedelivery = connector.getMaxRedelivery();
49          if (connectorRedelivery == JmsConnector.REDELIVERY_IGNORE || connectorRedelivery < 0 ) // just in case, for manual setting)
50          {
51              if (logger.isDebugEnabled())
52              {
53                  logger.debug("We were asked to ignore the redelivery count, nothing to do here.");
54              }
55              return;
56          }
57  
58          String messageId = message.getJMSMessageID();
59  
60          int deliveryCount = -1;
61          try
62          {
63              deliveryCount = message.getIntProperty(JmsConstants.JMS_X_DELIVERY_COUNT);
64          }
65          catch (NumberFormatException nex)
66          {
67              throw new MuleRuntimeException(MessageFactory.createStaticMessage(String.format(
68                      "Invalid use of %s. Message is flagged with JMSRedelivered, but JMSXDeliveryCount is not set",
69                      getClass().getName())));
70          }
71  
72          int redeliveryCount = deliveryCount - 1;
73  
74          if (redeliveryCount == 1)
75          {
76              if (logger.isDebugEnabled())
77              {
78                  logger.debug("Message with id: " + messageId + " has been redelivered for the first time");
79              }
80  
81              if (connectorRedelivery == JmsConnector.REDELIVERY_FAIL_ON_FIRST)
82              {
83                  MuleMessage msg = createMuleMessage(message);
84                  throw new MessageRedeliveredException(
85                      JmsMessages.tooManyRedeliveries(messageId, String.valueOf(redeliveryCount),
86                          connectorRedelivery, connector.getName()), msg);
87              }
88          }
89          else if (redeliveryCount > connectorRedelivery)
90          {
91              logger.debug(MessageFormat.format(
92                      "Message with id: {0} has been redelivered {1} times, which exceeds the maxRedelivery setting " +
93                      "of {2} on the connector {3}", messageId, redeliveryCount, connectorRedelivery, connector.getName()));
94  
95              MuleMessage msg = createMuleMessage(message);
96              throw new MessageRedeliveredException(
97                  JmsMessages.tooManyRedeliveries(messageId, String.valueOf(redeliveryCount),
98                      connectorRedelivery, connector.getName()), msg);
99          }
100         else
101         {
102             if (logger.isDebugEnabled())
103             {
104                 // re-delivery count is actually less by 1 than an actual delivery count
105                 logger.debug("Message with id: " + messageId + " has been redelivered " + redeliveryCount + " times");
106             }
107         }
108     }
109 }