[prev in list] [next in list] [prev in thread] [next in thread] 

List:       httpcomponents-commits
Subject:    svn commit: r558825 - in
From:       rolandw () apache ! org
Date:       2007-07-23 18:30:00
Message-ID: 20070723183000.C14E91A981A () eris ! apache ! org
[Download RAW message or body]

Author: rolandw
Date: Mon Jul 23 11:29:59 2007
New Revision: 558825

URL: http://svn.apache.org/viewvc?view=rev&rev=558825
Log:
HTTPCLIENT-636: factored out the reference queue worker

Added:
    jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RefQueueHandler.java \
(with props)  jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RefQueueWorker.java \
(with props) Modified:
    jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.java


Added: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RefQueueHandler.java
                
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-clien \
t/src/main/java/org/apache/http/impl/conn/tsccm/RefQueueHandler.java?view=auto&rev=558825
 ==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RefQueueHandler.java \
                (added)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RefQueueHandler.java \
Mon Jul 23 11:29:59 2007 @@ -0,0 +1,48 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.impl.conn.tsccm;
+
+import java.lang.ref.Reference;
+
+
+/**
+ * Callback handler for {@link RefQueueWorker RefQueueWorker}.
+ */
+public interface RefQueueHandler {
+
+    /**
+     * Invoked when a reference is found on the queue.
+     *
+     * @param ref       the reference to handle
+     */
+    public void handleReference(Reference ref)
+        ;
+}

Propchange: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RefQueueHandler.java
                
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RefQueueHandler.java
                
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RefQueueHandler.java
                
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RefQueueWorker.java
                
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-clien \
t/src/main/java/org/apache/http/impl/conn/tsccm/RefQueueWorker.java?view=auto&rev=558825
 ==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RefQueueWorker.java \
                (added)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RefQueueWorker.java \
Mon Jul 23 11:29:59 2007 @@ -0,0 +1,139 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.impl.conn.tsccm;
+
+import java.lang.ref.Reference;
+import java.lang.ref.ReferenceQueue;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+
+
+/**
+ * A worker thread for processing queued references.
+ * {@link References Reference}s can be
+ * {@link ReferenceQueue queued}
+ * automatically by the garbage collector.
+ * If that feature is used, a daemon thread should be executing
+ * this worker. It will pick up the queued references and pass them
+ * on to a handler for appropriate processing.
+ */
+public class RefQueueWorker implements Runnable {
+
+    protected final Log LOG = LogFactory.getLog(RefQueueWorker.class);
+
+
+    /** The reference queue to monitor. */
+    protected final ReferenceQueue refQueue;
+
+    /** The handler for the references found. */
+    protected final RefQueueHandler refHandler;
+
+
+    /**
+     * The thread executing this handler.
+     * This attribute is also used as a shutdown indicator.
+     */
+    protected volatile Thread workerThread;
+
+
+    /**
+     * Instantiates a new worker to listen for lost connections.
+     *
+     * @param queue     the queue on which to wait for references
+     * @param handler   the handler to pass the references to
+     */
+    public RefQueueWorker(ReferenceQueue queue, RefQueueHandler handler) {
+        if (queue == null) {
+            throw new IllegalArgumentException("Queue must not be null.");
+        }
+        if (handler == null) {
+            throw new IllegalArgumentException("Handler must not be null.");
+        }
+
+        refQueue   = queue;
+        refHandler = handler;
+    }
+
+
+    /**
+     * The main loop of this worker.
+     * If initialization succeeds, this method will only return
+     * after {@link #shutdown shutdown()}. Only one thread can
+     * execute the main loop at any time.
+     */
+    public void run() {
+
+        if (this.workerThread == null) {
+            this.workerThread = Thread.currentThread();
+        }
+
+        while (this.workerThread == Thread.currentThread()) {
+            try {
+                // remove the next reference and process it
+                Reference ref = refQueue.remove();
+                refHandler.handleReference(ref);
+            } catch (InterruptedException e) {
+                //@@@ is logging really necessary? this here is the
+                //@@@ only reason for having a LOG in this class
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug(this.toString() + " interrupted", e);
+                }
+            }
+        }
+    }
+
+
+    /**
+     * Shuts down this worker.
+     * It can be re-started afterwards by another call to {@link #run run()}.
+     */
+    public void shutdown() {
+        Thread wt = this.workerThread;
+        if (wt != null) {
+            this.workerThread = null; // indicate shutdown
+            wt.interrupt();
+        }
+    }
+
+
+    /**
+     * Obtains a description of this worker.
+     *
+     * @return  a descriptive string for this worker
+     */
+    public String toString() {
+        return "RefQueueWorker::" + this.workerThread;
+    }
+
+} // class RefQueueWorker
+

Propchange: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RefQueueWorker.java
                
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RefQueueWorker.java
                
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RefQueueWorker.java
                
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.java
                
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-clien \
t/src/main/java/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.java?view=diff&rev=558825&r1=558824&r2=558825
 ==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.java \
                (original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.java \
Mon Jul 23 11:29:59 2007 @@ -507,7 +507,7 @@
      * as well as per-route lists.
      */
     //@@@ temporary package visibility, for BadStaticMaps
-    class /*default*/ ConnectionPool {
+    class /*default*/ ConnectionPool implements RefQueueHandler {
         
         /** The list of free connections */
         private LinkedList freeConnections = new LinkedList();
@@ -528,7 +528,7 @@
         private ReferenceQueue refQueue = BadStaticMaps.REFERENCE_QUEUE; //@@@
 
         /** A worker (thread) to track loss of pool entries to GC. */
-        private LostConnWorker refWorker;
+        private RefQueueWorker refWorker;
 
 
         /**
@@ -554,10 +554,10 @@
             boolean conngc = false; //@@@ check parameters to decide
             if (conngc) {
                 refQueue = new ReferenceQueue();
-                refWorker = new LostConnWorker(this);
+                refWorker = new RefQueueWorker(refQueue, this);
                 Thread t = new Thread(refWorker); //@@@ use a thread factory
                 t.setDaemon(true);
-                t.setName("LostConnWorker/"+ThreadSafeClientConnManager.this);
+                t.setName("RefQueueWorker@"+ThreadSafeClientConnManager.this);
                 t.start();
             }
         }
@@ -638,27 +638,26 @@
         }
 
         
-        /**
-         * Handles cleaning up for the given connection reference.
-         * Invoked by the worker listening for lost connections.
-         * 
-         * @param ref the reference to clean up
-         */
-        protected synchronized void handleReference(PoolEntryRef ref) {
+        // non-javadoc, see interface RefQueueHandler
+        public synchronized void handleReference(Reference ref) {
 
-            // check if the GCed pool entry was still in use
-            //@@@ find a way to detect this without lookup
-            //@@@ flag in the PoolEntryRef, to be reset when freed?
-            final boolean lost = issuedConnections.remove(ref);
-            if (lost) {
-                if (LOG.isDebugEnabled()) {
-                    LOG.debug(
-                        "Connection reclaimed by garbage collector. "
-                        + ref.route);
-                }
+            if (ref instanceof PoolEntryRef) {
+                // check if the GCed pool entry was still in use
+                //@@@ find a way to detect this without lookup
+                //@@@ flag in the PoolEntryRef, to be reset when freed?
+                final boolean lost = issuedConnections.remove(ref);
+                if (lost) {
+                    final HttpRoute route = ((PoolEntryRef)ref).route;
+
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug(
+                            "Connection garbage collected. " + route);
+                    }
 
-                handleLostConnection(ref.route);
+                    handleLostConnection(route);
+                }
             }
+            //@@@ check if the connection manager was GCed
         }
 
 
@@ -969,69 +968,6 @@
          */
         public boolean interruptedByConnectionPool = false;
     }
-
-
-    /**
-     * Tracker for GCed connections.
-     * Can be started in a background thread.
-     * The worker will listen on a {@link ReferenceQueue ReferenceQueue}
-     * for {@link PoolEntryRef PoolEntryRef} objects being queued.
-     */
-    private static class LostConnWorker implements Runnable {
-
-        private volatile Thread workerThread;
-
-        private final ConnectionPool connPool;
-
-
-        /**
-         * Instantiates a new worker to listen for lost connections.
-         *
-         * @param pool  the connection pool for which to work,
-         *              and in which to reclaim the lost connections
-         */
-        public LostConnWorker(ConnectionPool pool) {
-            this.connPool = pool;
-        }
-
-
-        /**
-         * Shuts down this worker.
-         */
-        public void shutdown() {
-            Thread wt = this.workerThread;
-            if (wt != null) {
-                this.workerThread = null; // indicate shutdown
-                wt.interrupt();
-            }
-        }
-
-
-        /**
-         * The main loop of this worker.
-         * If initialization succeeds, this method will only return
-         * after {@link #shutdown shutdown()}. Only one thread can
-         * execute the main loop at any time.
-         */
-        public void run() {
-            if (this.workerThread == null) {
-                this.workerThread = Thread.currentThread();
-            }
-
-            while (this.workerThread == Thread.currentThread()) {
-                try {
-                    // remove the next reference and process it
-                    Reference ref = connPool.refQueue.remove();
-                    if (ref instanceof PoolEntryRef) {
-                        connPool.handleReference((PoolEntryRef) ref);
-                    }
-                } catch (InterruptedException e) {
-                    LOG.debug("LostConnWorker interrupted", e);
-                }
-            }
-        }
-
-    } // class ReferenceQueueThread
 
 
     /**


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic