Asynchronous REST Service using Jersey – Completion Callback


Here I am going to give an example on how Asynchronous REST webservice with Completion Callback. The most important concept in REST is resources, which are identified by global IDs — typically using URIs. Client applications use HTTP methods (GET/ POST/ PUT/ DELETE) to manipulate the resource or collection of resources. A RESTful Web service is implemented using HTTP and the principles of REST. Typically, a RESTful Web service should define the following aspects:

  • The base/root URI for the Web service such as http://<host>/<appcontext/contextpath>/<url pattern>/<resources>.
  • The MIME type of the response data supported, which are JSON/XML/TEXT/HTML etc.
  • The set of operations supported by the service. (for example, POST, GET, PUT or DELETE).

HTTP Methods

HTTP methods are mapped to CRUD (create, read, update and delete) actions for a resource. Although you can make slight modifications such as making the PUT method to be create or update, the basic patterns are listed as follows.

  • HTTP GET: Get/List/Retrieve an individual resource or a collection of resources.
  • HTTP POST: Create a new resource or resources.
  • HTTP PUT: Update an existing resource or collection of resources.
  • HTTP DELETE: Delete a resource or collection of resources.


Eclipse, JDK 1.8, Maven 3.6.0, Jersey 2.6

For more information on Asynchronous Service please go through

Asynchronous Server API

By default client connection of a request is processed in a synchronous mode in server in a single I/O container thread. After processing the client request, the thread returns to the I/O container and I/O container safely assumes that the request process is finished. Thus all resources associated with the client connection are released. This kind of synchronous mechanism is sufficient for requests which take relatively short execution time. But for requests which take relatively longer execution time, the association between a request processing thread and client connection is broken. Thus server-side asynchronous processing model should be used to facilitate explicitly suspend, resume and close the client connections.

Example with Source Code

Now I will go to the coding part and below steps to be followed to create an asynchronous REST servive. We will use grizlly web server for running the service.

Creating Project

Create maven webapp project in Eclipse with the below information:

Group Id: com.roytuts
Artifact Id: asynchronous-rest-completion-callback

Updating pom.xml

Modify the pom.xml file to have the similar dependencies and plugin.

<project xmlns=""

Updating web.xml

Modify web.xml file to use the jersey servlet.

We also specify the location of REST resource classes.

<web-app xmlns=""
	<display-name>Asynchronous REST- Completion Callback</display-name>
	<!-- Map /rest/* to Jersey -->

Creating REST Class

Create a REST resource class as shown below:

public class AsyncResource {
	private static int numberOfSuccessResponses = 0;
	private static int numberOfFailures = 0;
	private static Throwable lastException = null;
	public void asyncGetCompletionCallback(@Suspended final AsyncResponse asyncResponse) {
		asyncResponse.register(new CompletionCallback() {
			public void onComplete(Throwable throwable) {
				if (throwable == null) {
					// no throwable - the processing ended successfully
					// (response already written to the client)
				} else {
					lastException = throwable;
		new Thread(new Runnable() {
			public void run() {
				String result = veryExpensiveOperation();
			private String veryExpensiveOperation() {
				return "Very Expensive Operation with Completion Callback";

Creating Junit Class

Create JUnit test class for testing the service

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class AsyncResourceTest {
	private HttpServer httpServer;
	private WebTarget webTarget;
	private static final URI baseUri = URI.create("http://localhost:9090/rest/");
	public void setup() throws Exception {
		// create ResourceConfig from Resource class
		ResourceConfig rc = new ResourceConfig(AsyncResource.class);
		// create the Grizzly server instance
		httpServer = GrizzlyHttpServerFactory.createHttpServer(baseUri, rc);
		// start the server
		// configure client with the base URI path
		Client client = ClientBuilder.newClient();
		webTarget =;
	public void tearDown() throws Exception {
		// if you want to stop the server from the input through keyboard then uncomment
		// below two lines
		// System.out.println(String.format("Application started.%nHit enter to stop it..."));
		// stop the server
	public void testAsyncGetCompletionCallback() throws InterruptedException, ExecutionException {
		final AsyncInvoker asyncInvoker = webTarget.path("resource/asyncCompCallback").request().async();
		final Future<Response> responseFuture = asyncInvoker.get();
		System.out.println("Request is being processed asynchronously.");
		final Response response = responseFuture.get();
		// get() waits for the response to be ready
		System.out.println("Response received : " + response);
		System.out.println("Response from GET method : " + response.readEntity(String.class));

Testinh the Application

Run the JUnit test class.

Console output

Request is being processed asynchronously.
Response received : InboundJaxrsResponse{ClientResponse{method=GET, uri=http://localhost:9090/rest/resource/asyncCompCallback, status=200, reason=OK}}
Response from GET method : Very Expensive Operation with Completion Callback

Source Code

download source code

That’s all. Thanks for reading.

Leave a Reply

Your email address will not be published.