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