Home | History | Annotate | Download | only in tsccm
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RefQueueWorker.java $
      3  * $Revision: 673450 $
      4  * $Date: 2008-07-02 10:35:05 -0700 (Wed, 02 Jul 2008) $
      5  *
      6  * ====================================================================
      7  *
      8  *  Licensed to the Apache Software Foundation (ASF) under one or more
      9  *  contributor license agreements.  See the NOTICE file distributed with
     10  *  this work for additional information regarding copyright ownership.
     11  *  The ASF licenses this file to You under the Apache License, Version 2.0
     12  *  (the "License"); you may not use this file except in compliance with
     13  *  the License.  You may obtain a copy of the License at
     14  *
     15  *      http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  *  Unless required by applicable law or agreed to in writing, software
     18  *  distributed under the License is distributed on an "AS IS" BASIS,
     19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     20  *  See the License for the specific language governing permissions and
     21  *  limitations under the License.
     22  * ====================================================================
     23  *
     24  * This software consists of voluntary contributions made by many
     25  * individuals on behalf of the Apache Software Foundation.  For more
     26  * information on the Apache Software Foundation, please see
     27  * <http://www.apache.org/>.
     28  *
     29  */
     30 
     31 package org.apache.http.impl.conn.tsccm;
     32 
     33 import java.lang.ref.Reference;
     34 import java.lang.ref.ReferenceQueue;
     35 
     36 import org.apache.commons.logging.Log;
     37 import org.apache.commons.logging.LogFactory;
     38 
     39 
     40 
     41 /**
     42  * A worker thread for processing queued references.
     43  * {@link Reference Reference}s can be
     44  * {@link ReferenceQueue queued}
     45  * automatically by the garbage collector.
     46  * If that feature is used, a daemon thread should be executing
     47  * this worker. It will pick up the queued references and pass them
     48  * on to a handler for appropriate processing.
     49  *
     50  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     51  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     52  *     for further details.
     53  */
     54 @Deprecated
     55 public class RefQueueWorker implements Runnable {
     56 
     57     private final Log log = LogFactory.getLog(getClass());
     58 
     59     /** The reference queue to monitor. */
     60     protected final ReferenceQueue<?> refQueue;
     61 
     62     /** The handler for the references found. */
     63     protected final RefQueueHandler refHandler;
     64 
     65 
     66     /**
     67      * The thread executing this handler.
     68      * This attribute is also used as a shutdown indicator.
     69      */
     70     protected volatile Thread workerThread;
     71 
     72 
     73     /**
     74      * Instantiates a new worker to listen for lost connections.
     75      *
     76      * @param queue     the queue on which to wait for references
     77      * @param handler   the handler to pass the references to
     78      */
     79     public RefQueueWorker(ReferenceQueue<?> queue, RefQueueHandler handler) {
     80         if (queue == null) {
     81             throw new IllegalArgumentException("Queue must not be null.");
     82         }
     83         if (handler == null) {
     84             throw new IllegalArgumentException("Handler must not be null.");
     85         }
     86 
     87         refQueue   = queue;
     88         refHandler = handler;
     89     }
     90 
     91 
     92     /**
     93      * The main loop of this worker.
     94      * If initialization succeeds, this method will only return
     95      * after {@link #shutdown shutdown()}. Only one thread can
     96      * execute the main loop at any time.
     97      */
     98     public void run() {
     99 
    100         if (this.workerThread == null) {
    101             this.workerThread = Thread.currentThread();
    102         }
    103 
    104         while (this.workerThread == Thread.currentThread()) {
    105             try {
    106                 // remove the next reference and process it
    107                 Reference<?> ref = refQueue.remove();
    108                 refHandler.handleReference(ref);
    109             } catch (InterruptedException e) {
    110                 //@@@ is logging really necessary? this here is the
    111                 //@@@ only reason for having a log in this class
    112                 if (log.isDebugEnabled()) {
    113                     log.debug(this.toString() + " interrupted", e);
    114                 }
    115             }
    116         }
    117     }
    118 
    119 
    120     /**
    121      * Shuts down this worker.
    122      * It can be re-started afterwards by another call to {@link #run run()}.
    123      */
    124     public void shutdown() {
    125         Thread wt = this.workerThread;
    126         if (wt != null) {
    127             this.workerThread = null; // indicate shutdown
    128             wt.interrupt();
    129         }
    130     }
    131 
    132 
    133     /**
    134      * Obtains a description of this worker.
    135      *
    136      * @return  a descriptive string for this worker
    137      */
    138     @Override
    139     public String toString() {
    140         return "RefQueueWorker::" + this.workerThread;
    141     }
    142 
    143 } // class RefQueueWorker
    144 
    145