01: /*
02: * $Id: AbstractMessageRequester.java 10961 2008-02-22 19:01:02Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
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.MessageNotification;
19:
20: /**
21: * Provide a default dispatch (client) support for handling threads lifecycle and validation.
22: */
23: public abstract class AbstractMessageRequester extends
24: AbstractConnectable implements MessageRequester {
25:
26: public AbstractMessageRequester(InboundEndpoint endpoint) {
27: super (endpoint);
28: }
29:
30: /**
31: * Make a specific request to the underlying transport
32: *
33: * @param timeout the maximum time the operation should block before returning.
34: * The call should return immediately if there is data available. If
35: * no data becomes available before the timeout elapses, null will be
36: * returned
37: * @return the result of the request wrapped in a MuleMessage object. Null will be
38: * returned if no data was avaialable
39: * @throws Exception if the call to the underlying protocal cuases an exception
40: */
41: public final MuleMessage request(long timeout) throws Exception {
42: try {
43: // Make sure we are connected
44: connectionStrategy.connect(this );
45: MuleMessage result = doRequest(timeout);
46: if (result != null && connector.isEnableMessageEvents()) {
47: connector.fireNotification(new MessageNotification(
48: result, endpoint, null,
49: MessageNotification.MESSAGE_REQUESTED));
50: }
51: return result;
52: } catch (DispatchException e) {
53: disposeAndLogException();
54: throw e;
55: } catch (Exception e) {
56: disposeAndLogException();
57: throw new ReceiveException(endpoint, timeout, e);
58: }
59: }
60:
61: /**
62: * Make a specific request to the underlying transport
63: *
64: * @param timeout the maximum time the operation should block before returning.
65: * The call should return immediately if there is data available. If
66: * no data becomes available before the timeout elapses, null will be
67: * returned
68: * @return the result of the request wrapped in a MuleMessage object. Null will be
69: * returned if no data was avaialable
70: * @throws Exception if the call to the underlying protocal cuases an exception
71: */
72: protected abstract MuleMessage doRequest(long timeout)
73: throws Exception;
74:
75: }
|