|Most Shared

Asynchronous REST tech coding rest java tomcat jersey asynch
11 Jul 2014at Atlanta

Sharing a simple code snippet to make a asynchronous REST service with Jersey and embedded Tomcat Gradle plugin.

This is just for a quickstart of the rituals, as I was unlucky in searching for an similar example in StackOverflow and Google.


Make sure the jersey servlet spec is 3.0 and it supports asynch

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

        <servlet-name>Jersey REST Service</servlet-name>



The jersey resource that supposedly does a slow search of key across some imaginary datasources (SearchService), so it must be made asynchronous to free the request thread. The service follows the usual drill of a timeout handler and the forked new thread that callbacks to fill response.

public class SearchApi {
    private static Logger log = LogManager.getLogger(SearchApi.class);

    private SearchService service;

    public void fetchFor(@MatrixParam("key") String key, @Suspended final AsyncResponse response){

        response.setTimeoutHandler(r ->  r.resume(Response.status(SERVICE_UNAVAILABLE).entity("search timed out.").build()));
        response.setTimeout(20, SECONDS);
        response.register((CompletionCallback) error -> {
            if( error != null) log.error("search error:|key="+key+"|error="+error);
            else log.info("search complete:|key="+key);

        new Thread(()->{
            List<SearchView> views = service.fetch(key);


The DTO going over the wire as json from the service to client

public class SearchView {
    private String title;
    private String uri;


Use a tomcat plugin, plug the below script into your build.gradle, tomcat version should be 7+ to support Servlet 3.0

Run 'gradle tomcatRunWar'

buildscript {
    repositories {
        mavenRepo(url: 'http://jcenter.bintray.com')
    dependencies {
        classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:1.2.4'
apply plugin: 'tomcat'

dependencies {
    def tomcatVersion = '7.0.54'
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
    tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") {
        exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj'
tomcat {
    httpPort = 8080
    httpsPort = 8091
    enableSSL = true


        $ curl http://localhost:8080/tasker/rest/search;key=12
comments powered by Disqus

All content except noted photos and videos copyright © Vijayaraj Chakravarthy. All rights reserved. *Any images or videos not listed as mine are copyright to their respective owners and were used under creative common license or fair use standards. If a photo or video is your material and you do not wish it to be on the site, please email me vijayrc@outlook.com and I will remove it immediately.