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