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.MuleRuntimeException; |
16 | |
import org.mule.api.construct.FlowConstruct; |
17 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
18 | |
import org.mule.config.i18n.MessageFactory; |
19 | |
import org.mule.transport.jms.JmsConnector; |
20 | |
import org.mule.transport.jms.JmsConstants; |
21 | |
|
22 | |
import javax.jms.JMSException; |
23 | |
import javax.jms.Message; |
24 | |
|
25 | |
import org.apache.commons.logging.Log; |
26 | |
import org.apache.commons.logging.LogFactory; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | 0 | public class JmsXRedeliveryHandler extends AbstractRedeliveryHandler |
33 | |
{ |
34 | |
|
35 | |
|
36 | |
|
37 | 0 | protected static final Log logger = LogFactory.getLog(JmsXRedeliveryHandler.class); |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
@Override |
47 | |
public void handleRedelivery(Message message, ImmutableEndpoint endpoint, FlowConstruct flow) throws JMSException, MuleException |
48 | |
{ |
49 | 0 | final int connectorRedelivery = connector.getMaxRedelivery(); |
50 | 0 | if (connectorRedelivery == JmsConnector.REDELIVERY_IGNORE || connectorRedelivery < 0 ) |
51 | |
{ |
52 | 0 | if (logger.isDebugEnabled()) |
53 | |
{ |
54 | 0 | logger.debug("We were asked to ignore the redelivery count, nothing to do here."); |
55 | |
} |
56 | 0 | return; |
57 | |
} |
58 | |
|
59 | 0 | String messageId = message.getJMSMessageID(); |
60 | |
|
61 | 0 | int deliveryCount = -1; |
62 | |
try |
63 | |
{ |
64 | 0 | deliveryCount = message.getIntProperty(JmsConstants.JMS_X_DELIVERY_COUNT); |
65 | |
} |
66 | 0 | catch (NumberFormatException nex) |
67 | |
{ |
68 | 0 | throw new MuleRuntimeException(MessageFactory.createStaticMessage(String.format( |
69 | |
"Invalid use of %s. Message is flagged with JMSRedelivered, but JMSXDeliveryCount is not set", |
70 | |
getClass().getName()))); |
71 | 0 | } |
72 | |
|
73 | 0 | int redeliveryCount = deliveryCount - 1; |
74 | |
|
75 | 0 | if (redeliveryCount == 1) |
76 | |
{ |
77 | 0 | if (logger.isDebugEnabled()) |
78 | |
{ |
79 | 0 | logger.debug("Message with id: " + messageId + " has been redelivered for the first time"); |
80 | |
} |
81 | |
|
82 | 0 | if (connectorRedelivery == JmsConnector.REDELIVERY_FAIL_ON_FIRST) |
83 | |
{ |
84 | 0 | MuleMessage msg = createMuleMessage(message); |
85 | 0 | throw new MessageRedeliveredException(messageId, redeliveryCount, connectorRedelivery, endpoint, flow, msg); |
86 | |
} |
87 | |
} |
88 | 0 | else if (redeliveryCount > connectorRedelivery) |
89 | |
{ |
90 | 0 | MuleMessage msg = createMuleMessage(message); |
91 | 0 | throw new MessageRedeliveredException(messageId, redeliveryCount, connectorRedelivery, endpoint, flow, msg); |
92 | |
} |
93 | |
else |
94 | |
{ |
95 | 0 | if (logger.isDebugEnabled()) |
96 | |
{ |
97 | |
|
98 | 0 | logger.debug("Message with id: " + messageId + " has been redelivered " + redeliveryCount + " times"); |
99 | |
} |
100 | |
} |
101 | 0 | } |
102 | |
} |