java – 嵌入式jetty中的异步servlet似乎在高负载时写入错误的响应

当我发送手动请求时,下面给出的代码工作正常.如果我用siege生成请求(siege -c 500 -r 100’http:// localhost:8080 /?name = Chandru& age = 560′),我开始在jetty的日志中看到这些消息:

2013-01-02 00:06:55.761:WARN:oejh.HttpGenerator:Ignoring extra content {name: "
2013-01-02 00:07:56.393:WARN:oejh.HttpGenerator:Ignoring extra content Chandru
2013-01-02 00:07:56.393:WARN:oejh.HttpGenerator:Ignoring extra content ", age: 
2013-01-02 00:07:56.393:WARN:oejh.HttpGenerator:Ignoring extra content 560
2013-01-02 00:07:56.393:WARN:oejh.HttpGenerator:Ignoring extra content }

我在Java 1.7.0_10上使用Jetty 8.1.8.v20121106.我做错了什么?

public class Main {

    public static void main(String[] args) throws Exception {
        final Server server = new Server(8080);
        final ServletContextHandler context = new ServletContextHandler(server, "/");
        final ServletHolder servlet = new ServletHolder(new RequestHandler(new Responder()));
        servlet.setAsyncSupported(true);
        context.addServlet(servlet, "/");
        server.start();
    }
}

class RequestHandler extends HttpServlet {

    private final Responder responder;

    public RosterHandler(Responder responder) {
        this.responder = responder;
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        responder.process(request.startAsync());
    }
}

class Responder {

    private final ExecutorService pool;

    public Responder() {
        this.pool = Executors.newFixedThreadPool(100);
    }

    public void process(final AsyncContext ctx) {
        pool.execute(new Runnable() {
            @Override
            public void run() {

                final String name = ctx.getRequest().getParameter("name");
                final int age = Integer.parseInt(ctx.getRequest().getParameter("age"));

                ctx.getResponse().setContentType("application/json");
                try (final PrintWriter w = ctx.getResponse().getWriter()) {
                    w.printf("{name: \"%s\", age: %d}", name, age);
                } catch (IOException e) {
                    e.printStackTrace(System.err);
                }
            }
        });
    }
}

解决方法:

您应该调用context.complete()来表示您的工作已完成,并且可以认为异步连接已完成.

上一篇:Jetty 9:在类路径上检测不到Spring WebApplicationInitializer类型


下一篇:Jetty