java - How to close AsyncHttpClient with Netty for an asynchronous Http request? -
using asynchttpclient
netty
provider prevent main program terminate when execute asynchronous request. instance, following program terminates after println
, or not, depending on whether provider 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(); // ??? correct ???? return resp; } }); return promise; } public static void main(string [] args) throws exception { final string uri = "…"; system.out.println(getdataasync(uri).get()); } }
about asynhttpclient
documentation states:
ahc abstraction layer can work on top of bare jdk, netty , grizzly. note jdk implementation limited , should use other real providers.
to use asynchttpclient netty need include corresponding library in java class path. so, may run previous program
1 of following class path configurations use netty, or not:
-cp .;async-http-client-1.9.24.jar;netty-3.10.3.final.jar;slf4j-api-1.7.12.jar
usenettyasynchttpprovider
-cp .;async-http-client-1.9.24.jar;slf4j-api-1.7.12.jar
usejdkasynchttpprovider
what else should use netty provider correctly? instance, closing asynchttpclient
in asynccompletionhandler
. correct?
is there configuration change observed behavior?
using netty provider , stepping through in debugger, see calling asynchttpclient.close() inside callback causes niosocketchannelfactory.releaseexternalresources() fail when tries shutdown. exception thrown, , causing non daemon thread remain, , keeping vm exiting. exception thrown logged @ warn, you're not seeing (if add slf4j-log4j12-1.7.7.jar classpath should see it). can't call close() in call (and don't need close after every request execution anyhow).
here's trace:
warn [new i/o worker #1] (nettyasynchttpprovider.java:79) - unexpected error on close java.lang.illegalstateexception: must not called i/o-thread prevent deadlocks! @ org.jboss.netty.channel.socket.nio.abstractnioselector.shutdown(abstractnioselector.java:415) @ org.jboss.netty.channel.socket.nio.nioworker.shutdown(nioworker.java:36) @ org.jboss.netty.channel.socket.nio.abstractnioworkerpool.shutdown(abstractnioworkerpool.java:142) @ org.jboss.netty.channel.socket.nio.nioclientsocketchannelfactory.releaseexternalresources(nioclientsocketchannelfactory.java:225) @ com.ning.http.client.providers.netty.channel.channelmanager.close(channelmanager.java:355) @ com.ning.http.client.providers.netty.nettyasynchttpprovider.close(nettyasynchttpprovider.java:70) @ com.ning.http.client.asynchttpclient.close(asynchttpclient.java:336) @ com.cie.program.program$1.oncompleted(program.java:23)
Comments
Post a Comment