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 public class JmsXRedeliveryHandler extends AbstractRedeliveryHandler
33 {
34
35
36
37 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 final int connectorRedelivery = connector.getMaxRedelivery();
50 if (connectorRedelivery == JmsConnector.REDELIVERY_IGNORE || connectorRedelivery < 0 )
51 {
52 if (logger.isDebugEnabled())
53 {
54 logger.debug("We were asked to ignore the redelivery count, nothing to do here.");
55 }
56 return;
57 }
58
59 String messageId = message.getJMSMessageID();
60
61 int deliveryCount = -1;
62 try
63 {
64 deliveryCount = message.getIntProperty(JmsConstants.JMS_X_DELIVERY_COUNT);
65 }
66 catch (NumberFormatException nex)
67 {
68 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 }
72
73 int redeliveryCount = deliveryCount - 1;
74
75 if (redeliveryCount == 1)
76 {
77 if (logger.isDebugEnabled())
78 {
79 logger.debug("Message with id: " + messageId + " has been redelivered for the first time");
80 }
81
82 if (connectorRedelivery == JmsConnector.REDELIVERY_FAIL_ON_FIRST)
83 {
84 MuleMessage msg = createMuleMessage(message);
85 throw new MessageRedeliveredException(messageId, redeliveryCount, connectorRedelivery, endpoint, flow, msg);
86 }
87 }
88 else if (redeliveryCount > connectorRedelivery)
89 {
90 MuleMessage msg = createMuleMessage(message);
91 throw new MessageRedeliveredException(messageId, redeliveryCount, connectorRedelivery, endpoint, flow, msg);
92 }
93 else
94 {
95 if (logger.isDebugEnabled())
96 {
97
98 logger.debug("Message with id: " + messageId + " has been redelivered " + redeliveryCount + " times");
99 }
100 }
101 }
102 }