View Javadoc

1   /*
2    * $Id: TcpMessageRequester.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.tcp;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.api.endpoint.InboundEndpoint;
15  import org.mule.api.retry.RetryContext;
16  import org.mule.transport.AbstractMessageRequester;
17  
18  import java.net.Socket;
19  import java.net.SocketTimeoutException;
20  
21  /**
22   * Request transformed Mule events from TCP.
23   */
24  public class TcpMessageRequester extends AbstractMessageRequester
25  {
26  
27      private final TcpConnector connector;
28  
29      public TcpMessageRequester(InboundEndpoint endpoint)
30      {
31          super(endpoint);
32          this.connector = (TcpConnector) 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 protocal cuases an exception
45       */
46      @Override
47      protected MuleMessage doRequest(long timeout) throws Exception
48      {
49          if (timeout > Integer.MAX_VALUE || timeout < 0)
50          {
51              throw new IllegalArgumentException("Timeout incorrect: " + timeout);
52          }
53          Socket socket = connector.getSocket(endpoint);
54          try
55          {
56              Object result = TcpMessageDispatcher.receiveFromSocket(socket, (int)timeout, endpoint);
57              if (result == null)
58              {
59                  return null;
60              }
61              return createMuleMessage(result, endpoint.getEncoding());
62          }
63          catch (SocketTimeoutException e)
64          {
65              // we don't necesarily expect to receive a resonse here
66              if (logger.isDebugEnabled())
67              {
68                  logger.debug("Socket timed out normally while doing a synchronous receive on endpointUri: "
69                      + endpoint.getEndpointURI());
70              }
71              return null;
72          }
73      }
74  
75      @Override
76      protected synchronized void doDispose()
77      {
78          try
79          {
80              doDisconnect();
81          }
82          catch (Exception e)
83          {
84              logger.error("Failed to shutdown the dispatcher.", e);
85          }
86      }
87  
88      @Override
89      protected void doConnect() throws Exception
90      {
91          // nothing, there is an optional validation in validateConnection()
92      }
93  
94      @Override
95      protected void doDisconnect() throws Exception
96      {
97          //nothing to do
98      }
99  
100     @Override
101     public RetryContext validateConnection(RetryContext retryContext)
102     {
103         Socket socket = null;
104         try
105         {
106             socket = connector.getSocket(endpoint);
107 
108             retryContext.setOk();
109         }
110         catch (Exception ex)
111         {
112             retryContext.setFailed(ex);
113         }
114         finally
115         {
116             if (socket != null)
117             {
118                 try
119                 {
120                     connector.releaseSocket(socket, endpoint);
121                 }
122                 catch (Exception e)
123                 {
124                     if (logger.isDebugEnabled())
125                     {
126                         logger.debug("Failed to release a socket " + socket, e);
127                     }
128                 }
129             }
130         }
131 
132         return retryContext;
133     }
134 }