Using the AsyncHttpClient provider with Netty prevent the main program from terminating when executing an asynchronous request. For example, the following program exits after println or not, depending on whether the provider is JDKAsyncHttpProvider or NettyAsyncHttpProvider :
public class Program { public static CompletableFuture<Response> getDataAsync(String uri) { final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); final CompletableFuture<Response> promise = new CompletableFuture<>(); asyncHttpClient .prepareGet(uri) .execute(new AsyncCompletionHandler<Response>(){ @Override public Response onCompleted(Response resp) throws Exception { promise.complete(resp); asyncHttpClient.close();
About AsynHttpClient , the documentation states:
AHC is an abstraction layer that can work on top of the bare JDK, Netty, and Grizzly. Please note that the JDK implementation is very limited, and you must REALLY use other real providers.
To use AsyncHttpClient with Netty, we just need to include the appropriate library in the java class path. Thus, we can run the previous Program with one of the following class path configurations to use Netty or not:
-cp .;async-http-client-1.9.24.jar;netty-3.10.3.Final.jar;slf4j-api-1.7.12.jar will use NettyAsyncHttpProvider-cp .;async-http-client-1.9.24.jar;slf4j-api-1.7.12.jar will use JDKAsyncHttpProvider
What else needs to be done to use the Netty provider correctly? For example, I close AsyncHttpClient in AsyncCompletionHandler . It is right?
Is there any configuration to change the observed behavior?
source share