Skip to content

Instantly share code, notes, and snippets.

@dawnbreaks
Created April 1, 2015 09:31
Show Gist options
  • Save dawnbreaks/5565247d6d4ea8f7452b to your computer and use it in GitHub Desktop.
Save dawnbreaks/5565247d6d4ea8f7452b to your computer and use it in GitHub Desktop.
Test for AsyncHttpClient
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import com.ning.http.client.AsyncCompletionHandler;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClientConfig;
import com.ning.http.client.Response;
import com.ning.http.client.providers.netty.NettyAsyncHttpProviderConfig;
public class TestAsyncHttpClient {
public static void main(String[] args) throws InterruptedException, ExecutionException {
AsyncHttpClientConfig clientConf = new AsyncHttpClientConfig.Builder()
.setAsyncHttpClientProviderConfig(new NettyAsyncHttpProviderConfig())
.setMaxConnections(1000)
.setMaxConnectionsPerHost(1000)
.setConnectTimeout(3000)
.setReadTimeout(3000)
.build();
AsyncHttpClient asyncHttpClient = new AsyncHttpClient(clientConf);
// AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
try{
Future<Response> future = asyncHttpClient.prepareGet("http://www.ning.com/").execute();
Response response = future.get();
System.out.println("statusCodde=" + response.getStatusCode()+"|contentType="+response.getContentType());
asyncHttpClient.prepareGet("http://www.ning.com/").execute(new AsyncCompletionHandler<Response>(){
@Override
public Response onCompleted(Response response) throws Exception{
System.out.println("onCompleted|statusCodde=" + response.getStatusCode()+"|contentType="+response.getContentType());
return response;
}
@Override
public void onThrowable(Throwable t){
System.out.println("onThrowable|msg=" + t.getMessage());
}
});
Thread.sleep(1000*3);
}finally{
asyncHttpClient.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment