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