You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ha...@apache.org on 2003/11/09 13:18:19 UTC

cvs commit: jakarta-commons-sandbox/threadpool/src/java/org/apache/commons/threadpool CommonsLoggingThreadPoolMonitor.java NullThreadPoolMonitor.java ThreadPoolMonitor.java DefaultThreadPool.java

hammant     2003/11/09 04:18:19

  Modified:    threadpool/src/java/org/apache/commons/threadpool
                        DefaultThreadPool.java
  Added:       threadpool/src/java/org/apache/commons/threadpool
                        CommonsLoggingThreadPoolMonitor.java
                        NullThreadPoolMonitor.java ThreadPoolMonitor.java
  Log:
  Monitor added.
  
  Revision  Changes    Path
  1.3       +22 -18    jakarta-commons-sandbox/threadpool/src/java/org/apache/commons/threadpool/DefaultThreadPool.java
  
  Index: DefaultThreadPool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/threadpool/src/java/org/apache/commons/threadpool/DefaultThreadPool.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DefaultThreadPool.java	13 Oct 2003 08:32:59 -0000	1.2
  +++ DefaultThreadPool.java	9 Nov 2003 12:18:19 -0000	1.3
  @@ -61,10 +61,7 @@
    */
   package org.apache.commons.threadpool;
   
  -import org.apache.commons.logging.Log;
  -import org.apache.commons.logging.LogFactory;
  -
  -/** 
  +/**
    * A default implementation of a ThreadPool
    * which is constructed with a given number of threads.
    *
  @@ -74,26 +71,39 @@
   public class DefaultThreadPool implements Runnable, ThreadPool {
   
       /** The Log to which logging calls will be made. */
  -    private Log log = LogFactory.getLog(DefaultThreadPool.class);
   
       private MTQueue queue = new MTQueue();
       private boolean stopped = false;
  +    private final ThreadPoolMonitor monitor;
  +
  +    public DefaultThreadPool(ThreadPoolMonitor monitor,
  +                 int numberOfThreads, int threadPriority) {
  +        this.monitor = monitor;
  +        for ( int i = 0; i < numberOfThreads; i++ ) {
  +            startThread(threadPriority);
  +        }
  +    }
  +
  +    public DefaultThreadPool(ThreadPoolMonitor monitor,
  +                 int numberOfThreads) {
  +        this.monitor = monitor;
  +        for ( int i = 0; i < numberOfThreads; i++ ) {
  +            startThread();
  +        }
  +    }
   
       public DefaultThreadPool() {
  +        this.monitor = new CommonsLoggingThreadPoolMonitor();
           // typically a thread pool should have at least 1 thread
           startThread();
       }
   
       public DefaultThreadPool(int numberOfThreads) {
  -        for ( int i = 0; i < numberOfThreads; i++ ) {
  -            startThread();
  -        }
  +        this(new CommonsLoggingThreadPoolMonitor(), numberOfThreads);
       }
   
       public DefaultThreadPool(int numberOfThreads, int threadPriority) {
  -        for ( int i = 0; i < numberOfThreads; i++ ) {
  -            startThread(threadPriority);
  -        }
  +        this(new CommonsLoggingThreadPoolMonitor(), numberOfThreads, threadPriority);
       }
       
       /** Start a new thread running */
  @@ -146,15 +156,9 @@
                       task.run();
                   }
                   catch (Throwable t) {
  -                    handleException(t);
  +                    monitor.handleThrowable(this.getClass(), task, t);
                   }
               }
           }
  -    }
  -
  -    // Implementation methods
  -    //-------------------------------------------------------------------------
  -    protected void handleException(Throwable t) {
  -        log.error( "Caught: " + t, t );
       }
   }
  
  
  
  1.1                  jakarta-commons-sandbox/threadpool/src/java/org/apache/commons/threadpool/CommonsLoggingThreadPoolMonitor.java
  
  Index: CommonsLoggingThreadPoolMonitor.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/threadpool/src/java/org/apache/commons/threadpool/CommonsLoggingThreadPoolMonitor.java,v 1.1 2003/11/09 12:18:19 hammant Exp $
   * $Revision: 1.1 $
   * $Date: 2003/11/09 12:18:19 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * 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.commons.threadpool;
  
  import org.apache.commons.logging.LogFactory;
  
  public class CommonsLoggingThreadPoolMonitor implements ThreadPoolMonitor {
  
      public void handleThrowable(Class clazz, Runnable runnable,
                                  Throwable throwable) {
          LogFactory.getLog(clazz).error( "Caught: " + throwable + " in runnable " + runnable, throwable );
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/threadpool/src/java/org/apache/commons/threadpool/NullThreadPoolMonitor.java
  
  Index: NullThreadPoolMonitor.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/threadpool/src/java/org/apache/commons/threadpool/NullThreadPoolMonitor.java,v 1.1 2003/11/09 12:18:19 hammant Exp $
   * $Revision: 1.1 $
   * $Date: 2003/11/09 12:18:19 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * 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.commons.threadpool;
  /**
   * NullObject pattern impl of ThreadPoolMonitor
   */
  public class NullThreadPoolMonitor implements ThreadPoolMonitor {
      public void handleThrowable(Class clazz, Runnable runnable, Throwable t) {
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/threadpool/src/java/org/apache/commons/threadpool/ThreadPoolMonitor.java
  
  Index: ThreadPoolMonitor.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/threadpool/src/java/org/apache/commons/threadpool/ThreadPoolMonitor.java,v 1.1 2003/11/09 12:18:19 hammant Exp $
   * $Revision: 1.1 $
   * $Date: 2003/11/09 12:18:19 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * 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.commons.threadpool;
  /**
   * The idea is that the end-user can write a implementation of
   * this class that allows them have a deterministic for the
   * events that are monitored for. I.e. they can change the state
   * of other parts of their system (or not).
   *
   * One implementation of this is logging....
   * @see CommonsLoggingThreadPoolMonitor
   * @see NullThreadPoolMonitor
   */
  public interface ThreadPoolMonitor {
      void handleThrowable(Class clazz, Runnable runnable, Throwable t);
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org