View Javadoc

1   /*
2    * $Id: JmsMessageRequester.java 11710 2008-05-09 08:27:48Z 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.transport.jms;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.endpoint.InboundEndpoint;
16  import org.mule.transport.AbstractMessageRequester;
17  import org.mule.transport.jms.filters.JmsSelectorFilter;
18  
19  import javax.jms.Destination;
20  import javax.jms.Message;
21  import javax.jms.MessageConsumer;
22  import javax.jms.Session;
23  
24  /**
25   * <code>JmsMessageDispatcher</code> is responsible for dispatching messages to JMS
26   * destinations. All JMS semantics apply and settings such as replyTo and QoS
27   * properties are read from the event properties or defaults are used (according to
28   * the JMS specification)
29   */
30  public class JmsMessageRequester extends AbstractMessageRequester
31  {
32  
33      private JmsConnector connector;
34  
35      public JmsMessageRequester(InboundEndpoint endpoint)
36      {
37          super(endpoint);
38          this.connector = (JmsConnector) endpoint.getConnector();
39      }
40  
41      protected void doConnect() throws Exception
42      {
43          // template method
44      }
45  
46      protected void doDisconnect() throws Exception
47      {
48          // template method
49      }
50  
51      /**
52       * Make a specific request to the underlying transport
53       *
54       * @param timeout the maximum time the operation should block before returning.
55       *            The call should return immediately if there is data available. If
56       *            no data becomes available before the timeout elapses, null will be
57       *            returned
58       * @return the result of the request wrapped in a MuleMessage object. Null will be
59       *         returned if no data was avaialable
60       * @throws Exception if the call to the underlying protocal cuases an exception
61       */
62      protected MuleMessage doRequest(long timeout) throws Exception
63      {
64          Session session = null;
65          MessageConsumer consumer = null;
66  
67          try
68          {
69              final boolean topic = connector.getTopicResolver().isTopic(endpoint);
70  
71              JmsSupport support = connector.getJmsSupport();
72              session = connector.getSession(false, topic);
73              Destination dest = support.createDestination(session, endpoint.getEndpointURI().getAddress(),
74                  topic);
75  
76              // Extract jms selector
77              String selector = null;
78              if (endpoint.getFilter() != null && endpoint.getFilter() instanceof JmsSelectorFilter)
79              {
80                  selector = ((JmsSelectorFilter) endpoint.getFilter()).getExpression();
81              }
82              else if (endpoint.getProperties() != null)
83              {
84                  // still allow the selector to be set as a property on the endpoint
85                  // to be backward compatable
86                  selector = (String) endpoint.getProperties().get(JmsConstants.JMS_SELECTOR_PROPERTY);
87              }
88              String tempDurable = (String) endpoint.getProperties().get(JmsConstants.DURABLE_PROPERTY);
89              boolean durable = connector.isDurable();
90              if (tempDurable != null)
91              {
92                  durable = Boolean.valueOf(tempDurable).booleanValue();
93              }
94  
95              // Get the durable subscriber name if there is one
96              String durableName = (String) endpoint.getProperties().get(JmsConstants.DURABLE_NAME_PROPERTY);
97              if (durableName == null && durable && topic)
98              {
99                  durableName = "mule." + connector.getName() + "." + endpoint.getEndpointURI().getAddress();
100                 if (logger.isDebugEnabled())
101                 {
102                     logger.debug("Jms Connector for this receiver is durable but no durable name has been specified. Defaulting to: "
103                              + durableName);
104                 }
105             }
106 
107             // Create consumer
108             consumer = support.createConsumer(session, dest, selector, connector.isNoLocal(), durableName,
109                 topic);
110 
111             try
112             {
113                 Message message;
114 
115                 if (timeout == JmsMessageDispatcher.RECEIVE_NO_WAIT)
116                 {
117                     message = consumer.receiveNoWait();
118                 }
119                 else if (timeout == JmsMessageDispatcher.RECEIVE_WAIT_INDEFINITELY)
120                 {
121                     message = consumer.receive();
122                 }
123                 else
124                 {
125                     message = consumer.receive(timeout);
126                 }
127 
128                 if (message == null)
129                 {
130                     return null;
131                 }
132 
133                 message = connector.preProcessMessage(message, session);
134 
135                 return new DefaultMuleMessage(connector.getMessageAdapter(message));
136             }
137             catch (Exception e)
138             {
139                 connector.handleException(e);
140                 return null;
141             }
142         }
143         finally
144         {
145             connector.closeQuietly(consumer);
146             connector.closeQuietly(session);
147         }
148     }
149 
150     protected void doDispose()
151     {
152         // template method
153     }
154 
155 }