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?

coolguy
  • 3,011
  • 2
  • 18
  • 35

1 Answers1

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.

Community
  • 1
  • 1
mattm
  • 5,851
  • 11
  • 47
  • 77
  • 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 – coolguy Jan 05 '17 at 18:57