Files
pinpoint/src/test/java/com/nhn/pinpoint/testweb/httpclient/AsyncHTTPClientTest.java
T

47 lines
1.4 KiB
Java

package com.nhn.pinpoint.testweb.httpclient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.junit.Test;
import com.ning.http.client.AsyncCompletionHandler;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClientConfig;
import com.ning.http.client.ListenableFuture;
import com.ning.http.client.Response;
import com.ning.http.client.providers.netty.NettyAsyncHttpProvider;
public class AsyncHTTPClientTest {
@Test
public void asyncHttpClient() {
AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder().build();
AsyncHttpClient client = new AsyncHttpClient(new NettyAsyncHttpProvider(config), config);
try {
ListenableFuture<Response> future = client.prepareGet("http://www.naver.com").execute(new AsyncCompletionHandler<Response>() {
@Override
public Response onCompleted(Response response) throws Exception {
// do something
return response;
}
});
Response response = future.get(300L, TimeUnit.MILLISECONDS);
System.out.println(response.getResponseBody());
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}