|
13 | 13 | import org.zeromq.SocketType; |
14 | 14 | import org.zeromq.ZMQ; |
15 | 15 | import org.zeromq.ZMQException; |
| 16 | +import zmq.ZError; |
16 | 17 |
|
17 | 18 | /** TRex Transport class to create zmq socket for connection to trex server */ |
18 | 19 | public class TRexTransport { |
@@ -133,17 +134,47 @@ public synchronized String sendJson(String json) { |
133 | 134 | try { |
134 | 135 | zmqSocket.send(compressed); |
135 | 136 | } catch (ZMQException e) { |
136 | | - throw new IllegalStateException( |
137 | | - "Did not get any response from server " |
138 | | - + getHost() |
139 | | - + " within timeout " |
140 | | - + zmqSocket.getReceiveTimeOut(), |
141 | | - e); |
| 137 | + throwTimeoutException(e); |
142 | 138 | } |
143 | 139 | byte[] msg = zmqSocket.recv(); |
| 140 | + // add this if-block to deal with the connect lost error which is caused |
| 141 | + // after a long time the client-server don't interactive |
| 142 | + if (msg == null && zmqSocket.errno() == ZError.ENOTCONN) { |
| 143 | + zmqSocket.connect(this.connectionString); |
| 144 | + try { |
| 145 | + zmqSocket.send(compressed); |
| 146 | + } catch (ZMQException e) { |
| 147 | + throwTimeoutException(e); |
| 148 | + } |
| 149 | + msg = zmqSocket.recv(); |
| 150 | + } |
| 151 | + |
| 152 | + if (msg == null) { |
| 153 | + zmqSocket.connect(this.connectionString); |
| 154 | + try { |
| 155 | + zmqSocket.send(compressed); |
| 156 | + } catch (ZMQException e) { |
| 157 | + throw new IllegalStateException( |
| 158 | + "Did not get any response from server " |
| 159 | + + getHost() |
| 160 | + + " within timeout " |
| 161 | + + zmqSocket.getReceiveTimeOut(), |
| 162 | + e); |
| 163 | + } |
| 164 | + msg = zmqSocket.recv(); |
| 165 | + } |
144 | 166 |
|
145 | 167 | String response = this.dataCompressor.decompressBytesToString(msg); |
146 | 168 | LOGGER.debug("JSON Resp: {}", response); |
147 | 169 | return response; |
148 | 170 | } |
| 171 | + |
| 172 | + private void throwTimeoutException(Exception e) { |
| 173 | + throw new IllegalStateException( |
| 174 | + "Did not get any response from server " |
| 175 | + + getHost() |
| 176 | + + " within timeout " |
| 177 | + + zmqSocket.getReceiveTimeOut(), |
| 178 | + e); |
| 179 | + } |
149 | 180 | } |
0 commit comments