title: How to use Asynchronous Callbacks in Jersey 2 in tomcat 7
body: <p>How to use asynchronous callbacks of jersey 2 in tomcat server.
  I found some examples in <strong>Jersey manual : <a href="https://jersey.java.net/documentation/latest/user-guide.html#d0e8496" rel="noreferrer">https://jersey.java.net/documentation/latest/user-guide.html#d0e8496</a></strong>.</p>
  
  <p>But the issue occurs when I'm tring to test following code with this: <strong><a href="https://jersey.java.net/documentation/latest/user-guide.html#d0e8615" rel="noreferrer">https://jersey.java.net/documentation/latest/user-guide.html#d0e8615</a></strong> code as mentioned on Jersey 2 Manual. </p>
  
  <p><strong>-------------------------------------------------------------------------------------------------------------------------------</strong></p>
  
  <p><strong>As, manual version has been changed, I'm posting the codes over here:</strong></p>
  
  <p><strong>This is my Service:</strong></p>
  
  <pre><code>            import javax.ws.rs.GET; 
              import javax.ws.rs.Path; 
              import javax.ws.rs.Produces; 
              import javax.ws.rs.container.AsyncResponse; 
              import javax.ws.rs.container.CompletionCallback; 
              import javax.ws.rs.container.Suspended; 
              import javax.ws.rs.core.MediaType; 
              import javax.ws.rs.core.Response; 
  
              @Path("/resource") 
              public class AsyncResource { 
                  private static int numberOfSuccessResponses = 0; 
                  private static int numberOfFailures = 0; 
                  private static Throwable lastException = null; 
  
                  @GET 
                  public void asyncGetWithTimeout(@Suspended final AsyncResponse asyncResponse) { 
                  System.out.println("AsyncResource.asyncGetWithTimeout()"); 
                      asyncResponse.register(new CompletionCallback() { 
                          @Override 
                          public void onComplete(Throwable throwable) { 
                              if (throwable == null) { 
                                  // no throwable - the processing ended successfully 
                                  // (response already written to the client) 
                                  numberOfSuccessResponses++; 
                              } else { 
                                  numberOfFailures++; 
                                  lastException = throwable; 
                              } 
                          } 
                      }); 
  
                      new Thread(new Runnable() { 
                          @Override 
                          public void run() { 
                              String result = veryExpensiveOperation(); 
                              asyncResponse.resume(result); 
                          } 
  
                          private String veryExpensiveOperation() { 
                              // ... very expensive operation 
                          return "Hi"; 
                          } 
                      }).start(); 
                  } 
              }
  </code></pre>
  
  <p><strong>This is client:</strong></p>
  
  <pre><code>    ClientConfig clientConfig = new ClientConfig(); 
      Client client = ClientBuilder.newClient(clientConfig); 
  
      WebTarget webTarget = client.target("http://localhost:8080/Jersey2.5Service/rest"); 
      WebTarget target = webTarget.path("resource"); 
  
      final AsyncInvoker asyncInvoker = target 
                      .request().async(); 
      final Future&lt;Response&gt; 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.readEntity(String.class));
  </code></pre>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;servlet&gt;  
        &lt;servlet-name&gt;HelloServlet&lt;/servlet-name&gt;  
          &lt;servlet-class&gt;  
           org.glassfish.jersey.servlet.ServletContainer  
         &lt;/servlet-class&gt;  
         &lt;init-param&gt;  
           &lt;param-name&gt;jersey.config.server.provider.packages&lt;/param-name&gt;  
           &lt;param-value&gt;main.java&lt;/param-value&gt;  
         &lt;/init-param&gt;  
         &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;  
       &lt;/servlet&gt;  
       &lt;servlet-mapping&gt;  
         &lt;servlet-name&gt;HelloServlet&lt;/servlet-name&gt;  
         &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;  
       &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p><strong>These are the jars I'm using:</strong></p>
  
  <pre><code>    asm-all-repackaged-2.2.0-b21.jar 
      cglib-2.2.0-b21.jar 
      guava-14.0.1.jar 
      hk2-api-2.2.0-b21.jar 
      hk2-locator-2.2.0-b21.jar 
      hk2-utils-2.2.0-b21.jar 
      javax.annotation-api-1.2.jar 
      javax.inject-2.2.0-b21.jar 
      javax.servlet-api-3.0.1.jar 
      javax.ws.rs-api-2.0.jar 
      jaxb-api-2.2.7.jar 
      jersey-client.jar 
      jersey-common.jar 
      jersey-container-servlet-core.jar 
      jersey-container-servlet.jar 
      jersey-server.jar 
      org.osgi.core-4.2.0.jar 
      osgi-resource-locator-1.0.1.jar 
      persistence-api-1.0.jar 
      servlet-api-3.0.jar 
      validation-api-1.1.0.Final.jar 
  </code></pre>
  
  <p>These errors are coming:</p>
  
  <pre><code>Jan 31, 2014 4:06:53 PM org.glassfish.jersey.servlet.internal.ResponseWriter suspend
  WARNING: Attempt to put servlet request into asynchronous mode has failed. Please check your servlet configuration - all Servlet instances and Servlet filters involved in the request processing must explicitly declare support for asynchronous request processing.
  java.lang.IllegalStateException: Not supported.
      at org.apache.catalina.connector.Request.startAsync(Request.java:1676)
      at org.apache.catalina.connector.RequestFacade.startAsync(RequestFacade.java:1031)
      at org.glassfish.jersey.servlet.async.AsyncContextDelegateProviderImpl$ExtensionImpl.suspend(AsyncContextDelegateProviderImpl.java:87)
      at org.glassfish.jersey.servlet.internal.ResponseWriter.suspend(ResponseWriter.java:120)
      at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.suspend(ServerRuntime.java:758)
      at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:330)
      at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:106)
      at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:259)
      at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
      at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
      at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318)
      at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
      at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1010)
      at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
      at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:382)
      at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:345)
      at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:220)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  
  Jan 31, 2014 4:06:53 PM org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet [HelloServlet] in context with path [/Jersey2.5Service] threw exception [javax.ws.rs.ProcessingException: Attempt to suspend a connection of an asynchronous request failed in the underlying container.] with root cause
  javax.ws.rs.ProcessingException: Attempt to suspend a connection of an asynchronous request failed in the underlying container.
      at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:331)
      at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:106)
      at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:259)
      at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
      at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
      at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318)
      at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
      at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1010)
      at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
      at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:382)
      at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:345)
      at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:220)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>