Asynchronous servlet with embedded drive
I want to create a real simple example for invoking an asynchronous servlet on an embedded Jetty server. When I try to run a Runnable from an AsyncContext 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 the 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)
The server may need additional configuration. Any suggestions?
Addition: the same servlet works without any changes in the embedded tomcat.
source to share
Don't ServletHandler
directly use that inner class that ServletContextHandler
creates / uses / manages.
Here's a modification to your example that sets the servlet context correctly.
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 will 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
source to share