Reputation: 157
I wrote some logic which represent near 200 websocet connection with exchange at the same time.I use third party api and it based on org.eclipse.jetty.websocket.api. I have this method which i had to override.
try {
URI uri = new URI(websocketBaseUrl + url);
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setTrustAll(true);
WebSocketClient client = new WebSocketClient(sslContextFactory);
client.setMaxIdleTimeout(0);
client.start();
return client.connect(adapter, uri).get();
} catch (URISyntaxException e) {
throw new BinanceApiException("URL Syntax error: " + e.getMessage());
} catch (Throwable e) {
throw new BinanceApiException("Websocket error: " + e.getMessage());
}
I added setIdleTimeout to it so that the connection is not lost when i don't recieve information for a long time. Exchange close connection one time a day but for 2,3 sometimes 4 days it have been reconnected.But finally i get this:
java.nio.channels.ClosedChannelException: null at org.eclipse.jetty.io.WriteFlusher.onClose(WriteFlusher.java:507) at org.eclipse.jetty.io.ssl.SslConnection$DecryptedEndPoint.onIncompleteFlush(SslConnection.java:527) at org.eclipse.jetty.io.AbstractEndPoint$2.onIncompleteFlush(AbstractEndPoint.java:54) at org.eclipse.jetty.io.WriteFlusher.write(WriteFlusher.java:331) at org.eclipse.jetty.io.AbstractEndPoint.write(AbstractEndPoint.java:372) at org.eclipse.jetty.websocket.common.io.FrameFlusher$Flusher.flush(FrameFlusher.java:153) at org.eclipse.jetty.websocket.common.io.FrameFlusher$Flusher.process(FrameFlusher.java:217) at org.eclipse.jetty.util.IteratingCallback.processing(IteratingCallback.java:241) at org.eclipse.jetty.util.IteratingCallback.iterate(IteratingCallback.java:224) at org.eclipse.jetty.websocket.common.io.FrameFlusher.enqueue(FrameFlusher.java:382) at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.outgoingFrame(AbstractWebSocketConnection.java:614) at org.eclipse.jetty.websocket.client.io.WebSocketClientConnection.outgoingFrame(WebSocketClientConnection.java:72) at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.onConnectionStateChange(AbstractWebSocketConnection.java:473) at org.eclipse.jetty.websocket.common.io.IOState.notifyStateListeners(IOState.java:184) at org.eclipse.jetty.websocket.common.io.IOState.onReadFailure(IOState.java:498) at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.readParse(AbstractWebSocketConnection.java:666) at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.onFillable(AbstractWebSocketConnection.java:511) at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279) at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:104) at org.eclipse.jetty.io.ssl.SslConnection.onFillable(SslConnection.java:289) at org.eclipse.jetty.io.ssl.SslConnection$3.succeeded(SslConnection.java:149) at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:104) at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:247) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:140) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131) at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:243) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:679) at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:597) at java.lang.Thread.run(Thread.java:748)
I find this question on stackoverflow but i can't see clear answer. Pls help.Thanks in advance.
Upvotes: 0
Views: 1033
Reputation: 131
If you want the connection to stay open if idle, you should configure your client this way:
client.setMaxIdleTimeout(Long.MAX_VALUE);
Setting maxIdleTimeout to 0 would have the opposite effect: Closing the connection as soon as it becomes idle.
Upvotes: 2