Using Asynchronous timeouts in the Java WebSocket API

Sending messages in an asynchronous manner avoid blocking the sending thread. This is a great where your solution needs to scale in order to support a large number of clients.

But there is a limit on how long can we wait for the asynchronous process to complete

The Java WebSocket API gives you a few options in this regard

Async Timeout support

  • first and foremost, there is a notion of a timeout and this can be configured using the setSendTimeout method in the RemoteEndpoint.Async interface
  • secondly, the failure result manifests itself using the Future object or java.websocket.SendResult

How do timeouts manifest ?

It depends on which strategy you’re using in order to send your messages

  • Callback based
  • Future (java.util.concurrent) based

In case you are using the java.websocket.SendHandler i.e. the callback handler route, the timeout exception details will be available via SendResult.getException()

If you chose to use the Future to track the completion, calling it get method will result in a java.util.concurrent.ExecutionException

Further reading

Cheers!

Advertisements

About Abhishek

Java EE & distributed systems junkie who frequently blogs at abhirockzz.wordpress.com as well as simplydistributed.wordpress.com. Oh, I have also authored a few (mini) books, articles, Refcards etc. :-)
This entry was posted in Java, Java EE and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s