1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.jms; |
12 | |
|
13 | |
import org.mule.impl.model.AbstractComponent; |
14 | |
import org.mule.providers.DefaultReplyToHandler; |
15 | |
import org.mule.providers.jms.i18n.JmsMessages; |
16 | |
import org.mule.umo.UMOEvent; |
17 | |
import org.mule.umo.UMOException; |
18 | |
import org.mule.umo.UMOMessage; |
19 | |
import org.mule.umo.provider.DispatchException; |
20 | |
import org.mule.umo.transformer.UMOTransformer; |
21 | |
|
22 | |
import javax.jms.DeliveryMode; |
23 | |
import javax.jms.Destination; |
24 | |
import javax.jms.Message; |
25 | |
import javax.jms.MessageProducer; |
26 | |
import javax.jms.Session; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public class JmsReplyToHandler extends DefaultReplyToHandler |
33 | |
{ |
34 | |
private final JmsConnector connector; |
35 | |
|
36 | |
public JmsReplyToHandler(JmsConnector connector, UMOTransformer transformer) |
37 | |
{ |
38 | 0 | super(transformer); |
39 | 0 | this.connector = connector; |
40 | 0 | } |
41 | |
|
42 | |
public void processReplyTo(UMOEvent event, UMOMessage returnMessage, Object replyTo) throws UMOException |
43 | |
{ |
44 | 0 | Destination replyToDestination = null; |
45 | 0 | MessageProducer replyToProducer = null; |
46 | 0 | Session session = null; |
47 | |
try |
48 | |
{ |
49 | |
|
50 | 0 | if (replyTo instanceof Destination) |
51 | |
{ |
52 | 0 | replyToDestination = (Destination)replyTo; |
53 | |
} |
54 | 0 | if (replyToDestination == null) |
55 | |
{ |
56 | 0 | super.processReplyTo(event, returnMessage, replyTo); |
57 | |
return; |
58 | |
} |
59 | 0 | Object payload = returnMessage.getPayload(); |
60 | 0 | if (getTransformer() != null) |
61 | |
{ |
62 | 0 | getTransformer().setEndpoint(getEndpoint(event, "jms://temporary")); |
63 | 0 | if (getTransformer().isSourceTypeSupported(payload.getClass())) |
64 | |
{ |
65 | 0 | payload = getTransformer().transform(payload); |
66 | |
} |
67 | 0 | else if (logger.isDebugEnabled()) |
68 | |
{ |
69 | 0 | logger.debug("Transformer for replyTo Handler: " + getTransformer().toString() |
70 | |
+ " does not support source type: " + payload.getClass() |
71 | |
+ ". Not doing a transform"); |
72 | |
} |
73 | |
} |
74 | |
|
75 | 0 | final boolean topic = connector.getTopicResolver().isTopic(replyToDestination); |
76 | 0 | session = connector.getSession(false, topic); |
77 | 0 | Message replyToMessage = JmsMessageUtils.toMessage(payload, session); |
78 | |
|
79 | 0 | replyToMessage.setJMSReplyTo(null); |
80 | 0 | if (logger.isDebugEnabled()) |
81 | |
{ |
82 | 0 | logger.debug("Sending jms reply to: " + replyToDestination + "(" |
83 | |
+ replyToDestination.getClass().getName() + ")"); |
84 | |
} |
85 | 0 | replyToProducer = connector.getJmsSupport().createProducer(session, replyToDestination, topic); |
86 | |
|
87 | |
|
88 | 0 | UMOMessage eventMsg = event.getMessage(); |
89 | 0 | String ttlString = (String)eventMsg.removeProperty(JmsConstants.TIME_TO_LIVE_PROPERTY); |
90 | 0 | String priorityString = (String)eventMsg.removeProperty(JmsConstants.PRIORITY_PROPERTY); |
91 | 0 | String persistentDeliveryString = (String)eventMsg.removeProperty(JmsConstants.PERSISTENT_DELIVERY_PROPERTY); |
92 | |
|
93 | 0 | if (ttlString == null && priorityString == null && persistentDeliveryString == null) |
94 | |
{ |
95 | 0 | connector.getJmsSupport().send(replyToProducer, replyToMessage, topic); |
96 | |
} |
97 | |
else |
98 | |
{ |
99 | 0 | long ttl = Message.DEFAULT_TIME_TO_LIVE; |
100 | 0 | int priority = Message.DEFAULT_PRIORITY; |
101 | |
|
102 | |
|
103 | 0 | boolean persistent = Message.DEFAULT_DELIVERY_MODE == DeliveryMode.PERSISTENT; |
104 | |
|
105 | 0 | if (ttlString != null) |
106 | |
{ |
107 | 0 | ttl = Long.parseLong(ttlString); |
108 | |
} |
109 | 0 | if (priorityString != null) |
110 | |
{ |
111 | 0 | priority = Integer.parseInt(priorityString); |
112 | |
} |
113 | |
|
114 | 0 | persistent = persistentDeliveryString != null |
115 | |
? Boolean.valueOf(persistentDeliveryString).booleanValue() |
116 | |
: connector.isPersistentDelivery(); |
117 | |
|
118 | 0 | connector.getJmsSupport().send(replyToProducer, replyToMessage, persistent, priority, ttl, |
119 | |
topic); |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | 0 | logger.info("Reply Message sent to: " + replyToDestination); |
125 | 0 | ((AbstractComponent)event.getComponent()).getStatistics().incSentReplyToEvent(); |
126 | |
} |
127 | 0 | catch (Exception e) |
128 | |
{ |
129 | 0 | throw new DispatchException( |
130 | |
JmsMessages.failedToCreateAndDispatchResponse(replyToDestination), returnMessage, null, e); |
131 | |
} |
132 | |
finally |
133 | |
{ |
134 | 0 | connector.closeQuietly(replyToProducer); |
135 | 0 | connector.closeQuietly(session); |
136 | 0 | } |
137 | 0 | } |
138 | |
} |