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