View Javadoc

1   /*
2    * $Id: JmsMessageRequester.java 22718 2011-08-23 06:37:20Z mike.schilling $
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             JmsSelectorFilter selectorFilter = connector.getSelector(endpoint);
112             if (selectorFilter != null)
113             {
114                 final String expressionTemplate = selectorFilter.getExpression();
115                 if (StringUtils.isNotBlank(expressionTemplate))
116                 {
117                     selector = connector.getMuleContext().getExpressionManager().parse(expressionTemplate, null);
118                 }
119             }
120             else if (endpoint.getProperties() != null)
121             {
122                 // still allow the selector to be set as a property on the endpoint
123                 // to be backward compatable
124                 final String expressionTemplate = (String) endpoint.getProperty(JmsConstants.JMS_SELECTOR_PROPERTY);
125                 if (StringUtils.isNotBlank(expressionTemplate))
126                 {
127                     selector = connector.getMuleContext().getExpressionManager().parse(expressionTemplate, null);
128                 }
129             }
130             String tempDurable = (String) endpoint.getProperties().get(JmsConstants.DURABLE_PROPERTY);
131             boolean durable = connector.isDurable();
132             if (tempDurable != null)
133             {
134                 durable = Boolean.valueOf(tempDurable);
135             }
136 
137             // Get the durable subscriber name if there is one
138             String durableName = (String) endpoint.getProperties().get(JmsConstants.DURABLE_NAME_PROPERTY);
139             if (durableName == null && durable && topic)
140             {
141                 durableName = "mule." + connector.getName() + "." + endpoint.getEndpointURI().getAddress();
142                 if (logger.isDebugEnabled())
143                 {
144                     logger.debug("Jms Connector for this receiver is durable but no durable name has been specified. Defaulting to: "
145                              + durableName);
146                 }
147             }
148 
149             // Create consumer
150             consumer = support.createConsumer(session, dest, selector, connector.isNoLocal(), durableName,
151                 topic, endpoint);
152 
153             Message message;
154 
155             if (timeout == JmsMessageDispatcher.RECEIVE_NO_WAIT)
156             {
157                 message = consumer.receiveNoWait();
158             }
159             else if (timeout == JmsMessageDispatcher.RECEIVE_WAIT_INDEFINITELY)
160             {
161                 message = consumer.receive();
162             }
163             else
164             {
165                 message = consumer.receive(timeout);
166             }
167 
168             if (message == null)
169             {
170                 return null;
171             }
172 
173             message = connector.preProcessMessage(message, session);
174             return createMuleMessage(message, endpoint.getEncoding());
175         }
176         finally
177         {
178             if (!cleanupListenerRegistered)
179             {
180                 connector.closeQuietly(consumer);
181                 connector.closeSessionIfNoTransactionActive(session);
182             }
183         }
184     }
185 
186     @Override
187     protected void doDispose()
188     {
189         // template method
190     }
191 
192 }