View Javadoc

1   /*
2    * $Id: DefaultRedeliveryHandler.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.providers.jms;
12  
13  import org.mule.providers.jms.i18n.JmsMessages;
14  import org.mule.umo.MessagingException;
15  
16  import java.util.Collections;
17  import java.util.Map;
18  
19  import javax.jms.JMSException;
20  import javax.jms.Message;
21  
22  import org.apache.commons.collections.map.LRUMap;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  /**
27   * <code>DefaultRedeliveryHandler</code> TODO
28   */
29  public class DefaultRedeliveryHandler implements RedeliveryHandler
30  {
31      /**
32       * logger used by this class
33       */
34      protected static final Log logger = LogFactory.getLog(DefaultRedeliveryHandler.class);
35  
36      private Map messages = null;
37  
38      protected JmsConnector connector;
39  
40      public DefaultRedeliveryHandler()
41      {
42          messages = Collections.synchronizedMap(new LRUMap(256));
43      }
44  
45      /**
46       * The connector associated with this handler is set before
47       * <code>handleRedelivery()</code> is called
48       * 
49       * @param connector the connector associated with this handler
50       */
51      public void setConnector(JmsConnector connector)
52      {
53          this.connector = connector;
54      }
55  
56      /**
57       * process the redelivered message. If the Jms receiver should process the
58       * message, it should be returned. Otherwise the connector should throw a
59       * <code>MessageRedeliveredException</code> to indicate that the message should
60       * be handled by the connector Exception Handler.
61       * 
62       * @param message
63       */
64      public void handleRedelivery(Message message) throws JMSException, MessagingException
65      {
66          if (connector.getMaxRedelivery() <= 0)
67          {
68              return;
69          }
70  
71          String id = message.getJMSMessageID();
72          Integer i = (Integer)messages.remove(id);
73          if (i == null)
74          {
75              if (logger.isDebugEnabled())
76              {
77                  logger.debug("Message with id: " + id + " has been redelivered for the first time");
78              }
79              messages.put(id, new Integer(1));
80              return;
81          }
82          else if (i.intValue() == connector.getMaxRedelivery())
83          {
84              if (logger.isDebugEnabled())
85              {
86                  logger.debug("Message with id: " + id + " has been redelivered " + (i.intValue() + 1)
87                               + " times, which exceeds the maxRedelivery setting on the connector");
88              }
89              JmsMessageAdapter adapter = (JmsMessageAdapter)connector.getMessageAdapter(message);
90              throw new MessageRedeliveredException(
91                  JmsMessages.tooManyRedeliveries(id, String.valueOf(i.intValue() + 1)), adapter);
92  
93          }
94          else
95          {
96              messages.put(id, new Integer(i.intValue() + 1));
97              if (logger.isDebugEnabled())
98              {
99                  logger.debug("Message with id: " + id + " has been redelivered " + i.intValue() + " times");
100             }
101         }
102     }
103 }