KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > jms > DefaultRedeliveryHandler


1 /*
2  * $Id: DefaultRedeliveryHandler.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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 java.util.Collections JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import javax.jms.JMSException JavaDoc;
17 import javax.jms.Message JavaDoc;
18
19 import org.apache.commons.collections.map.LRUMap;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.mule.umo.MessagingException;
23
24 /**
25  * <code>DefaultRedeliveryHandler</code> TODO
26  */

27 public class DefaultRedeliveryHandler implements RedeliveryHandler
28 {
29     /**
30      * logger used by this class
31      */

32     protected static Log logger = LogFactory.getLog(DefaultRedeliveryHandler.class);
33
34     private Map JavaDoc messages = null;
35
36     protected JmsConnector connector;
37
38     public DefaultRedeliveryHandler()
39     {
40         messages = Collections.synchronizedMap(new LRUMap(256));
41     }
42
43     /**
44      * The connector associated with this handler is set before
45      * <code>handleRedelivery()</code> is called
46      *
47      * @param connector the connector associated with this handler
48      */

49     public void setConnector(JmsConnector connector)
50     {
51         this.connector = connector;
52     }
53
54     /**
55      * process the redelivered message. If the Jms receiver should process the
56      * message, it should be returned. Otherwise the connector should throw a
57      * <code>MessageRedeliveredException</code> to indicate that the message should
58      * be handled by the connector Exception Handler.
59      *
60      * @param message
61      */

62     public void handleRedelivery(Message JavaDoc message) throws JMSException JavaDoc, MessagingException
63     {
64         if (connector.getMaxRedelivery() <= 0)
65         {
66             return;
67         }
68
69         String JavaDoc id = message.getJMSMessageID();
70         Integer JavaDoc i = (Integer JavaDoc)messages.remove(id);
71         if (i == null)
72         {
73             if (logger.isDebugEnabled())
74             {
75                 logger.debug("Message with id: " + id + " has been redelivered for the fist time");
76             }
77             messages.put(id, new Integer JavaDoc(1));
78             return;
79         }
80         else if (i.intValue() == connector.getMaxRedelivery())
81         {
82             if (logger.isDebugEnabled())
83             {
84                 logger.debug("Message with id: " + id + " has been redelivered " + (i.intValue() + 1)
85                              + " times, which exceeds the maxRedelivery setting on the connector");
86             }
87             JmsMessageAdapter adapter = (JmsMessageAdapter)connector.getMessageAdapter(message);
88             throw new MessageRedeliveredException(new org.mule.config.i18n.Message("jms", 11, id,
89                 String.valueOf(i.intValue() + 1)), adapter);
90
91         }
92         else
93         {
94             messages.put(id, new Integer JavaDoc(i.intValue() + 1));
95             if (logger.isDebugEnabled())
96             {
97                 logger.debug("Message with id: " + id + " has been redelivered " + i.intValue() + " times");
98             }
99         }
100     }
101 }
102
Popular Tags