Author: giacomo Date: Sat Nov 6 14:43:55 2004 New Revision: 56783 Modified: cocoon/trunk/src/java/org/apache/cocoon/components/thread/DefaultRunnableManager.java cocoon/trunk/src/java/org/apache/cocoon/components/thread/DefaultThreadPool.java Log: first bug fixes
Modified: cocoon/trunk/src/java/org/apache/cocoon/components/thread/DefaultRunnableManager.java ============================================================================== --- cocoon/trunk/src/java/org/apache/cocoon/components/thread/DefaultRunnableManager.java (original) +++ cocoon/trunk/src/java/org/apache/cocoon/components/thread/DefaultRunnableManager.java Sat Nov 6 14:43:55 2004 @@ -112,7 +112,7 @@ * Sorted set of <code>ExecutionInfo</code> instances, based on their next * execution time. */ - protected SortedSet m_executionInfo = new TreeSet( ); + protected SortedSet m_commandStack = new TreeSet( ); /** The managed thread pools */ final Map m_pools = new HashMap( ); @@ -146,44 +146,11 @@ } final Configuration [] threadpools = - config.getChildren( "thread-pools" ); + config.getChild( "thread-pools" ).getChildren( "thread-pool" ); for( int i = 0; i < threadpools.length; i++ ) { final DefaultThreadPool pool = configThreadPool( threadpools[ i ] ); - - if( getLogger( ).isInfoEnabled( ) ) - { - if( pool.isQueued( ) ) - { - final StringBuffer msg = new StringBuffer( ); - msg.append( "ThreadPool named \"" ).append( pool.getName( ) ); - msg.append( "\" created with queue-size=" ); - msg.append( pool.getQueueSize( ) ); - msg.append( ",max-pool-size=" ).append( pool.getMaximumPoolSize( ) ); - msg.append( ",min-pool-size=" ).append( pool.getMinimumPoolSize( ) ); - msg.append( ",priority=" ).append( pool.getPriority( ) ); - msg.append( ",isDaemon=" ).append( ( (ThreadFactory)pool.getThreadFactory( ) ).isDaemon( ) ); - msg.append( ",keep-alive-time-ms=" ).append( pool.getKeepAliveTime( ) ); - msg.append( ",block-policy=\"" ).append( pool.getBlockPolicy( ) ); - msg.append( "\",shutdown-wait-time-ms=" ).append( pool.getShutdownWaitTimeMs( ) ); - getLogger( ).info( msg.toString( ) ); - } - else - { - final StringBuffer msg = new StringBuffer( ); - msg.append( "ThreadPool named \"" ).append( pool.getName( ) ); - msg.append( "\" created with no queue,max-pool-size=" ) - .append( pool.getMaximumPoolSize( ) ); - msg.append( ",min-pool-size=" ).append( pool.getMinimumPoolSize( ) ); - msg.append( ",priority=" ).append( pool.getPriority( ) ); - msg.append( ",isDaemon=" ).append( ( (ThreadFactory)pool.getThreadFactory( ) ).isDaemon( ) ); - msg.append( ",keep-alive-time-ms=" ).append( pool.getKeepAliveTime( ) ); - msg.append( ",block-policy=" ).append( pool.getBlockPolicy( ) ); - msg.append( ",shutdown-wait-time-ms=" ).append( pool.getShutdownWaitTimeMs( ) ); - getLogger( ).info( msg.toString( ) ); - } - } } // Check if a "default" pool has been created @@ -436,20 +403,20 @@ while( m_keepRunning ) { - synchronized( m_executionInfo ) + synchronized( m_commandStack ) { try { - if( m_executionInfo.size( ) > 0 ) + if( m_commandStack.size( ) > 0 ) { final ExecutionInfo info = - (ExecutionInfo)m_executionInfo.first( ); + (ExecutionInfo)m_commandStack.first( ); final long delay = info.m_nextRun - System.currentTimeMillis( ); if( delay > 0 ) { - m_executionInfo.wait( delay ); + m_commandStack.wait( delay ); } } else @@ -459,7 +426,7 @@ getLogger( ).debug( "No commands available. Will just wait for one" ); } - m_executionInfo.wait( ); + m_commandStack.wait( ); } } catch( final InterruptedException ie ) @@ -472,14 +439,17 @@ if( m_keepRunning ) { - final ExecutionInfo info = - (ExecutionInfo)m_executionInfo.first( ); - final long delay = - info.m_nextRun - System.currentTimeMillis( ); - - if( delay < 0 ) + if( m_commandStack.size( ) > 0 ) { - info.execute( ); + final ExecutionInfo info = + (ExecutionInfo)m_commandStack.first( ); + final long delay = + info.m_nextRun - System.currentTimeMillis( ); + + if( delay < 0 ) + { + info.execute( ); + } } } } @@ -518,9 +488,9 @@ { m_keepRunning = false; - synchronized( m_executionInfo ) + synchronized( m_commandStack ) { - m_executionInfo.notifyAll( ); + m_commandStack.notifyAll( ); } } @@ -547,6 +517,9 @@ } else { + getLogger( ).warn( "Unknown thread priority \"" + priority + + "\". Set to \"NORM\"." ); + return Thread.NORM_PRIORITY; } } @@ -566,8 +539,14 @@ final String name = config.getChild( "name" ).getValue( ); final int queueSize = config.getChild( "queue-size" ).getValueAsInteger( DEFAULT_QUEUE_SIZE ); - final int maxPoolSize = + int maxPoolSize = config.getChild( "max-pool-size" ).getValueAsInteger( DEFAULT_MAX_POOL_SIZE ); + + if( maxPoolSize < 0 ) + { + maxPoolSize = Integer.MAX_VALUE; + } + int minPoolSize = config.getChild( "min-pool-size" ).getValueAsInteger( DEFAULT_MIN_POOL_SIZE ); @@ -578,13 +557,27 @@ { minPoolSize = DEFAULT_MIN_POOL_SIZE; } + else if( minPoolSize < 1 ) + { + getLogger( ).warn( "Config element min-pool-size < 1 for pool \"" + + name + "\". Set to 1" ); + minPoolSize = 1; + } final String priority = config.getChild( "priority" ).getValue( DEFAULT_THREAD_PRIORITY ); final boolean isDaemon = config.getChild( "daemon" ).getValueAsBoolean( DEFAULT_DAEMON_MODE ); - final long keepAliveTime = + long keepAliveTime = config.getChild( "keep-alive-time-ms" ).getValueAsLong( DEFAULT_KEEP_ALIVE_TIME ); + + if( keepAliveTime < 0 ) + { + getLogger( ).warn( "Config element keep-alive-time-ms < 0 for pool \"" + + name + "\". Set to 1000" ); + keepAliveTime = 1000; + } + final String blockPolicy = config.getChild( "block-policy" ).getValue( DefaultThreadPool.POLICY_DEFAULT ); final boolean shutdownGraceful = @@ -668,9 +661,51 @@ m_pools.put( name, pool ); } + printPoolInfo( pool ); + return pool; } + /** + * DOCUMENT ME! + * + * @param pool DOCUMENT ME! + */ + private void printPoolInfo( final DefaultThreadPool pool ) + { + if( getLogger( ).isInfoEnabled( ) ) + { + if( pool.isQueued( ) ) + { + final StringBuffer msg = new StringBuffer( ); + msg.append( "ThreadPool named \"" ).append( pool.getName( ) ); + msg.append( "\" created with maximum queue-size=" ); + msg.append( pool.getMaxQueueSize( ) ); + msg.append( ",max-pool-size=" ).append( pool.getMaximumPoolSize( ) ); + msg.append( ",min-pool-size=" ).append( pool.getMinimumPoolSize( ) ); + msg.append( ",priority=" ).append( pool.getPriority( ) ); + msg.append( ",isDaemon=" ).append( ( (ThreadFactory)pool.getThreadFactory( ) ).isDaemon( ) ); + msg.append( ",keep-alive-time-ms=" ).append( pool.getKeepAliveTime( ) ); + msg.append( ",block-policy=\"" ).append( pool.getBlockPolicy( ) ); + msg.append( "\",shutdown-wait-time-ms=" ).append( pool.getShutdownWaitTimeMs( ) ); + getLogger( ).info( msg.toString( ) ); + } + else + { + final StringBuffer msg = new StringBuffer( ); + msg.append( "ThreadPool named \"" ).append( pool.getName( ) ); + msg.append( "\" created with no queue,max-pool-size=" ).append( pool.getMaximumPoolSize( ) ); + msg.append( ",min-pool-size=" ).append( pool.getMinimumPoolSize( ) ); + msg.append( ",priority=" ).append( pool.getPriority( ) ); + msg.append( ",isDaemon=" ).append( ( (ThreadFactory)pool.getThreadFactory( ) ).isDaemon( ) ); + msg.append( ",keep-alive-time-ms=" ).append( pool.getKeepAliveTime( ) ); + msg.append( ",block-policy=" ).append( pool.getBlockPolicy( ) ); + msg.append( ",shutdown-wait-time-ms=" ).append( pool.getShutdownWaitTimeMs( ) ); + getLogger( ).info( msg.toString( ) ); + } + } + } + //~ Inner Classes ---------------------------------------------------------- /** @@ -726,10 +761,10 @@ m_logger = logger; m_nextRun = System.currentTimeMillis( ) + delay; - synchronized( m_executionInfo ) + synchronized( m_commandStack ) { - m_executionInfo.add( this ); - m_executionInfo.notifyAll( ); + m_commandStack.add( this ); + m_commandStack.notifyAll( ); } } @@ -762,16 +797,16 @@ ",interval=" + m_interval ); } - synchronized( m_executionInfo ) + synchronized( m_commandStack ) { - m_executionInfo.remove( this ); + m_commandStack.remove( this ); m_nextRun = ( ( m_interval > 0 ) ? ( System.currentTimeMillis( ) + m_interval ) : 0 ); if( m_nextRun > 0 ) { - m_executionInfo.add( this ); - m_executionInfo.notifyAll( ); + m_commandStack.add( this ); + m_commandStack.notifyAll( ); } } Modified: cocoon/trunk/src/java/org/apache/cocoon/components/thread/DefaultThreadPool.java ============================================================================== --- cocoon/trunk/src/java/org/apache/cocoon/components/thread/DefaultThreadPool.java (original) +++ cocoon/trunk/src/java/org/apache/cocoon/components/thread/DefaultThreadPool.java Sat Nov 6 14:43:55 2004 @@ -27,7 +27,7 @@ * configuration into the <code>configure</code> method. * * @author <a href="mailto:giacomo.at.apache.org">Giacomo Pati</a> - * @version CVS $Id: DefaultThreadPool.java,v 1.5 2004/06/23 20:25:43 giacomo Exp $ + * @version CVS $Id$ */ public class DefaultThreadPool extends PooledExecutor @@ -103,6 +103,18 @@ /** * DOCUMENT ME! * + * @return maximum size of the queue (0 if isQueued() == false) + * + * @see org.apache.cocoon.components.thread.ThreadPool#getQueueSize() + */ + public int getMaxQueueSize( ) + { + return ( ( m_queueSize < 0 ) ? Integer.MAX_VALUE : m_queueSize ); + } + + /** + * DOCUMENT ME! + * * @return size of queue (0 if isQueued() == false) * * @see org.apache.cocoon.components.thread.ThreadPool#getQueueSize() @@ -123,8 +135,8 @@ /** * Get hte priority used to create Threads * - * @return [EMAIL PROTECTED] Thread#MIN_PRIORITY}, [EMAIL PROTECTED] - * Thread#NORM_PRIORITY}, or [EMAIL PROTECTED] Thread#MAX_PRIORITY} + * @return [EMAIL PROTECTED] Thread#MIN_PRIORITY}, [EMAIL PROTECTED] Thread#NORM_PRIORITY}, or + * [EMAIL PROTECTED] Thread#MAX_PRIORITY} */ public int getPriority( ) { @@ -134,7 +146,7 @@ /** * DOCUMENT ME! * - * @return size of queue (0 if isQueued() == false) + * @return current size of the queue (0 if isQueued() == false) * * @see org.apache.cocoon.components.thread.ThreadPool#getQueueSize() */ @@ -210,6 +222,8 @@ */ void setBlockPolicy( final String blockPolicy ) { + m_blockPolicy = blockPolicy; + if( POLICY_ABORT.equalsIgnoreCase( blockPolicy ) ) { abortWhenBlocked( ); @@ -259,6 +273,16 @@ /** * DOCUMENT ME! * + * @param priority The priority to set. + */ + void setPriority( final int priority ) + { + m_priority = priority; + } + + /** + * DOCUMENT ME! + * * @param queueSize DOCUMENT ME! */ void setQueue( final int queueSize ) @@ -331,13 +355,5 @@ private Logger getLogger( ) { return m_logger; - } - - /** - * @param priority The priority to set. - */ - void setPriority( final int priority ) - { - m_priority = priority; } }