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