View Javadoc

1   /*
2    * $Id: CountingRedeliveryHandler.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.transport.jms.i18n.JmsMessages;
16  
17  import java.text.MessageFormat;
18  import java.util.Collections;
19  import java.util.Map;
20  
21  import javax.jms.JMSException;
22  import javax.jms.Message;
23  
24  import org.apache.commons.collections.map.LRUMap;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /**
29   * This redelivery handler will keep counting the redelivery attempts for each message redelivered. Used for
30   * providers not implementing the {@code JMSXDeliveryCount} property support.
31   */
32  public class CountingRedeliveryHandler extends AbstractRedeliveryHandler
33  {
34      /**
35       * logger used by this class
36       */
37      protected static final Log logger = LogFactory.getLog(CountingRedeliveryHandler.class);
38  
39      private Map<String, Integer> messages = null;
40  
41      @SuppressWarnings("unchecked")
42      public CountingRedeliveryHandler()
43      {
44          super();
45          messages = Collections.synchronizedMap(new LRUMap(256));
46      }
47  
48      /**
49       * process the redelivered message. If the Jms receiver should process the
50       * message, it should be returned. Otherwise the connector should throw a
51       * <code>MessageRedeliveredException</code> to indicate that the message should
52       * be handled by the connector Exception Handler.
53       * 
54       */
55      @Override
56      public void handleRedelivery(Message message) throws JMSException, MuleException
57      {
58          final int connectorRedelivery = connector.getMaxRedelivery();
59          if (connectorRedelivery == JmsConnector.REDELIVERY_IGNORE || connectorRedelivery < 0 ) // just in case, for manual setting)
60          {
61              if (logger.isDebugEnabled())
62              {
63                  logger.debug("We were asked to ignore the redelivery count, nothing to do here.");
64              }
65              return;
66          }
67  
68          String id = message.getJMSMessageID();
69  
70          if (id == null)
71          {
72              if (logger.isDebugEnabled())
73              {
74                  logger.debug("Message doesn't have a JMSMessageID set, Mule can't handle redelivery for it. " + message);
75              }
76              return;
77          }
78  
79          Integer redeliveryCount = messages.remove(id);
80          if (redeliveryCount != null)
81          {
82              redeliveryCount += 1; // inc the count
83          }
84  
85          if (redeliveryCount == null)
86          {
87              if (logger.isDebugEnabled())
88              {
89                  logger.debug("Message with id: " + id + " has been redelivered for the first time");
90              }
91              messages.put(id, 1);
92          }
93          else if (redeliveryCount == 1)
94          {
95              if (logger.isDebugEnabled())
96              {
97                  logger.debug("Message with id: " + id + " has been redelivered for the first time");
98              }
99  
100             if (connectorRedelivery == JmsConnector.REDELIVERY_FAIL_ON_FIRST)
101             {
102                 MuleMessage msg = createMuleMessage(message);
103                 throw new MessageRedeliveredException(
104                     JmsMessages.tooManyRedeliveries(id, String.valueOf(redeliveryCount), 
105                         connectorRedelivery, connector.getName()), msg);
106             }
107         }
108         else if (redeliveryCount > connectorRedelivery)
109         {
110             if (logger.isDebugEnabled())
111             {
112                 logger.debug(MessageFormat.format(
113                         "Message with id: {0} has been redelivered {1} times, which exceeds the maxRedelivery setting " +
114                         "of {2} on the connector {3}", id, redeliveryCount, connectorRedelivery, connector.getName()));
115             }
116             MuleMessage msg = createMuleMessage(message);
117             throw new MessageRedeliveredException(
118                 JmsMessages.tooManyRedeliveries(id, "" + redeliveryCount, connectorRedelivery,
119                     connector.getName()), msg);
120         }
121         else
122         {
123             messages.put(id, redeliveryCount);
124             if (logger.isDebugEnabled())
125             {
126                 logger.debug("Message with id: " + id + " has been redelivered " + redeliveryCount + " times");
127             }
128         }
129     }
130 }