Asynchronous REST Service with Timeout using Jersey

Here I am going to give an example on how Asynchronous REST webservice with Timeout works.

Introduction

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).

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.

Prerequisites

Eclipse, JDK 1.6

For more information on Asynchronous Service please go through https://jersey.java.net/documentation/latest/async.html

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.

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.

Step 1. Create maven webapp project in Eclipse with the below information

Group Id: in.webtuts
Artifact Id: rest

Step 2. Modify the pom.xml file to use jar libraries

 

Step 3. Modify web.xml file to use the jersey servlet

 

Step 4. Create a REST resource class as shown below

 

Step 5. Create JUnit test class for testing the service

 

Step 6. Run the JUnit test class.

Console output

 

That’s all. Thanks for your reading.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.