'httpasyncclient'에 해당되는 글 1건

  1. 2016.09.29 [HttpAsyncClient] example : AsyncClientHttpExchangeFutureCallback

[HttpAsyncClient] example : AsyncClientHttpExchangeFutureCallback

ITWeb/개발일반 2016.09.29 16:35

Concurrent Request 를 보낼 일이 있어서 샘플 코드 투척해 봅니다.

제가 작성한건 아니고 그냥 httpcomponents-asyncclient 에 있는 example 입니다.


Main Method)

public static void main(final String[] args) throws Exception {
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(3000)
.setConnectTimeout(3000).build();
CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
.setDefaultRequestConfig(requestConfig)
.build();
try {
httpclient.start();
final HttpGet[] requests = new HttpGet[] {
new HttpGet("http://www.apache.org/"),
new HttpGet("https://www.verisign.com/"),
new HttpGet("http://www.google.com/")
};
final CountDownLatch latch = new CountDownLatch(requests.length);
for (final HttpGet request: requests) {
httpclient.execute(request, new FutureCallback<HttpResponse>() {

@Override
public void completed(final HttpResponse response) {
latch.countDown();
System.out.println(request.getRequestLine() + "->"
+ response.getStatusLine());
}

@Override
public void failed(final Exception ex) {
latch.countDown();
System.out.println(request.getRequestLine() + "->" + ex);
}

@Override
public void cancelled() {
latch.countDown();
System.out.println(request.getRequestLine() + " cancelled");
}

});
}
latch.await();
System.out.println("Shutting down");
} finally {
httpclient.close();
}
System.out.println("Done");
}


저작자 표시 비영리 변경 금지
신고
크리에이티브 커먼즈 라이선스
Creative Commons License
Trackback 0 : Comment 0

티스토리 툴바