Class ThreadPoolExecutorProvider
- All Implemented Interfaces:
AutoCloseable,ExecutorServiceProvider
executor service provider SPI.
This provider creates and provisions a shared ThreadPoolExecutor instance
using the customizable values for :
- Since:
- 2.18
- Author:
- Marek Potociar
-
Field Summary
Fields inherited from class org.glassfish.jersey.spi.AbstractThreadPoolProvider
DEFAULT_TERMINATION_TIMEOUT -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new instance of the thread pool executor provider. -
Method Summary
Modifier and TypeMethodDescriptionprotected ThreadPoolExecutorcreateExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) Creates a newThreadPoolExecutorwith the given initial parameters.protected final ThreadPoolExecutorcreateExecutor(int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler) Create a new instance of the thread pool executor that should be provided by theAbstractThreadPoolProvider.getExecutor()method.voiddispose(ExecutorService executorService) Invoked when Jersey runtime no longer requires use of the provided executor service.Get an executor service to be used by Jersey client or server runtime to execute specific tasks.protected longGet the thread keep-alive time (in seconds).protected intGet the maximum number of threads to allow in the thread pool.protected BlockingQueue<Runnable>Get the work queue for the provisioned thread pool executor.voidContainer pre-destroy handler method.Methods inherited from class org.glassfish.jersey.spi.AbstractThreadPoolProvider
close, getBackingThreadFactory, getCorePoolSize, getExecutor, getRejectedExecutionHandler, getTerminationTimeout, isClosed, onClose
-
Constructor Details
-
ThreadPoolExecutorProvider
Create a new instance of the thread pool executor provider.- Parameters:
name- provider name. The name will be used to name the threads created & used by the provisioned thread pool executor.
-
-
Method Details
-
getExecutorService
Description copied from interface:ExecutorServiceProviderGet an executor service to be used by Jersey client or server runtime to execute specific tasks.This method is usually invoked just once at either Jersey client or server application runtime initialization, it may however be invoked multiple times. Once the instance of the provided executor service is not needed anymore by Jersey application runtime, it will be
disposed. This typically happens in one of the following situations:- Jersey client instance is closed (client runtime is shut down).
- Jersey container running a server-side Jersey application is shut down.
- Jersey server-side application is un-deployed.
- Specified by:
getExecutorServicein interfaceExecutorServiceProvider- Returns:
- an executor service. Must not return
null.
-
createExecutor
protected final ThreadPoolExecutor createExecutor(int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler) Description copied from class:AbstractThreadPoolProviderCreate a new instance of the thread pool executor that should be provided by theAbstractThreadPoolProvider.getExecutor()method.Concrete implementations of this class must override this method and implement the logic that creates the executor service to be provided. The returned thread pool executor will be shut down when this provider instance is
closed.This method is invoked at most once, during the first call to the
getExecutor()method.- Specified by:
createExecutorin classAbstractThreadPoolProvider<ThreadPoolExecutor>- Parameters:
corePoolSize- number of core threads the provisioned thread pool executor should provide.threadFactory- thread factory to be used by the provisioned thread pool executor when creating new threads.handler- handler for tasks that cannot be executed by the provisioned thread pool executor (e.g. due to a shutdown).- Returns:
- new instance of the provided thread pool executor.
- See Also:
-
createExecutor
protected ThreadPoolExecutor createExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) Creates a newThreadPoolExecutorwith the given initial parameters.- Parameters:
corePoolSize- the number of threads to keep in the thread pool, even if they are idle.maximumPoolSize- the maximum number of threads to allow in the thread pool.keepAliveTime- when the number of threads is greater than the core, this is the maximum time (in seconds) that excess idle threads will wait for new tasks before terminating.workQueue- the queue to use for holding tasks before they are executed. This queue will hold only theRunnabletasks submitted by theexecutemethod.threadFactory- the factory to use when the executor creates a new thread.handler- the handler to use when execution is blocked because the thread bounds and queue capacities are reached.- Returns:
- new configured thread pool instance.
- Throws:
IllegalArgumentException- if one of the following holds:
corePoolSize < 0
keepAliveTime < 0
maximumPoolSize <= 0
maximumPoolSize < corePoolSizeNullPointerException- ifworkQueueorthreadFactoryorhandlerisnull.
-
getMaximumPoolSize
protected int getMaximumPoolSize()Get the maximum number of threads to allow in the thread pool.The value from this method is passed as one of the input parameters in a call to the
createExecutor(int, int, long, java.util.concurrent.BlockingQueue, java.util.concurrent.ThreadFactory, java.util.concurrent.RejectedExecutionHandler)method.The method can be overridden to customize the maximum number of threads allowed in the provisioned thread pool executor. If not customized, the method defaults to
Integer.MAX_VALUE.- Returns:
- maximum number of threads allowed in the thread pool.
-
getKeepAliveTime
protected long getKeepAliveTime()Get the thread keep-alive time (in seconds).When the number of threads in the provisioned thread pool is greater than the core, this is the maximum time (in seconds) that excess idle threads will wait for new tasks before terminating.
The value from this method is passed as one of the input parameters in a call to the
createExecutor(int, int, long, java.util.concurrent.BlockingQueue, java.util.concurrent.ThreadFactory, java.util.concurrent.RejectedExecutionHandler)method.The method can be overridden to customize the thread keep-alive time in the provisioned thread pool executor. If not customized, the method defaults to:
- 60L in case the
maximum pool sizeis equal toInteger.MAX_VALUE 0Lin case the maximum pool size is lower thanjava.lang.Integer#MAX_VALUE
Executors.newCachedThreadPool()andExecutors.newFixedThreadPool(int)methods.- Returns:
- thread keep-alive time (in seconds) for the provisioned thread pool executor.
- 60L in case the
-
getWorkQueue
Get the work queue for the provisioned thread pool executor.The work queue is used to hold the tasks before they are executed by the provisioned thread pool executor. The queue will hold only the
Runnabletasks submitted by theThreadPoolExecutor.execute(java.lang.Runnable)method.The value from this method is passed as one of the input parameters in a call to the
createExecutor(int, int, long, java.util.concurrent.BlockingQueue, java.util.concurrent.ThreadFactory, java.util.concurrent.RejectedExecutionHandler)method.The method can be overridden to customize the work queue used by the provisioned thread pool executor. If not customized, the method defaults to:
SynchronousQueuein case themaximum pool sizeis equal toInteger.MAX_VALUELinkedBlockingQueuein case the maximum pool size is lower thanjava.lang.Integer#MAX_VALUE
Executors.newCachedThreadPool()andExecutors.newFixedThreadPool(int)methods.- Returns:
- work queue for the provisioned thread pool executor.
-
dispose
Description copied from interface:ExecutorServiceProviderInvoked when Jersey runtime no longer requires use of the provided executor service.- Specified by:
disposein interfaceExecutorServiceProvider- Parameters:
executorService- executor service to be disposed.
-
preDestroy
@PreDestroy public void preDestroy()Container pre-destroy handler method.Invoking the method
closesthis provider.
-