View Javadoc

1   /*
2    * $Id: VMMessageRequester.java 11586 2008-04-17 14:33:16Z 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.vm;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.api.ThreadSafeAccess;
15  import org.mule.api.endpoint.InboundEndpoint;
16  import org.mule.transport.AbstractMessageRequester;
17  import org.mule.util.queue.Queue;
18  import org.mule.util.queue.QueueSession;
19  
20  /**
21   * <code>VMMessageDispatcher</code> is used for providing in memory interaction
22   * between components.
23   */
24  public class VMMessageRequester extends AbstractMessageRequester
25  {
26  
27      private final VMConnector connector;
28  
29      public VMMessageRequester(InboundEndpoint endpoint)
30      {
31          super(endpoint);
32          this.connector = (VMConnector) endpoint.getConnector();
33      }
34  
35      /**
36       * Make a specific request to the underlying transport
37       *
38       * @param timeout the maximum time the operation should block before returning.
39       *                The call should return immediately if there is data available. If
40       *                no data becomes available before the timeout elapses, null will be
41       *                returned
42       * @return the result of the request wrapped in a MuleMessage object. Null will be
43       *         returned if no data was available
44       * @throws Exception if the call to the underlying protocol causes an exception
45       */
46      protected MuleMessage doRequest(long timeout) throws Exception
47      {
48          if (!connector.isQueueEvents())
49          {
50              throw new UnsupportedOperationException("Receive requested on VM Connector, but queueEvents is false");
51          }
52  
53          try
54          {
55              QueueSession queueSession = connector.getQueueSession();
56              Queue queue = queueSession.getQueue(endpoint.getEndpointURI().getAddress());
57  
58              if (queue == null)
59              {
60                  if (logger.isDebugEnabled())
61                  {
62                      logger.debug("No queue with name " + endpoint.getEndpointURI().getAddress());
63                  }
64                  return null;
65              }
66              else
67              {
68                  MuleMessage message = null;
69                  if (logger.isDebugEnabled())
70                  {
71                      logger.debug("Waiting for a message on " + endpoint.getEndpointURI().getAddress());
72                  }
73                  try
74                  {
75                      message = (MuleMessage) queue.poll(timeout);
76                  }
77                  catch (InterruptedException e)
78                  {
79                      logger.error("Failed to receive message from queue: " + endpoint.getEndpointURI());
80                  }
81                  if (message != null)
82                  {
83                      //The message will contain old thread information, we need to reset it
84                      if(message instanceof ThreadSafeAccess)
85                      {
86                          //TODO: would it be ok just to reset access control here? i.e.
87                          ((ThreadSafeAccess)message).resetAccessControl();
88  //                        message = (MuleMessage)((ThreadSafeAccess)message).newThreadCopy();
89                      }
90                      if (logger.isDebugEnabled())
91                      {
92                          logger.debug("Message received: " + message);
93                      }
94                      return message;
95                  }
96                  else
97                  {
98                      if (logger.isDebugEnabled())
99                      {
100                         logger.debug("No event received after " + timeout + " ms");
101                     }
102                     return null;
103                 }
104             }
105         }
106         catch (Exception e)
107         {
108             throw e;
109         }
110     }
111 
112     protected void doDispose()
113     {
114         // template method
115     }
116 
117     protected void doConnect() throws Exception
118     {
119         if (connector.isQueueEvents())
120         {
121             // use the default queue profile to configure this queue.
122             connector.getQueueProfile().configureQueue(
123                     endpoint.getEndpointURI().getAddress(), connector.getQueueManager());
124         }
125     }
126 
127     protected void doDisconnect() throws Exception
128     {
129         // template method
130     }
131 
132 }