View Javadoc

1   /*
2    * $Id: JmsMessageRequester.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.api.MuleMessage;
14  import org.mule.api.context.notification.TransactionNotificationListener;
15  import org.mule.api.endpoint.InboundEndpoint;
16  import org.mule.api.transaction.Transaction;
17  import org.mule.api.transaction.TransactionConfig;
18  import org.mule.context.notification.TransactionNotification;
19  import org.mule.transaction.TransactionCoordination;
20  import org.mule.transport.AbstractMessageRequester;
21  import org.mule.transport.jms.filters.JmsSelectorFilter;
22  import org.mule.util.StringUtils;
23  
24  import javax.jms.Destination;
25  import javax.jms.Message;
26  import javax.jms.MessageConsumer;
27  import javax.jms.Session;
28  
29  /**
30   * <code>JmsMessageDispatcher</code> is responsible for dispatching messages to JMS
31   * destinations. All JMS semantics apply and settings such as replyTo and QoS
32   * properties are read from the event properties or defaults are used (according to
33   * the JMS specification)
34   */
35  public class JmsMessageRequester extends AbstractMessageRequester
36  {
37  
38      private JmsConnector connector;
39  
40      public JmsMessageRequester(InboundEndpoint endpoint)
41      {
42          super(endpoint);
43          this.connector = (JmsConnector) endpoint.getConnector();
44      }
45  
46      @Override
47      protected void doConnect() throws Exception
48      {
49          // template method
50      }
51  
52      @Override
53      protected void doDisconnect() throws Exception
54      {
55          // template method
56      }
57  
58      /**
59       * Make a specific request to the underlying transport
60       *
61       * @param timeout the maximum time the operation should block before returning.
62       *            The call should return immediately if there is data available. If
63       *            no data becomes available before the timeout elapses, null will be
64       *            returned
65       * @return the result of the request wrapped in a MuleMessage object. Null will be
66       *         returned if no data was avaialable
67       * @throws Exception if the call to the underlying protocal cuases an exception
68       */
69      @Override
70      protected MuleMessage doRequest(long timeout) throws Exception
71      {
72          Session session = null;
73          MessageConsumer consumer = null;
74          boolean cleanupListenerRegistered = false;
75  
76          try
77          {
78              final boolean topic = connector.getTopicResolver().isTopic(endpoint);
79  
80              JmsSupport support = connector.getJmsSupport();
81              final TransactionConfig transactionConfig = endpoint.getTransactionConfig();
82              final Transaction tx = TransactionCoordination.getInstance().getTransaction();
83              boolean transacted = transactionConfig != null && transactionConfig.isTransacted();
84  
85              session = connector.getSession(transacted, topic);
86  
87              if (transacted && !tx.isXA())
88              {
89                  // register a session close listener
90                  final Session finalSession = session;
91                  getConnector().getMuleContext().registerListener(new TransactionNotificationListener<TransactionNotification>()
92                  {
93                      public void onNotification(TransactionNotification txNotification)
94                      {
95                          final int txAction = txNotification.getAction();
96                          final String txId = txNotification.getTransactionStringId();
97                          if ((txAction == TransactionNotification.TRANSACTION_COMMITTED || txAction == TransactionNotification.TRANSACTION_ROLLEDBACK) &&
98                              txId.equals(tx.getId())) {
99                              connector.closeQuietly(finalSession);
100                         }
101                     }
102                 }, tx.getId());
103 
104                 cleanupListenerRegistered = true;
105             }
106 
107             Destination dest = support.createDestination(session, endpoint);
108 
109             // Extract jms selector
110             String selector = null;
111             if (endpoint.getFilter() != null && endpoint.getFilter() instanceof JmsSelectorFilter)
112             {
113                 final String expressionTemplate = ((JmsSelectorFilter) endpoint.getFilter()).getExpression();
114                 if (StringUtils.isNotBlank(expressionTemplate))
115                 {
116                     selector = connector.getMuleContext().getExpressionManager().parse(expressionTemplate, null);
117                 }
118             }
119             else if (endpoint.getProperties() != null)
120             {
121                 // still allow the selector to be set as a property on the endpoint
122                 // to be backward compatable
123                 final String expressionTemplate = (String) endpoint.getProperty(JmsConstants.JMS_SELECTOR_PROPERTY);
124                 if (StringUtils.isNotBlank(expressionTemplate))
125                 {
126                     selector = connector.getMuleContext().getExpressionManager().parse(expressionTemplate, null);
127                 }
128             }
129             String tempDurable = (String) endpoint.getProperties().get(JmsConstants.DURABLE_PROPERTY);
130             boolean durable = connector.isDurable();
131             if (tempDurable != null)
132             {
133                 durable = Boolean.valueOf(tempDurable);
134             }
135 
136             // Get the durable subscriber name if there is one
137             String durableName = (String) endpoint.getProperties().get(JmsConstants.DURABLE_NAME_PROPERTY);
138             if (durableName == null && durable && topic)
139             {
140                 durableName = "mule." + connector.getName() + "." + endpoint.getEndpointURI().getAddress();
141                 if (logger.isDebugEnabled())
142                 {
143                     logger.debug("Jms Connector for this receiver is durable but no durable name has been specified. Defaulting to: "
144                              + durableName);
145                 }
146             }
147 
148             // Create consumer
149             consumer = support.createConsumer(session, dest, selector, connector.isNoLocal(), durableName,
150                 topic, endpoint);
151 
152             Message message;
153 
154             if (timeout == JmsMessageDispatcher.RECEIVE_NO_WAIT)
155             {
156                 message = consumer.receiveNoWait();
157             }
158             else if (timeout == JmsMessageDispatcher.RECEIVE_WAIT_INDEFINITELY)
159             {
160                 message = consumer.receive();
161             }
162             else
163             {
164                 message = consumer.receive(timeout);
165             }
166 
167             if (message == null)
168             {
169                 return null;
170             }
171 
172             message = connector.preProcessMessage(message, session);
173             return createMuleMessage(message, endpoint.getEncoding());
174         }
175         finally
176         {
177             if (!cleanupListenerRegistered)
178             {
179                 connector.closeQuietly(consumer);
180                 connector.closeSessionIfNoTransactionActive(session);
181             }
182         }
183     }
184 
185     @Override
186     protected void doDispose()
187     {
188         // template method
189     }
190 
191 }