1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.tcp; |
8 | |
|
9 | |
import org.mule.api.MuleMessage; |
10 | |
import org.mule.api.endpoint.InboundEndpoint; |
11 | |
import org.mule.api.retry.RetryContext; |
12 | |
import org.mule.transport.AbstractMessageRequester; |
13 | |
|
14 | |
import java.net.Socket; |
15 | |
import java.net.SocketTimeoutException; |
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
public class TcpMessageRequester extends AbstractMessageRequester |
21 | |
{ |
22 | |
|
23 | |
private final TcpConnector connector; |
24 | |
|
25 | |
public TcpMessageRequester(InboundEndpoint endpoint) |
26 | |
{ |
27 | 0 | super(endpoint); |
28 | 0 | this.connector = (TcpConnector) endpoint.getConnector(); |
29 | 0 | } |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
@Override |
43 | |
protected MuleMessage doRequest(long timeout) throws Exception |
44 | |
{ |
45 | 0 | if (timeout > Integer.MAX_VALUE || timeout < 0) |
46 | |
{ |
47 | 0 | throw new IllegalArgumentException("Timeout incorrect: " + timeout); |
48 | |
} |
49 | 0 | Socket socket = connector.getSocket(endpoint); |
50 | |
try |
51 | |
{ |
52 | 0 | Object result = TcpMessageDispatcher.receiveFromSocket(socket, (int)timeout, endpoint); |
53 | 0 | if (result == null) |
54 | |
{ |
55 | 0 | return null; |
56 | |
} |
57 | 0 | return createMuleMessage(result, endpoint.getEncoding()); |
58 | |
} |
59 | 0 | catch (SocketTimeoutException e) |
60 | |
{ |
61 | |
|
62 | 0 | if (logger.isDebugEnabled()) |
63 | |
{ |
64 | 0 | logger.debug("Socket timed out normally while doing a synchronous receive on endpointUri: " |
65 | |
+ endpoint.getEndpointURI()); |
66 | |
} |
67 | 0 | return null; |
68 | |
} |
69 | |
} |
70 | |
|
71 | |
@Override |
72 | |
protected synchronized void doDispose() |
73 | |
{ |
74 | |
try |
75 | |
{ |
76 | 0 | doDisconnect(); |
77 | |
} |
78 | 0 | catch (Exception e) |
79 | |
{ |
80 | 0 | logger.error("Failed to shutdown the dispatcher.", e); |
81 | 0 | } |
82 | 0 | } |
83 | |
|
84 | |
@Override |
85 | |
protected void doConnect() throws Exception |
86 | |
{ |
87 | |
|
88 | 0 | } |
89 | |
|
90 | |
@Override |
91 | |
protected void doDisconnect() throws Exception |
92 | |
{ |
93 | |
|
94 | 0 | } |
95 | |
|
96 | |
@Override |
97 | |
public RetryContext validateConnection(RetryContext retryContext) |
98 | |
{ |
99 | 0 | Socket socket = null; |
100 | |
try |
101 | |
{ |
102 | 0 | socket = connector.getSocket(endpoint); |
103 | |
|
104 | 0 | retryContext.setOk(); |
105 | |
} |
106 | 0 | catch (Exception ex) |
107 | |
{ |
108 | 0 | retryContext.setFailed(ex); |
109 | |
} |
110 | |
finally |
111 | |
{ |
112 | 0 | if (socket != null) |
113 | |
{ |
114 | |
try |
115 | |
{ |
116 | 0 | connector.releaseSocket(socket, endpoint); |
117 | |
} |
118 | 0 | catch (Exception e) |
119 | |
{ |
120 | 0 | if (logger.isDebugEnabled()) |
121 | |
{ |
122 | 0 | logger.debug("Failed to release a socket " + socket, e); |
123 | |
} |
124 | 0 | } |
125 | |
} |
126 | |
} |
127 | |
|
128 | 0 | return retryContext; |
129 | |
} |
130 | |
} |