1

I'm using javax.websocket API in my app. I send messages from server to client like this:

Future<Void> messageFuture = session.getAsyncRemote().sendText(message);
messageFutures.add(messageFuture); // List<Future<Void>> messageFutures

I use async API because I really care about performance and cannot make server wait until each message is delivered, because server does smth like this:

for (i = 1..N) {
 result = doStuff()
 sendMessage(result)
}

So it is impossible to wait for message delivery each iteration.

After I send all the messages I need to wait for all the Future's to be finished (all messages are delivered). And to be safe I need to use some timeout like "if server sends message to client and client doesn't confirm receipt in 30 seconds then consider websocket connection broken" - as far as I understand it should be possible to do with websockets since they work over TCP.

There is a method session.setMaxIdleTimeout(long):

Set the non-zero number of milliseconds before this session will be closed by the container if it is inactive, ie no messages are either sent or received. A value that is 0 or negative indicates the session will never timeout due to inactivity.

but I really not sure if it is what I want (is it?). So how can I set a timeout like I described using javax.websocket API?

asked Jan 5, 2017 at 16:17

1 Answer 1

1

The idle timeout could cover your case, but it is not designed to. The idle timeout applies more to the case where a client makes a connection, but is using it only infrequently.

The more precise feature for checking a timeout when sending is setAsyncSendTimeout.

Using both of these allows you to configure for the case where a client may leave a connection idle for minutes at a time, but the server expects relatively quick messages acknowledgements.


In my experience with Spring, the timeout implementation provided by Spring is not actually configurable. See How do you quickly close a nonresponsive websocket in Java Spring Tomcat? I am not sure whether this is applicable to your websocket implementation.

answered Jan 5, 2017 at 18:37
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! seems like this is exactly what I need, smth like ContainerProvider.getWebSocketContainer().setAsyncSendTimeout(30_000) should do the thing. Fortunately (or not) my app isn't built on Spring

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.