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.