【问题标题】:How can I get HttpAsyncClient to act asynchronously?如何让 HttpAsyncClient 异步执行?
【发布时间】:2014-03-28 11:50:44
【问题描述】:

我正在尝试使用 HttpAsyncClient 对我的 Web 应用程序进行压力测试,方法是每秒提交一百或一千个 HTTP 请求并为每个请求计时。我的代码基于this quickstart guide 但似乎连接到服务器并发送 HTTP 请求的线程然后坐在那里等待服务器的响应!相反,我希望它继续发送下一个 HTTP 请求而不等待 HTTP 响应

这是我的测试代码:

public class TestAsyncHttpRequest {

    private static final SimpleDateFormat FORMAT = new SimpleDateFormat(
            "yyyy-MM-dd hh:mm:ss.SSS");

    public static void main(String[] args) throws Exception {

        CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
        try {
            // Start the client
            httpclient.start();
            final CountDownLatch latch = new CountDownLatch(100);
            for (int i = 0; i < 100; i++) {
                System.out.println("Sending POST request at "
                        + FORMAT.format(new Date()));
                final HttpPost request = new HttpPost(
                        "http://localhost:8080/test");
                httpclient.execute(request, new FutureCallback<HttpResponse>() {

                    public void completed(final HttpResponse response) {
                        latch.countDown();
                    }

                    public void failed(final Exception ex) {
                        latch.countDown();
                    }

                    public void cancelled() {
                        latch.countDown();
                    }

                });
                Thread.sleep(50);
            }
            latch.await();
        } finally {
            httpclient.close();
        }
    }
}

对应的测试servlet只是打印请求的时间,然后休眠20秒:

public class TestServlet extends HttpServlet {
    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        getServletContext().log(
                "TestServlet Received POST at: " + fmt.format(new Date()));
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

}

servlet 输出如下 - 注意它每 20 秒只接收两个请求:

INFO: TestServlet Received POST at: 2014-03-28 07:01:16.838
INFO: TestServlet Received POST at: 2014-03-28 07:01:16.838
INFO: TestServlet Received POST at: 2014-03-28 07:01:36.873
INFO: TestServlet Received POST at: 2014-03-28 07:01:36.873
INFO: TestServlet Received POST at: 2014-03-28 07:01:56.881
INFO: TestServlet Received POST at: 2014-03-28 07:01:56.881
INFO: TestServlet Received POST at: 2014-03-28 07:02:16.891
INFO: TestServlet Received POST at: 2014-03-28 07:02:16.891

是否有一些配置选项可以让我在不产生大量线程的情况下向服务器发送一千个 HTTP 请求?

【问题讨论】:

    标签: java multithreading apache-httpclient-4.x


    【解决方案1】:

    HttpAsyncClient 对并发连接总数和每台主机的并发连接数有限制。

    尝试增加它们:

    CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
        .setMaxConnTotal(100)
        .setMaxConnPerRoute(100)
        .build();
    

    【讨论】:

    • 哦,是的!看来,对我来说,限制因素是每条路由的 mac 连接数,默认为 2?!?!
    猜你喜欢
    • 2014-10-08
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    相关资源
    最近更新 更多