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