By default there is no timeout defined for this so called heavy processing. This is where the “TimeoutHandler” event handler comes into action. Consider the following code:


In the above code whenever the heavy operation processing takes more than 40 seconds, the processing is canceled, resources are released and the “handleTimeout()” method is called. Finally, the Http 503 response code will be returned. To show various mechanisms that are offered by JAVA EE for concurrency and asynchronous processing support, the “ManagedThreadFactory” is used this time. “ManagedThreadFactory” is used whenever an explicit creation and consumption of a thread is required in the JAVA EE runtime environment. As you can see, the actual thread is created as is done in a normal JAVA SE program. As the “Managed” name indicates, the JAVA EE environment manages the execution and life time of this thread.

Another useful feature that is offered by the JAX-RS async API is the asynchronous server side call backs “CompletionCallback / ConnectionCallback”. By registering the AsyncResponse with “CompletionCallback”, whenever a request is finished or failed, the onComplete() method will be called. Similarly by registering the AsyncResponse with “ConnectionCallback”, whenever a connection to a client is closed or failed, the onDisconnect() method will be called. This come in handy whenever monitoring and logging of the runtime operations are needed. Consider the following code.


Again a new utility for concurrency was used. A runnable object is submitted to and the task will be executed asynchronously.

Pluralsight Building Asynchronous Restful Services With Jersey City

Besides the Server side async API, The client side async API is also supported by JAX-RS 2.0. Clients can use this API for asynchronous request response processing. Consider the following code:



The GET is called on async method rather than the request. This changes the synchronous call to an asynchronous one. Instead of responding synchronously, the async method returns a FUTURE object. By calling the get method, the call blocks until the response is ready. The Future.get() will be returned whenever the response is ready.

Again there are callback methods that are used in the async client side and complete the asynchronous scenario. The InvocationCallback interface provides two methods, completed() and failed(). The Completed method is called whenever the processing is finished and the response is received. If you are familiar with FUTURE object, the Completed callback method frees you from checking the isDone() method constantly until the response is ready. Conversely the Failed() method is called whenever the request processing is not successful. Consider the following code:


To some up, Asynchronous API is perfectly supported by JAX-RS 2.0. In this article various methods and mechanisms to use JAX-RS async API were introduced. Asynchronous system design improves system scalability and division of resources. Higher THROUGPUT is one of the remarkable product of following such programming approach.


Pluralsight Building Asynchronous Restful Services With Jersey Shore

Restful

Like This Article? Read More From DZone

java ,jax-rs
Opinions expressed by DZone contributors are their own.
Learn the fundamentals of Java EE 8 APIs to build effective web services

Key Features

Book Description

Java Enterprise Edition is one of the leading application programming platforms for enterprise Java development. With Java EE 8 finally released and the first application servers now available, it is time to take a closer look at how to develop modern and lightweight web services with the latest API additions and improvements.
Building RESTful Web Services with Java EE 8 is a comprehensive guide that will show you how to develop state-of-the-art RESTful web services with the latest Java EE 8 APIs. You will begin with an overview of Java EE 8 and the latest API additions and improvements. You will then delve into the details of implementing synchronous RESTful web services and clients with JAX-RS. Next up, you will learn about the specifics of data binding and content marshalling using the JSON-B 1.0 and JSON-P 1.1 APIs.

Pluralsight Building Asynchronous Restful Services With Jerseys

This book also guides you in leveraging the power of asynchronous APIs on the server and client side, and you will learn to use server-sent events (SSEs) for push communication. The final section covers advanced web service topics such as validation, JWT security, and diagnosability.
By the end of this book, you will have implemented several working web services and have a thorough understanding of the Java EE 8 APIs required for lightweight web service development.

What you will learn

Who this book is for

If you're a Java developer who wants to learn how to implement web services using the latest Java EE 8 APIs, this book is for you. Though no prior knowledge of Java EE 8 is required, experience with a previous Java EE version will be beneficial.
Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.