diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientWithRequestFuture.java b/httpclient/src/examples/org/apache/http/examples/client/ClientWithRequestFuture.java index 3a0e79494..5abbeb307 100644 --- a/httpclient/src/examples/org/apache/http/examples/client/ClientWithRequestFuture.java +++ b/httpclient/src/examples/org/apache/http/examples/client/ClientWithRequestFuture.java @@ -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 handler = new ResponseHandler() { - 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 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 futureTask2 = requestExecService.execute(request2, + // Because things are asynchronous, you must provide a ResponseHandler + ResponseHandler handler = new ResponseHandler() { + 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 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 futureTask3 = requestExecService.execute(request3, - HttpClientContext.create(), handler); - Boolean wasItOk3 = futureTask3.get(10, TimeUnit.SECONDS); - System.out.println("It was ok? " + wasItOk3); - - FutureCallback callback = new FutureCallback() { - public void completed(Boolean result) { - System.out.println("completed with " + result); + // Cancel a request + try { + HttpGet request2 = new HttpGet("http://google.com"); + HttpRequestFutureTask 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 futureTask3 = requestExecService.execute(request3, + HttpClientContext.create(), handler); + Boolean wasItOk3 = futureTask3.get(10, TimeUnit.SECONDS); + System.out.println("It was ok? " + wasItOk3); + + FutureCallback callback = new FutureCallback() { + 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 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> 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 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 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> 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 future : futureTask) { - System.out.println("another result " + future.get()); + } finally { + requestExecService.close(); } } - } \ No newline at end of file