Last active
December 16, 2015 22:19
-
-
Save conorgil/5505603 to your computer and use it in GitHub Desktop.
Attemp to understand if AsyncHttpClient can make non-blocking calls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.ning.http.client.AsyncCompletionHandler; | |
import com.ning.http.client.AsyncHttpClient; | |
import com.ning.http.client.AsyncHttpClientConfig; | |
import com.ning.http.client.AsyncHttpClientConfig.Builder; | |
import com.ning.http.client.ListenableFuture; | |
import com.ning.http.client.Response; | |
public class MyAsyncHttpClientTest { | |
public static void main(String[] args) throws Exception { | |
int numRequests = 10; | |
Builder configBuilder = new AsyncHttpClientConfig.Builder(); | |
AsyncHttpClient asyncHttpClient = new AsyncHttpClient( | |
configBuilder.build()); | |
ListenableFuture<Void> f = null; | |
try { | |
for (int i = 0; i < numRequests; i++) { | |
f = asyncHttpClient.prepareGet("http://www.google.com") | |
.execute(new CompletionHandler(i)); | |
System.out.println(String.format("Request %d sent! ", i)); | |
// f.get(); | |
System.out.flush(); | |
} | |
} finally { | |
if (f != null) | |
System.out.println(f.get()); | |
System.out.println("done"); | |
asyncHttpClient.close(); | |
} | |
} | |
private 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())); | |
System.out.flush(); | |
return null; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>test</groupId> | |
<artifactId>async-http-client-test</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>com.ning</groupId> | |
<artifactId>async-http-client</artifactId> | |
<version>1.7.14</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.1</version> | |
<configuration> | |
<target>7</target> | |
<source>7</source> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
With line 23 uncommented, the code does in fact become "asynchronous" in that the HTTP calls are made on a thread other than the main thread, but the call to get() is blocking so aren't we just offloading work onto another thread without any benefit in throughput?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would expect this gist to print out "Request X sent!" AND "Response X: something" for each request. However, it appears that the HTTP call is not made (and thus, the handler not executed) until each Future calls get. Uncommenting line 23 f.get() makes the code work as expected, but the call to Future#get() is blocking... Is there a way to just provide a callback function that gets executed once the HTTP response is fully retrieved?