hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r573210 - /jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/examples/nio/NHttpClient.java
Date Thu, 06 Sep 2007 10:12:58 GMT
Author: olegk
Date: Thu Sep  6 03:12:52 2007
New Revision: 573210

URL: http://svn.apache.org/viewvc?rev=573210&view=rev
Log:
Improved synchronization logic in the NHttpClient sample

Modified:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/examples/nio/NHttpClient.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/examples/nio/NHttpClient.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/examples/nio/NHttpClient.java?rev=573210&r1=573209&r2=573210&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/examples/nio/NHttpClient.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/examples/nio/NHttpClient.java
Thu Sep  6 03:12:52 2007
@@ -85,9 +85,13 @@
         httpproc.addInterceptor(new RequestUserAgent());
         httpproc.addInterceptor(new RequestExpectContinue());
         
+        // We are going to use this object to synchronize between the 
+        // I/O event and main threads
+        RequestCount requestCount = new RequestCount(3);
+        
         BufferingHttpClientHandler handler = new BufferingHttpClientHandler(
                 httpproc,
-                new MyHttpRequestExecutionHandler(),
+                new MyHttpRequestExecutionHandler(requestCount),
                 new DefaultConnectionReuseStrategy(),
                 params);
 
@@ -111,7 +115,7 @@
         });
         t.start();
 
-        SessionRequest[] reqs = new SessionRequest[3];
+        SessionRequest[] reqs = new SessionRequest[requestCount.getValue()];
         reqs[0] = ioReactor.connect(
                 new InetSocketAddress("www.yahoo.com", 80), 
                 null, 
@@ -127,11 +131,33 @@
                 null,
                 new HttpHost("www.apache.org"),
                 null);
+     
+        // Block until all connections signal
+        // completion of the request execution
+        synchronized (requestCount) {
+            while (requestCount.getValue() > 0) {
+                requestCount.wait();
+            }
+        }
+
+        System.out.println("Shutting down I/O reactor");
         
+        ioReactor.shutdown();
+        
+        System.out.println("Done");
     }
     
     static class MyHttpRequestExecutionHandler implements HttpRequestExecutionHandler {
 
+        private final static String DONE = "done";
+        
+        private final RequestCount requestCount;
+        
+        public MyHttpRequestExecutionHandler(final RequestCount requestCount) {
+            super();
+            this.requestCount = requestCount;
+        }
+        
         public void initalizeContext(final HttpContext context, final Object attachment)
{
             HttpHost targetHost = (HttpHost) attachment;
             context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, targetHost);
@@ -140,21 +166,18 @@
         public HttpRequest submitRequest(final HttpContext context) {
             HttpHost targetHost = (HttpHost) context.getAttribute(
                     ExecutionContext.HTTP_TARGET_HOST);
-            Integer countObj = (Integer) context.getAttribute(
-                    "request-count");
-            int counter = 0; 
-            if (countObj != null) {
-                counter = countObj.intValue(); 
-            }
-            counter++;
-            context.setAttribute("request-count", new Integer(counter));
-            if (counter < 3) {
+            Object token = context.getAttribute(DONE);
+            if (token == null) {
+                // Stick some object into the context
+                context.setAttribute(DONE, Boolean.TRUE);
+
                 System.out.println("--------------");
                 System.out.println("Sending request to " + targetHost);
                 System.out.println("--------------");
+                
                 return new BasicHttpRequest("GET", "/");
             } else {
-                // Return null to terminate the connection
+                // No new request to submit
                 return null;
             }
         }
@@ -172,17 +195,19 @@
             } catch (IOException ex) {
                 System.err.println("I/O error: " + ex.getMessage());
             }
+
+            // Signal completion of the request execution
+            synchronized (this.requestCount) {
+                this.requestCount.decrement();
+                this.requestCount.notifyAll();
+            }
         }
         
     }
     
     static class EventLogger implements EventListener {
 
-        private int openNo = 0;
-        private int closedNo = 0;
-        
         public void connectionOpen(final NHttpConnection conn) {
-            this.openNo++;
             System.out.println("Connection open: " + conn);
         }
 
@@ -192,10 +217,6 @@
 
         public void connectionClosed(final NHttpConnection conn) {
             System.out.println("Connection closed: " + conn);
-            this.closedNo++;
-            if (this.openNo == this.closedNo) {
-                System.exit(0);
-            }
         }
 
         public void fatalIOException(final IOException ex, final NHttpConnection conn) {
@@ -207,5 +228,23 @@
         }
         
     }
+
+    static class RequestCount {
+        
+        private int value;
+        
+        public RequestCount(int initialValue) {
+            this.value = initialValue;
+        }
         
+        public int getValue() {
+            return this.value;
+        }
+        
+        public void decrement() {
+            this.value--;
+        }
+        
+    }
+    
 }



Mime
View raw message