View Javadoc

1   /*
2    * $Id: JmsReplyToHandler.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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   * <code>JmsReplyToHandler</code> will process a JMS replyTo or hand off to the
30   * default replyTo handler if the replyTo is a URL.
31   */
32  public class JmsReplyToHandler extends DefaultReplyToHandler
33  {
34      private final JmsConnector connector;
35  
36      public JmsReplyToHandler(JmsConnector connector, UMOTransformer transformer)
37      {
38          super(transformer);
39          this.connector = connector;
40      }
41  
42      public void processReplyTo(UMOEvent event, UMOMessage returnMessage, Object replyTo) throws UMOException
43      {
44          Destination replyToDestination = null;
45          MessageProducer replyToProducer = null;
46          Session session = null;
47          try
48          {
49              // now we need to send the response
50              if (replyTo instanceof Destination)
51              {
52                  replyToDestination = (Destination)replyTo;
53              }
54              if (replyToDestination == null)
55              {
56                  super.processReplyTo(event, returnMessage, replyTo);
57                  return;
58              }
59              Object payload = returnMessage.getPayload();
60              if (getTransformer() != null)
61              {
62                  getTransformer().setEndpoint(getEndpoint(event, "jms://temporary"));
63                  if (getTransformer().isSourceTypeSupported(payload.getClass()))
64                  {
65                      payload = getTransformer().transform(payload);
66                  }
67                  else if (logger.isDebugEnabled())
68                  {
69                      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              final boolean topic = connector.getTopicResolver().isTopic(replyToDestination);
76              session = connector.getSession(false, topic);
77              Message replyToMessage = JmsMessageUtils.toMessage(payload, session);
78  
79              replyToMessage.setJMSReplyTo(null);
80              if (logger.isDebugEnabled())
81              {
82                  logger.debug("Sending jms reply to: " + replyToDestination + "("
83                               + replyToDestination.getClass().getName() + ")");
84              }
85              replyToProducer = connector.getJmsSupport().createProducer(session, replyToDestination, topic);
86  
87              // QoS support
88              UMOMessage eventMsg = event.getMessage();
89              String ttlString = (String)eventMsg.removeProperty(JmsConstants.TIME_TO_LIVE_PROPERTY);
90              String priorityString = (String)eventMsg.removeProperty(JmsConstants.PRIORITY_PROPERTY);
91              String persistentDeliveryString = (String)eventMsg.removeProperty(JmsConstants.PERSISTENT_DELIVERY_PROPERTY);
92  
93              if (ttlString == null && priorityString == null && persistentDeliveryString == null)
94              {
95                  connector.getJmsSupport().send(replyToProducer, replyToMessage, topic);
96              }
97              else
98              {
99                  long ttl = Message.DEFAULT_TIME_TO_LIVE;
100                 int priority = Message.DEFAULT_PRIORITY;
101 
102                 // TODO this first assignment is ignored anyway, review and remove if need to
103                 boolean persistent = Message.DEFAULT_DELIVERY_MODE == DeliveryMode.PERSISTENT;
104 
105                 if (ttlString != null)
106                 {
107                     ttl = Long.parseLong(ttlString);
108                 }
109                 if (priorityString != null)
110                 {
111                     priority = Integer.parseInt(priorityString);
112                 }
113                 // TODO StringUtils.notBlank() would be more robust here
114                 persistent = persistentDeliveryString != null
115                                 ? Boolean.valueOf(persistentDeliveryString).booleanValue()
116                                 : connector.isPersistentDelivery();
117 
118                 connector.getJmsSupport().send(replyToProducer, replyToMessage, persistent, priority, ttl,
119                     topic);
120             }
121 
122             // connector.getJmsSupport().send(replyToProducer, replyToMessage,
123             // replyToDestination);
124             logger.info("Reply Message sent to: " + replyToDestination);
125             ((AbstractComponent)event.getComponent()).getStatistics().incSentReplyToEvent();
126         }
127         catch (Exception e)
128         {
129             throw new DispatchException(
130                 JmsMessages.failedToCreateAndDispatchResponse(replyToDestination), returnMessage, null, e);
131         }
132         finally
133         {
134             connector.closeQuietly(replyToProducer);
135             connector.closeQuietly(session);
136         }
137     }
138 }