Tweaked example app

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1483382 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2013-05-16 14:15:11 +00:00
parent 077dbc687a
commit 4add562f6c
1 changed files with 69 additions and 66 deletions

View File

@ -53,78 +53,81 @@ public class ClientWithRequestFuture {
.setMaxConnPerRoute(5)
.setMaxConnTotal(5).build();
ExecutorService execService = Executors.newFixedThreadPool(5);
FutureRequestExecutionService requestExecService = new FutureRequestExecutionService(httpclient, execService);
// Because things are asynchronous, you must provide a ResponseHandler
ResponseHandler<Boolean> handler = new ResponseHandler<Boolean>() {
public Boolean handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
// simply return true if the status was OK
return response.getStatusLine().getStatusCode() == 200;
}
};
// Simple request ...
HttpGet request1 = new HttpGet("http://google.com");
HttpRequestFutureTask<Boolean> futureTask1 = requestExecService.execute(request1,
HttpClientContext.create(), handler);
Boolean wasItOk1 = futureTask1.get();
System.out.println("It was ok? " + wasItOk1);
// Cancel a request
FutureRequestExecutionService requestExecService = new FutureRequestExecutionService(
httpclient, execService);
try {
HttpGet request2 = new HttpGet("http://google.com");
HttpRequestFutureTask<Boolean> futureTask2 = requestExecService.execute(request2,
// Because things are asynchronous, you must provide a ResponseHandler
ResponseHandler<Boolean> handler = new ResponseHandler<Boolean>() {
public Boolean handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
// simply return true if the status was OK
return response.getStatusLine().getStatusCode() == 200;
}
};
// Simple request ...
HttpGet request1 = new HttpGet("http://google.com");
HttpRequestFutureTask<Boolean> futureTask1 = requestExecService.execute(request1,
HttpClientContext.create(), handler);
futureTask2.cancel(true);
Boolean wasItOk2 = futureTask2.get();
System.out.println("It was cancelled so it should never print this: " + wasItOk2);
} catch (CancellationException e) {
System.out.println("We cancelled it, so this is expected");
}
Boolean wasItOk1 = futureTask1.get();
System.out.println("It was ok? " + wasItOk1);
// Request with a timeout
HttpGet request3 = new HttpGet("http://google.com");
HttpRequestFutureTask<Boolean> futureTask3 = requestExecService.execute(request3,
HttpClientContext.create(), handler);
Boolean wasItOk3 = futureTask3.get(10, TimeUnit.SECONDS);
System.out.println("It was ok? " + wasItOk3);
FutureCallback<Boolean> callback = new FutureCallback<Boolean>() {
public void completed(Boolean result) {
System.out.println("completed with " + result);
// Cancel a request
try {
HttpGet request2 = new HttpGet("http://google.com");
HttpRequestFutureTask<Boolean> futureTask2 = requestExecService.execute(request2,
HttpClientContext.create(), handler);
futureTask2.cancel(true);
Boolean wasItOk2 = futureTask2.get();
System.out.println("It was cancelled so it should never print this: " + wasItOk2);
} catch (CancellationException e) {
System.out.println("We cancelled it, so this is expected");
}
public void failed(Exception ex) {
System.out.println("failed with " + ex.getMessage());
// Request with a timeout
HttpGet request3 = new HttpGet("http://google.com");
HttpRequestFutureTask<Boolean> futureTask3 = requestExecService.execute(request3,
HttpClientContext.create(), handler);
Boolean wasItOk3 = futureTask3.get(10, TimeUnit.SECONDS);
System.out.println("It was ok? " + wasItOk3);
FutureCallback<Boolean> callback = new FutureCallback<Boolean>() {
public void completed(Boolean result) {
System.out.println("completed with " + result);
}
public void failed(Exception ex) {
System.out.println("failed with " + ex.getMessage());
}
public void cancelled() {
System.out.println("cancelled");
}
};
// Simple request with a callback
HttpGet request4 = new HttpGet("http://google.com");
// using a null HttpContext here since it is optional
// the callback will be called when the task completes, fails, or is cancelled
HttpRequestFutureTask<Boolean> futureTask4 = requestExecService.execute(request4,
HttpClientContext.create(), handler, callback);
Boolean wasItOk4 = futureTask4.get(10, TimeUnit.SECONDS);
System.out.println("It was ok? " + wasItOk4);
// Multiple requests, with a callback
HttpGet request5 = new HttpGet("http://google.com");
HttpGet request6 = new HttpGet("http://bing.com");
HttpGet request7 = new HttpGet("http://yahoo.com");
// using a null HttpContext here since it is optional
// the callback will be called for each request as their responses come back.
List<Future<Boolean>> futureTask = requestExecService.executeMultiple(
HttpClientContext.create(), handler, callback,
20,TimeUnit.SECONDS, request5, request6, request7);
// you can still access the futures directly, if you want. The futures are in the same order as the requests.
for (Future<Boolean> future : futureTask) {
System.out.println("another result " + future.get());
}
public void cancelled() {
System.out.println("cancelled");
}
};
// Simple request with a callback
HttpGet request4 = new HttpGet("http://google.com");
// using a null HttpContext here since it is optional
// the callback will be called when the task completes, fails, or is cancelled
HttpRequestFutureTask<Boolean> futureTask4 = requestExecService.execute(request4,
HttpClientContext.create(), handler, callback);
Boolean wasItOk4 = futureTask4.get(10, TimeUnit.SECONDS);
System.out.println("It was ok? " + wasItOk4);
// Multiple requests, with a callback
HttpGet request5 = new HttpGet("http://google.com");
HttpGet request6 = new HttpGet("http://bing.com");
HttpGet request7 = new HttpGet("http://yahoo.com");
// using a null HttpContext here since it is optional
// the callback will be called for each request as their responses come back.
List<Future<Boolean>> futureTask = requestExecService.executeMultiple(
HttpClientContext.create(), handler, callback,
20,TimeUnit.SECONDS, request5, request6, request7);
// you can still access the futures directly, if you want. The futures are in the same order as the requests.
for (Future<Boolean> future : futureTask) {
System.out.println("another result " + future.get());
} finally {
requestExecService.close();
}
}
}