Non-blocking HTTP requests on HttpHandlers

349 views Asked by At

As I understand if I have any blocking action on Undertow HTTP handlers I have to follow this pattern:

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

But I'm using awesome org.asynchttpclient library for having async HTTP calls like this:

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

The problem is exchange never sends 'OK' out ('OK' will not print out to the response), it seems it's already closed when I'm trying to write it.

Any way do handle this situation. Overall, does it have any advantage over solution one?

0

There are 0 answers