4

当我发送手动请求时,下面给出的代码可以正常工作。如果我使用 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);
                }
            }
        });
    }
}
4

1 回答 1

1

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

于 2013-01-03T21:47:06.283 回答