KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > util > thread > DefaultThreadManager


1 /***********************************************************************
2  * Copyright (c) 1999-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.util.thread;
19
20 import java.util.HashMap JavaDoc;
21 import org.apache.avalon.cornerstone.services.threads.ThreadManager;
22 import org.apache.excalibur.threadcontext.ThreadContext;
23 import org.apache.avalon.excalibur.thread.ThreadPool;
24 import org.apache.avalon.framework.configuration.Configurable;
25 import org.apache.avalon.framework.configuration.Configuration;
26 import org.apache.avalon.framework.configuration.ConfigurationException;
27 import org.apache.avalon.framework.component.Component;
28 import org.apache.avalon.framework.logger.AbstractLogEnabled;
29
30 /**
31  * Default implementation of ThreadManager.
32  *
33  * @phoenix:block
34  * @phoenix:service name="org.apache.avalon.cornerstone.services.threads.ThreadManager"
35  *
36  */

37 public class DefaultThreadManager
38    extends AbstractLogEnabled
39    implements ThreadManager, Configurable, Component
40 {
41     ///Map of thread pools for application
42
private HashMap JavaDoc m_threadPools = new HashMap JavaDoc();
43
44     /**
45      * Setup thread pools based on configuration data.
46      *
47      * @param configuration the configuration data
48      * @exception ConfigurationException if an error occurs
49      * @phoenix:configuration-schema type="relax-ng"
50      */

51        public void configure( final Configuration configuration )
52                throws ConfigurationException
53        {
54            ThreadContext threadContext = ThreadContext.getThreadContext();
55            if( null != threadContext )
56            {
57                threadContext = threadContext.duplicate();
58            }
59
60            final Configuration[] groups = configuration.getChildren( "thread-group" );
61            for( int i = 0; i < groups.length; i++ )
62            {
63                configureThreadPool( groups[ i ], threadContext );
64            }
65        }
66
67        private void configureThreadPool( final Configuration configuration,
68                                          final ThreadContext threadContext )
69                throws ConfigurationException
70        {
71            final String JavaDoc name = configuration.getChild( "name" ).getValue();
72            final int priority = configuration.getChild( "priority" ).getValueAsInteger( 5 );
73            final boolean isDaemon = configuration.getChild( "is-daemon" ).getValueAsBoolean( false );
74
75            final int minThreads = configuration.getChild( "min-threads" ).getValueAsInteger( 5 );
76            final int maxThreads = configuration.getChild( "max-threads" ).getValueAsInteger( 10 );
77            final int minSpareThreads = configuration.getChild( "min-spare-threads" ).
78                                        getValueAsInteger( maxThreads - minThreads );
79
80            try
81            {
82                final DefaultThreadPool threadPool =
83                    new DefaultThreadPool( name, minThreads, maxThreads, threadContext );
84                threadPool.setDaemon( isDaemon );
85                threadPool.enableLogging( getLogger() );
86                m_threadPools.put( name, threadPool );
87            }
88            catch( final Exception JavaDoc e )
89            {
90                final String JavaDoc message = "Error creating ThreadPool named " + name;
91                throw new ConfigurationException( message, e );
92            }
93        }
94
95     /**
96      * Retrieve a thread pool by name.
97      *
98      * @param name the name of thread pool
99      * @return the threadpool
100      * @exception IllegalArgumentException if the name of thread pool is
101      * invalid or named pool does not exist
102      */

103        public ThreadPool getThreadPool( final String JavaDoc name )
104                throws IllegalArgumentException JavaDoc
105        {
106            final ThreadPool threadPool = (ThreadPool)m_threadPools.get( name );
107
108            if( null == threadPool )
109            {
110                final String JavaDoc message = "Unable to locate ThreadPool named " + name;
111                throw new IllegalArgumentException JavaDoc( message );
112            }
113
114            return threadPool;
115        }
116
117     /**
118      * Retrieve the default thread pool.
119      *
120      * @return the thread pool
121      */

122        public ThreadPool getDefaultThreadPool()
123        {
124            return getThreadPool( "default" );
125        }
126 }
127
Popular Tags