1
2
3
4
5
6
7
8
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
28
29 public class DefaultRedeliveryHandler implements RedeliveryHandler
30 {
31
32
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
47
48
49
50
51 public void setConnector(JmsConnector connector)
52 {
53 this.connector = connector;
54 }
55
56
57
58
59
60
61
62
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 }