HttpHandlers上的非阻塞HTTP请求

时间:2017-01-08 11:23:26

标签: java undertow

据我所知,如果我对Undertow HTTP处理程序有任何阻止操作,我必须遵循这种模式:

public void handleRequest(final HttpServerExchange exchange) throws Exception {
    if (exchange.isInIoThread()) {
    exchange.dispatch(this);
       return;
    }
    // Do any blocking action like normal http calls...
}

但是我使用了很棒的org.asynchttpclient库来进行异步HTTP调用:

public void handleRequest(HttpServerExchange exchange) throws Exception {
    new DefaultAsyncHttpClient().prepareGet("http://some/url").execute()
            .toCompletableFuture()
            .thenApply(r -> {
                exchange.getResponseSender().send("OK");
                return r;
            });
}

问题是交换永远不会发送' OK' out(' OK'不会打印到回复中),当我试图写它时,它似乎已经关闭。

任何方式都可以处理这种情况。总的来说,它比解决方案1有什么优势吗?

0 个答案:

没有答案