public class ThreadPoolExecutorProvider extends AbstractThreadPoolProvider<ThreadPoolExecutor> implements ExecutorServiceProvider
executor service provider SPI
.
This provider creates and provisions a shared ThreadPoolExecutor
instance
using the customizable values for :
DEFAULT_TERMINATION_TIMEOUT
Constructor and Description |
---|
ThreadPoolExecutorProvider(String name)
Create a new instance of the thread pool executor provider.
|
Modifier and Type | Method and Description |
---|---|
protected ThreadPoolExecutor |
createExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
Creates a new
ThreadPoolExecutor with the given initial parameters. |
protected ThreadPoolExecutor |
createExecutor(int corePoolSize,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
Create a new instance of the thread pool executor that should be provided by the
AbstractThreadPoolProvider.getExecutor() method. |
void |
dispose(ExecutorService executorService)
Invoked when Jersey runtime no longer requires use of the provided executor service.
|
ExecutorService |
getExecutorService()
Get an executor service to be used by Jersey client or server runtime to execute specific tasks.
|
protected long |
getKeepAliveTime()
Get the thread keep-alive time (in seconds).
|
protected int |
getMaximumPoolSize()
Get the maximum number of threads to allow in the thread pool.
|
protected BlockingQueue<Runnable> |
getWorkQueue()
Get the work queue for the provisioned thread pool executor.
|
void |
preDestroy()
Container pre-destroy handler method.
|
close, getBackingThreadFactory, getCorePoolSize, getExecutor, getRejectedExecutionHandler, getTerminationTimeout, isClosed, onClose
public ThreadPoolExecutorProvider(String name)
name
- provider name. The name will be used to name the threads created & used by the
provisioned thread pool executor.public ExecutorService getExecutorService()
ExecutorServiceProvider
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:
getExecutorService
in interface ExecutorServiceProvider
null
.protected final ThreadPoolExecutor createExecutor(int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler)
AbstractThreadPoolProvider
AbstractThreadPoolProvider.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.
createExecutor
in class AbstractThreadPoolProvider<ThreadPoolExecutor>
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).AbstractThreadPoolProvider.getExecutor()
,
AbstractThreadPoolProvider.close()
,
AbstractThreadPoolProvider.getCorePoolSize()
,
AbstractThreadPoolProvider.getBackingThreadFactory()
,
AbstractThreadPoolProvider.getRejectedExecutionHandler()
protected ThreadPoolExecutor createExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)
ThreadPoolExecutor
with the given initial 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 the
Runnable
tasks submitted by the execute
method.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.IllegalArgumentException
- if one of the following holds:corePoolSize < 0
keepAliveTime < 0
maximumPoolSize <= 0
maximumPoolSize < corePoolSize
NullPointerException
- if workQueue
or threadFactory
or handler
is null
.protected int getMaximumPoolSize()
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
.
protected long getKeepAliveTime()
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:
maximum pool size
is equal to Integer.MAX_VALUE
0L
in case the maximum pool size is lower than
java.lang.Integer#MAX_VALUE
Executors.newCachedThreadPool()
and
Executors.newFixedThreadPool(int)
methods.
protected BlockingQueue<Runnable> getWorkQueue()
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 Runnable
tasks submitted by the ThreadPoolExecutor.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:
SynchronousQueue
in case the maximum pool size
is equal to Integer.MAX_VALUE
LinkedBlockingQueue
in case the maximum pool size is lower than
java.lang.Integer#MAX_VALUE
Executors.newCachedThreadPool()
and
Executors.newFixedThreadPool(int)
methods.
public void dispose(ExecutorService executorService)
ExecutorServiceProvider
dispose
in interface ExecutorServiceProvider
executorService
- executor service to be disposed.@PreDestroy public void preDestroy()
Invoking the method closes
this provider.
Copyright © 2007-2017, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.