java - can AsyncHttpClient perform non-blocking, async HTTP calls? -


all,

i trying decide whether use nodejs or java application. communicating couchdb on http , asynchronous non-blocking design application thread can process additional requests while waits on query response couchdb.

i prefer use java , have been looking @ asynchttpclient few days potential solution. however, having trouble understanding library , think may have fundamental misunderstanding of something.

i posted gist here: https://gist.github.com/conorgil/5505603

i expect gist print out "request x sent!" , "response x: something" each request. however, appears http call not made (and thus, handler not executed) until each future calls get(). uncommenting line 23 f.get() makes code work expected, call future#get() blocking, correct? there way provide callback function gets executed once http response retrieved without blocking?

something following: 1) request comes in on main thread 2) async, non-blocking http call made couchdb. completion handler registered process response couchdb 3) main thread free process next request 4) http response couchdb arrives @ point , registered handler called perform business logic 5) main thread continues processing requests (for requests not need hit couchdb, can responded quickly)

am fundamentally misunderstanding here? possible type of thing in java? asynchttpclient answer? question related, not sure if things have changed since 2011 (perform async connect java asynchttpclient library?)

since nodejs runs event loop, non-blocking, async behavior standard. register callback function handle db response when received , event loop process other things in meantime.

any , advice appreciated.

thanks, conor

the main purpose of asynchttpclient non-blocking http , have used effect. example, ran simplified version of code:

public class myasynchttpclienttest {   public static void main(string[] args) throws exception {     asynchttpclient asynchttpclient = new asynchttpclient();     (int = 0; < 10; i++) {       asynchttpclient.prepareget("http://www.google.com")         .execute(new completionhandler(i));       system.out.println(string.format("request %d sent! ", i));       system.out.flush();     }   }   static class completionhandler extends asynccompletionhandler<void> {     private final int reqnumber;     public completionhandler(int reqnumber) { this.reqnumber = reqnumber; }     @override public void oncompleted(response response) throws exception {       system.out.println(string.format("response %d: %s", reqnumber,           response.getresponsebody()));       return null;     }   } } 

notice no futures involved. produces following output, 1 should expect:

request 0 sent!  request 1 sent!  request 2 sent!  request 3 sent!  request 4 sent!  request 5 sent!  request 6 sent!  request 7 sent!  request 8 sent!  request 9 sent!  response 1: <html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"> <title>302 moved</title></head><body> <h1>302 moved</h1> document has moved <a href="http://www.google.hr/">here</a>. </body></html>  response 0: <html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"> <title>302 moved</title></head><body> <h1>302 moved</h1> document has moved <a href="http://www.google.hr/">here</a>. </body></html>  ... 

the trouble is, process left hanging because there no code shuts down client, that's separate story.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -