-
Notifications
You must be signed in to change notification settings - Fork 0
Overview
Async Http client library purpose is to allow Java applications to easily execute HTTP requests and asynchronously process the HTTP responses. The Async HTTP Client library is simple to use. First, in order to add it to your Maven project, simply add this dependency: Sonatype Sonatype Release http://oss.sonatype.org/content/repositories/release and then define the dependency as:
The in your code, you can simply do: import com.ning.http.client.*; import java.util.concurrent.Future; AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); Future f = asyncHttpClient.prepareGet("http://www.ning.com/").execute(); Response r = f.get();
You can also accomplish asynchronous operation without using a Future if you want to receive and process the response in your handler:
import com.ning.http.client.*; import java.util.concurrent.Future;
AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); asyncHttpClient.prepareGet("http://www.ning.com").execute(new AsyncCompleteHandler() {
@Override
public Response onCompleted(Response response) throws Exception {
//Do something with the Response
// ...
return response;
}
@Override
public void onThrowable(Throwable t) {
//something wrong happended
}
})
-
You can also mix Future with AsynHandler to only retrieve part of the asynchronous response: import com.ning.http.client.* import java.util.concurrent.Future; AsyncHttpClient asynHttpClient = new AsyncHttpClient(); Future f = asyncHttpClient.prepareGet("http://www.ning.com/").execute(new AsyncCompletionHandler() { @Override public Integer onCompleted(Response response) throws Exception { //Do something with the Response return response.getStatusCode(); } @Override public void onThrowable(Throwable t) { //Something wrong happended. } });
int statusCode = f.get();
-
You have full control on the Response life cycle, so you can decide at any moment to stop processing what the server is sending back:
import com.ning.http.client.*; import java.util.concurrent.Future;
AsyncHttpClient c = new AsyncHttpClient(); Future f = c.prepareGet("http://www.ning.com/").execure(new AsyncHandler() { @Override public STATE onStatusRetrieved(HttpResponseStatus status) throws Exception { int statusCode = status.getStatusCode(); //The status have been read // If you don;t want to read the headers, body or stop processing the response return STATE.ABORT; } @Override public STATE onBodyPartRetrieve(HttpResponseBodyPart bodyPart) throws Exception { builder.append(new String(bodyPart.getBodyPartBytes())); return STATE.CONTINUE; }
@Override public String onCompleted() throws Exception { //Will be invoked once the response has been fully read or ResponseComplete exception // has been thrown return builder.toString(); }
@Override public void onThrowable(Throwable t) { } });
String bodyResponse = f.get();
Finally, you can also configure the AsyncHttpClient via it's AsyncHttpClientConfig object:
AsyncHttpClientConfig cf = new AsyncHttpClientConfig.Builder().setProxyServer(new ProxyServer("127.0.0.1",38080)).build();
AsyncHttpClient c = new AsyncHttpClient(cf);
The library uses Java non blocking I/O for supporting asynchronous operations. The default asynchronous provider is on top of Netty (http://jboss.org/netty), the Java NIO client Server Socket Framework from Jboss, but the library exposes a configurable provider SPI which allows to easily plug in order frameworks.