1 17 18 package org.apache.james.util.thread; 19 20 import java.util.HashMap ; 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 37 public class DefaultThreadManager 38 extends AbstractLogEnabled 39 implements ThreadManager, Configurable, Component 40 { 41 private HashMap m_threadPools = new HashMap (); 43 44 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 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 e ) 89 { 90 final String message = "Error creating ThreadPool named " + name; 91 throw new ConfigurationException( message, e ); 92 } 93 } 94 95 103 public ThreadPool getThreadPool( final String name ) 104 throws IllegalArgumentException 105 { 106 final ThreadPool threadPool = (ThreadPool)m_threadPools.get( name ); 107 108 if( null == threadPool ) 109 { 110 final String message = "Unable to locate ThreadPool named " + name; 111 throw new IllegalArgumentException ( message ); 112 } 113 114 return threadPool; 115 } 116 117 122 public ThreadPool getDefaultThreadPool() 123 { 124 return getThreadPool( "default" ); 125 } 126 } 127 | Popular Tags |