1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.jms; |
12 | |
|
13 | |
import org.mule.api.MessagingException; |
14 | |
import org.mule.transport.jms.i18n.JmsMessages; |
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 | 12 | protected static final Log logger = LogFactory.getLog(DefaultRedeliveryHandler.class); |
35 | |
|
36 | 30 | private Map messages = null; |
37 | |
|
38 | |
protected JmsConnector connector; |
39 | |
|
40 | |
public DefaultRedeliveryHandler() |
41 | 30 | { |
42 | 30 | messages = Collections.synchronizedMap(new LRUMap(256)); |
43 | 30 | } |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public void setConnector(JmsConnector connector) |
52 | |
{ |
53 | 20 | this.connector = connector; |
54 | 20 | } |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public void handleRedelivery(Message message) throws JMSException, MessagingException |
65 | |
{ |
66 | 2 | if (connector.getMaxRedelivery() <= 0) |
67 | |
{ |
68 | 0 | return; |
69 | |
} |
70 | |
|
71 | 2 | String id = message.getJMSMessageID(); |
72 | 2 | Integer i = (Integer)messages.remove(id); |
73 | 2 | if (i == null) |
74 | |
{ |
75 | 2 | if (logger.isDebugEnabled()) |
76 | |
{ |
77 | 0 | logger.debug("Message with id: " + id + " has been redelivered for the first time"); |
78 | |
} |
79 | 2 | messages.put(id, new Integer(1)); |
80 | 2 | return; |
81 | |
} |
82 | 0 | else if (i.intValue() == connector.getMaxRedelivery()) |
83 | |
{ |
84 | 0 | if (logger.isDebugEnabled()) |
85 | |
{ |
86 | 0 | logger.debug("Message with id: " + id + " has been redelivered " + (i.intValue() + 1) |
87 | |
+ " times, which exceeds the maxRedelivery setting on the connector"); |
88 | |
} |
89 | 0 | JmsMessageAdapter adapter = (JmsMessageAdapter)connector.getMessageAdapter(message); |
90 | 0 | throw new MessageRedeliveredException( |
91 | |
JmsMessages.tooManyRedeliveries(id, String.valueOf(i.intValue() + 1)), adapter); |
92 | |
|
93 | |
} |
94 | |
else |
95 | |
{ |
96 | 0 | messages.put(id, new Integer(i.intValue() + 1)); |
97 | 0 | if (logger.isDebugEnabled()) |
98 | |
{ |
99 | 0 | logger.debug("Message with id: " + id + " has been redelivered " + i.intValue() + " times"); |
100 | |
} |
101 | |
} |
102 | 0 | } |
103 | |
} |