Asynchronous Servlet with Embedded Jetty

Gregor picture Gregor · Dec 28, 2014 · Viewed 8.5k times · Source

I want to build a real simple example for invoking a asynchronous servlet in an embedded Jetty server. When I try to start a Runnable from the AsyncContext of the Request, I get a NullPointerException. This is the server code:

public static void main(String[] args) throws Exception {
    Server server = new Server(8080);
    ServletHandler handler = new ServletHandler();
    server.setHandler(handler);
    ServletHolder holder = handler.addServletWithMapping(AsyncServlet.class, "/*");
    holder.setAsyncSupported(true);
    server.start();
    server.join();
}

This is the servlet code:

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
    final AsyncContext ctxt = req.startAsync();
    ctxt.start(() -> {
        ctxt.complete();
    });
}

And this is the error with stack trace:

java.lang.NullPointerException
at org.eclipse.jetty.server.AsyncContextState$2.run(AsyncContextState.java:168)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:620)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:540)
at java.lang.Thread.run(Thread.java:745) 

Maybe the server needs any additional configuration. Any suggestions?

Addition: The same servlet runs without any change in an embedded tomcat.

Answer

Joakim Erdfelt picture Joakim Erdfelt · Dec 29, 2014

Don't use ServletHandler directly, that's an internal class that ServletContextHandler creates / uses / manages.

Here's a modification of your example, that sets up the servlet context properly.

package jetty;

import java.io.IOException;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class EmbeddedAsyncServer
{
    public static class EmbeddedAsyncServlet extends HttpServlet
    {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
        {
            final AsyncContext ctxt = req.startAsync();
            ctxt.start(new Runnable()
            {
                @Override
                public void run()
                {
                    System.err.println("In AsyncContext / Start / Runnable / run");
                    ctxt.complete();
                }
            });
        }
    }

    public static void main(String[] args) throws Exception
    {
        Server server = new Server(9090);
        ServletContextHandler context = new ServletContextHandler();
        context.setContextPath("/");
        ServletHolder asyncHolder = context.addServlet(EmbeddedAsyncServlet.class,"/async");
        asyncHolder.setAsyncSupported(true);
        server.setHandler(context);
        server.start();
        server.join();
    }
}

Access http://localhost:9090/async and you'll see the following output

2014-12-29 13:18:57.075:INFO::main: Logging initialized @69ms
2014-12-29 13:18:57.131:INFO:oejs.Server:main: jetty-9.2.6.v20141205
2014-12-29 13:18:57.156:INFO:oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@65123c41{/,null,AVAILABLE}
2014-12-29 13:18:57.169:INFO:oejs.ServerConnector:main: Started ServerConnector@722b302{HTTP/1.1}{0.0.0.0:9090}
2014-12-29 13:18:57.169:INFO:oejs.Server:main: Started @166ms
In AsyncContext / Start / Runnable / run