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