View Javadoc

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