View Javadoc

1   /*
2    * $Id: AbstractMessageRequester.java 12247 2008-07-07 21:25:01Z dfeist $
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;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.api.endpoint.InboundEndpoint;
15  import org.mule.api.transport.DispatchException;
16  import org.mule.api.transport.MessageRequester;
17  import org.mule.api.transport.ReceiveException;
18  import org.mule.context.notification.EndpointMessageNotification;
19  
20  /**
21   * Provide a default dispatch (client) support for handling threads lifecycle and validation.
22   */
23  public abstract class AbstractMessageRequester extends AbstractConnectable implements MessageRequester
24  {
25      
26      public AbstractMessageRequester(InboundEndpoint endpoint)
27      {
28          super(endpoint);
29      }
30  
31      /**
32       * Make a specific request to the underlying transport
33       *
34       * @param timeout the maximum time the operation should block before returning.
35       *            The call should return immediately if there is data available. If
36       *            no data becomes available before the timeout elapses, null will be
37       *            returned
38       * @return the result of the request wrapped in a MuleMessage object. Null will be
39       *         returned if no data was available
40       * @throws Exception if the call to the underlying protocol causes an exception
41       */
42      public final MuleMessage request(long timeout) throws Exception
43      {
44          try
45          {
46              // Make sure we are connected
47              connectionStrategy.connect(this);
48              MuleMessage result = doRequest(timeout);
49              if (result != null && connector.isEnableMessageEvents())
50              {
51                  connector.fireNotification(new EndpointMessageNotification(result, endpoint, null,
52                      EndpointMessageNotification.MESSAGE_REQUESTED));
53              }
54              return result;
55          }
56          catch (DispatchException e)
57          {
58              disposeAndLogException();
59              throw e;
60          }
61          catch (Exception e)
62          {
63              disposeAndLogException();
64              throw new ReceiveException(endpoint, timeout, e);
65          }
66      }
67  
68      /**
69       * Make a specific request to the underlying transport
70       *
71       * @param timeout the maximum time the operation should block before returning.
72       *            The call should return immediately if there is data available. If
73       *            no data becomes available before the timeout elapses, null will be
74       *            returned
75       * @return the result of the request wrapped in a MuleMessage object. Null will be
76       *         returned if no data was avaialable
77       * @throws Exception if the call to the underlying protocal cuases an exception
78       */
79      protected abstract MuleMessage doRequest(long timeout) throws Exception;
80  
81  }