DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • How to Merge HTML Documents in Java
  • Designing a Java Connector for Software Integrations
  • Jakarta NoSQL 1.0: A Way To Bring Java and NoSQL Together
  • How to Get Word Document Form Values Using Java

Trending

  • Navigating the LLM Landscape: A Comparative Analysis of Leading Large Language Models
  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • Debugging With Confidence in the Age of Observability-First Systems
  • Creating a Web Project: Caching for Performance Optimization
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Use Asynchronous Timeouts in the Java Websocket API

How to Use Asynchronous Timeouts in the Java Websocket API

In this post we take a look at how to deal with timeouts when using the Java WebSocket API. Read on to find out how and for some example code.

By 
Abhishek Gupta user avatar
Abhishek Gupta
DZone Core CORE ·
Dec. 10, 16 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
12.0K Views

Join the DZone community and get the full member experience.

Join For Free

Sending messages in an asynchronous manner avoids blocking the sending thread. This is great when 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. 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-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(). 

....
public void broadcast(Session s, String msg){
  RemoteEndpoint asyncHandle = s.getRemoteAsync();
  asyncHandle.setSendTimeout(1000); //1 second
  asyncHandle.sendText(msg, 
    new SendHandler(){
      @Override
        public void onResult(SendResult result) {
        if(!result.isOK()){
        System.out.println("Async send failure: "+ result.getException());
        }
    }
  }); //will timeout after 2 seconds
  tracker.get(); //will throw java.util.ExecutionException if the process had timed out
}
....

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

....
public void broadcast(Session s, String msg){
  RemoteEndpoint asyncHandle = s.getRemoteAsync();
  asyncHandle.setSendTimeout(2000); //2000 ms
  Future<Void> tracker = asyncHandle.sendText(msg); //will timeout after 2 seconds
  tracker.get(); //will throw java.util.ExecutionException if the process had timed out
}
....

Further Reading

  • Java WebSocket API specification.
  • Other WebSocket blogs.
API Timeout (computing) WebSocket Java (programming language)

Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Merge HTML Documents in Java
  • Designing a Java Connector for Software Integrations
  • Jakarta NoSQL 1.0: A Way To Bring Java and NoSQL Together
  • How to Get Word Document Form Values Using Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!