title: Jersey Asynchronous Services Throws Error while closing the output stream in order to commit response
body: <p>I have just tried the <a href="https://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/container/AsyncResponse.html" rel="nofollow">sample resource</a> explaining the asynchronous response getting resumed by different thread which did not start the resource method.</p>
  
  <p>[Environment used:tomcat 7, jersey 2.3]
  when making post request right after get request as in the example. I have following issues:</p>
  
  <p>1) Iam getting NPE while server runtime writing the response as "error while closing output stream in order to cimmit response. ( &lt;-- some times the post request completes with out any exception)</p>
  
  <pre><code>at org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:215)
  at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:462)
  at org.apache.coyote.http11.InternalOutputBuffer.endRequest(InternalOutputBuffer.java:159)
  at org.apache.coyote.http11.AbstractHttp11Processor.action(AbstractHttp11Processor.java:749)
  at org.apache.coyote.Response.action(Response.java:173)
  at org.apache.coyote.Response.finish(Response.java:279)
  at org.apache.catalina.connector.OutputBuffer.close(OutputBuffer.java:293)
  at org.apache.catalina.connector.CoyoteOutputStream.close(CoyoteOutputStream.java:108)
  at org.glassfish.jersey.message.internal.CommittingOutputStream.close(CommittingOutputStream.java:275)
  at org.glassfish.jersey.message.internal.OutboundMessageContext.close(OutboundMessageContext.java:835)
  at org.glassfish.jersey.server.ContainerResponse.close(ContainerResponse.java:411)
  at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:625)
  at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:357)
  at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:347)
  at org.glassfish.jersey.server.ServerRuntime$AsyncResponder$3.run(ServerRuntime.java:759)
  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:295)
  at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.resume(ServerRuntime.java:797)
  at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.resume(ServerRuntime.java:754)
  </code></pre>
  
  <p>2) My client request (method type - GET), was NOT getting the asynchronous response written by the post method call. I don't get any exceptions.</p>
  
  <h2>code I tried:</h2>
  
  <pre><code>import java.text.DateFormat;
  import java.util.Date;
  import java.util.concurrent.ArrayBlockingQueue;
  import java.util.concurrent.BlockingQueue;
  
  import javax.ws.rs.GET;
  import javax.ws.rs.HEAD;
  import javax.ws.rs.POST;
  import javax.ws.rs.Path;
  import javax.ws.rs.PathParam;
  import javax.ws.rs.container.AsyncResponse;
  import javax.ws.rs.container.Suspended;
  
  import org.glassfish.jersey.server.ManagedAsync;
  
  @Path("/messages/next")
  public class MessagingResource {
      private static final BlockingQueue&lt;AsyncResponse&gt; suspended =
          new ArrayBlockingQueue&lt;AsyncResponse&gt;(5, Boolean.TRUE);
  
  @GET
  @ManagedAsync
  public void readMessage(@Suspended AsyncResponse ar) throws InterruptedException {
      echo(" readMessage: before put");
      synchronized (MessagingResource.class) {
          echo(" readMessage: before put, count :"+suspended.size());
          suspended.put(ar);
          echo(" readMessage: after put, count :"+suspended.size());
      }
      echo(" readMessage: after put");
  }
  
  @POST
  @ManagedAsync
  public String postMessage() throws InterruptedException {
  
      AsyncResponse ar = null; 
      echo(" postMessage: before take, resume");
      synchronized (MessagingResource.class) {
          echo(" postMessage: before put, count :"+suspended.size());
          ar = suspended.take();
          echo(" postMessage: after put, count :"+suspended.size());
      }
      String message = " TEST TEST";
      ar.resume(message ); // resumes the processing of one GET request
      echo(" postMessage: after take , resume");
      return "Message sent";
  }
  
  private void echo(String string) {
      String dtStr = DateFormat.getTimeInstance(DateFormat.LONG).format(new Date());
      System.out.println( dtStr + " : " +string );
  }
  </code></pre>