package org.apache.commons.jcs3.utils.threadpool; /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import java.util.Iterator; import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import org.apache.commons.jcs3.log.Log; import org.apache.commons.jcs3.log.LogManager; import org.apache.commons.jcs3.utils.config.PropertySetter; /** * This manages threadpools for an application *
* It is a singleton since threads need to be managed vm wide. *
** This manager forces you to use a bounded queue. By default it uses the current thread for * execution when the buffer is full and no free threads can be created. *
** You can specify the props file to use or pass in a properties object prior to configuration. *
** If set, the Properties object will take precedence. *
*
* If a value is not set for a particular pool, the hard coded defaults in PoolConfiguration will be used.
* You can configure default settings by specifying thread_pool.default in the properties, ie "cache.ccf"
*
* @param config the pool configuration * @param threadNamePrefix prefix for the thread names of the pool * @return A ThreadPool wrapper */ public ExecutorService createPool( final PoolConfiguration config, final String threadNamePrefix) { return createPool(config, threadNamePrefix, Thread.NORM_PRIORITY); } /** * Creates a pool based on the configuration info. *
* @param config the pool configuration
* @param threadNamePrefix prefix for the thread names of the pool
* @param threadPriority the priority of the created threads
* @return A ThreadPool wrapper
*/
public ExecutorService createPool( final PoolConfiguration config, final String threadNamePrefix, final int threadPriority )
{
BlockingQueue
* @param config the pool configuration
* @param threadNamePrefix prefix for the thread names of the pool
* @param threadPriority the priority of the created threads
* @return A ScheduledExecutorService
*/
public ScheduledExecutorService createSchedulerPool( final PoolConfiguration config, final String threadNamePrefix, final int threadPriority )
{
return Executors.newScheduledThreadPool(
config.getMaximumPoolSize(),
new DaemonThreadFactory(threadNamePrefix, threadPriority));
}
/**
* Returns a configured instance of the ThreadPoolManger To specify a configuration file or
* Properties object to use call the appropriate setter prior to calling getInstance.
*
* @return The single instance of the ThreadPoolManager
*/
public static ThreadPoolManager getInstance()
{
return ThreadPoolManagerHolder.INSTANCE;
}
/**
* Dispose of the instance of the ThreadPoolManger and shut down all thread pools
*/
public static void dispose()
{
for ( final Iterator
* Services are lazily created.
*
* @param name
* @return The executor service configured for the name.
*/
public ExecutorService getExecutorService( final String name )
{
return pools.computeIfAbsent(name, key -> {
log.debug( "Creating pool for name [{0}]", key );
final PoolConfiguration config = loadConfig( PROP_NAME_ROOT + "." + key, defaultConfig );
return createPool( config, "JCS-ThreadPoolManager-" + key + "-" );
});
}
/**
* Returns a scheduler pool by name. If a pool by this name does not exist in the configuration file or
* properties, one will be created using the default values.
*
* Pools are lazily created.
*
* @param name
* @return The scheduler pool configured for the name.
*/
public ScheduledExecutorService getSchedulerPool( final String name )
{
return schedulerPools.computeIfAbsent(name, key -> {
log.debug( "Creating scheduler pool for name [{0}]", key );
final PoolConfiguration config = loadConfig( PROP_NAME_SCHEDULER_ROOT + "." + key,
defaultSchedulerConfig );
return createSchedulerPool( config, "JCS-ThreadPoolManager-" + key + "-", Thread.NORM_PRIORITY );
});
}
/**
* Returns the names of all configured pools.
*
* @return ArrayList of string names
*/
protected Set
* @param props The props to set.
*/
public static void setProps( final Properties props )
{
ThreadPoolManager.props = props;
}
/**
* Initialize the ThreadPoolManager and create all the pools defined in the configuration.
*/
private void configure()
{
log.debug( "Initializing ThreadPoolManager" );
if ( props == null )
{
log.warn( "No configuration settings found. Using hardcoded default values for all pools." );
props = new Properties();
}
// set initial default and then override if new settings are available
defaultConfig = loadConfig( DEFAULT_PROP_NAME_ROOT, new PoolConfiguration() );
defaultSchedulerConfig = loadConfig( DEFAULT_PROP_NAME_SCHEDULER_ROOT, new PoolConfiguration() );
}
/**
* Configures the PoolConfiguration settings.
*
* @param root the configuration key prefix
* @param defaultPoolConfiguration the default configuration
* @return PoolConfiguration
*/
private static PoolConfiguration loadConfig( final String root, final PoolConfiguration defaultPoolConfiguration )
{
final PoolConfiguration config = defaultPoolConfiguration.clone();
PropertySetter.setProperties( config, props, root + "." );
log.debug( "{0} PoolConfiguration = {1}", root, config );
return config;
}
}