3
votes

I couldn't get ChunkedOutput working using JAX-RS Jersey 2.3.1 with Tomcat 7. I am trying to follow the tutorial from Jersey Tutorial - Chapter 10. Asynchronous Services and Clients

My code looks like the following

@GET
@Path("/asynchronous")
public ChunkedOutput<String> getAsyncResponse() {

    final ChunkedOutput<String> output = new ChunkedOutput<String>(String.class);

    new Thread() {
        public void run() {
            try {
                String text;
                for (int i = 0; i < 20; i++) {
                    text = String.valueOf(i);
                    output.write(text);
                    Thread.sleep(1000);
                }
            } catch (Throwable th) {
                th.printStackTrace();
            } finally {
                try {
                    output.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }.start();

    return output;
}

When I try to invoke it whether via curl or my Java client, I got the following exception on the web service end.

org.glassfish.jersey.server.internal.process.MappableException: java.lang.NullPointerException
    at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:96)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:149)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1139)
    at org.glassfish.jersey.server.ChunkedOutput$1.call(ChunkedOutput.java:148)
    at org.glassfish.jersey.server.ChunkedOutput$1.call(ChunkedOutput.java:121)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:242)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:345)
    at org.glassfish.jersey.server.ChunkedOutput.flushQueue(ChunkedOutput.java:121)
    at org.glassfish.jersey.server.ChunkedOutput.write(ChunkedOutput.java:111)
    at jaxrs.prototype.tomcat7.AsyncPrototype$1.run(AsyncPrototype.java:82)
Caused by: java.lang.NullPointerException
    at org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:215)
    at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:480)
...

Any idea what went wrong? Originally I was deploying it on Tomcat 6 then I realized that the async capability is available in Servlet 3.0 API and Tomcat 6 doesn't support that therefore I switched to Tomcat 7.

1

1 Answers

1
votes

Adding async-supported in web.xml does the trick for me. I no longer see the exception after that.

Although I am still having issue on my client side where it is not receiving chunked output each time when a write occur. Instead it is receiving all chunked output all at once at the end.

  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>jaxrs.prototype;org.codehaus.jackson.jaxrs</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>