Return-Path: X-Original-To: apmail-hc-commits-archive@www.apache.org Delivered-To: apmail-hc-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B8F8F9CB4 for ; Thu, 16 May 2013 14:15:36 +0000 (UTC) Received: (qmail 98052 invoked by uid 500); 16 May 2013 14:15:36 -0000 Delivered-To: apmail-hc-commits-archive@hc.apache.org Received: (qmail 97916 invoked by uid 500); 16 May 2013 14:15:33 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 97897 invoked by uid 99); 16 May 2013 14:15:33 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 16 May 2013 14:15:33 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 16 May 2013 14:15:31 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 67F7C2388962 for ; Thu, 16 May 2013 14:15:11 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1483382 - /httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientWithRequestFuture.java Date: Thu, 16 May 2013 14:15:11 -0000 To: commits@hc.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130516141511.67F7C2388962@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: olegk Date: Thu May 16 14:15:11 2013 New Revision: 1483382 URL: http://svn.apache.org/r1483382 Log: Tweaked example app Modified: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientWithRequestFuture.java Modified: httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientWithRequestFuture.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientWithRequestFuture.java?rev=1483382&r1=1483381&r2=1483382&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientWithRequestFuture.java (original) +++ httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientWithRequestFuture.java Thu May 16 14:15:11 2013 @@ -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); - public void cancelled() { - System.out.println("cancelled"); + 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()); } - }; - - // 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