HttpAsyncClient回调中的异常记录

时间:2018-08-10 11:36:38

标签: java apache-httpcomponents apache-httpasyncclient

如何记录Apache HttpAsyncClient回调方法中发生的异常?例如,以下代码抛出NullPointerException,但它是隐藏的。

CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
try {
    final HttpGet request = new HttpGet("https://google.com");
    CountDownLatch sentRequestsCountDown = new CountDownLatch(1);
    String nullObject = null;
    final FutureCallback<HttpResponse> callback = new FutureCallback<HttpResponse>() {

        @Override
        public void completed(HttpResponse result) {
            sentRequestsCountDown.countDown();
            // NPE thrown here:
            String itThrowsNPE = nullObject.toString();
            System.out.println(itThrowsNPE);
        }

        @Override
        public void failed(Exception ex) {
            sentRequestsCountDown.countDown();
            System.out.println("failed");
        }

        @Override
        public void cancelled() {
            sentRequestsCountDown.countDown();
            System.out.println("cancelled");
        }
    };

    client.start();
    client.execute(request, callback);
    sentRequestsCountDown.await();
} finally {
    client.close();
}

运行此命令时,没有看到任何异常记录。如何为此配置记录器?看来NO_OP ExceptionLogger是默认使用的,我找不到改变它的方法。

0 个答案:

没有答案