View Javadoc

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