1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.jms.redelivery; |
12 | |
|
13 | |
import org.mule.api.MuleException; |
14 | |
import org.mule.api.MuleMessage; |
15 | |
import org.mule.api.construct.FlowConstruct; |
16 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
17 | |
import org.mule.transport.jms.JmsConnector; |
18 | |
|
19 | |
import java.util.Collections; |
20 | |
import java.util.Map; |
21 | |
|
22 | |
import javax.jms.JMSException; |
23 | |
import javax.jms.Message; |
24 | |
|
25 | |
import org.apache.commons.collections.map.LRUMap; |
26 | |
import org.apache.commons.logging.Log; |
27 | |
import org.apache.commons.logging.LogFactory; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class CountingRedeliveryHandler extends AbstractRedeliveryHandler |
34 | |
{ |
35 | |
|
36 | |
|
37 | |
|
38 | 0 | protected static final Log logger = LogFactory.getLog(CountingRedeliveryHandler.class); |
39 | |
|
40 | 0 | private Map<String, Integer> messages = null; |
41 | |
|
42 | |
@SuppressWarnings("unchecked") |
43 | |
public CountingRedeliveryHandler() |
44 | |
{ |
45 | 0 | super(); |
46 | 0 | messages = Collections.synchronizedMap(new LRUMap(256)); |
47 | 0 | } |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
@Override |
57 | |
public void handleRedelivery(Message message, ImmutableEndpoint endpoint, FlowConstruct flow) throws JMSException, MuleException |
58 | |
{ |
59 | 0 | final int connectorRedelivery = connector.getMaxRedelivery(); |
60 | 0 | if (connectorRedelivery == JmsConnector.REDELIVERY_IGNORE || connectorRedelivery < 0 ) |
61 | |
{ |
62 | 0 | if (logger.isDebugEnabled()) |
63 | |
{ |
64 | 0 | logger.debug("We were asked to ignore the redelivery count, nothing to do here."); |
65 | |
} |
66 | 0 | return; |
67 | |
} |
68 | |
|
69 | 0 | String id = message.getJMSMessageID(); |
70 | |
|
71 | 0 | if (id == null) |
72 | |
{ |
73 | 0 | if (logger.isDebugEnabled()) |
74 | |
{ |
75 | 0 | logger.debug("Message doesn't have a JMSMessageID set, Mule can't handle redelivery for it. " + message); |
76 | |
} |
77 | 0 | return; |
78 | |
} |
79 | |
|
80 | 0 | Integer redeliveryCount = messages.remove(id); |
81 | 0 | if (redeliveryCount != null) |
82 | |
{ |
83 | 0 | redeliveryCount += 1; |
84 | |
} |
85 | |
|
86 | 0 | if (redeliveryCount == null) |
87 | |
{ |
88 | 0 | if (logger.isDebugEnabled()) |
89 | |
{ |
90 | 0 | logger.debug("Message with id: " + id + " has been redelivered for the first time"); |
91 | |
} |
92 | 0 | messages.put(id, 1); |
93 | |
} |
94 | 0 | else if (redeliveryCount == 1) |
95 | |
{ |
96 | 0 | if (logger.isDebugEnabled()) |
97 | |
{ |
98 | 0 | logger.debug("Message with id: " + id + " has been redelivered for the first time"); |
99 | |
} |
100 | |
|
101 | 0 | if (connectorRedelivery == JmsConnector.REDELIVERY_FAIL_ON_FIRST) |
102 | |
{ |
103 | 0 | MuleMessage msg = createMuleMessage(message); |
104 | 0 | throw new MessageRedeliveredException(id, redeliveryCount, connectorRedelivery, endpoint, flow, msg); |
105 | |
} |
106 | |
} |
107 | 0 | else if (redeliveryCount > connectorRedelivery) |
108 | |
{ |
109 | 0 | MuleMessage msg = createMuleMessage(message); |
110 | 0 | throw new MessageRedeliveredException(id, redeliveryCount, connectorRedelivery, endpoint, flow, msg); |
111 | |
} |
112 | |
else |
113 | |
{ |
114 | 0 | messages.put(id, redeliveryCount); |
115 | 0 | if (logger.isDebugEnabled()) |
116 | |
{ |
117 | 0 | logger.debug("Message with id: " + id + " has been redelivered " + redeliveryCount + " times"); |
118 | |
} |
119 | |
} |
120 | 0 | } |
121 | |
} |