org.glassfish.jersey.spi

## Class ThreadPoolExecutorProvider

- java.lang.Object
- - org.glassfish.jersey.spi.AbstractThreadPoolProvider<ThreadPoolExecutor>
  - - org.glassfish.jersey.spi.ThreadPoolExecutorProvider

All Implemented Interfaces:
  AutoCloseable, ExecutorServiceProvider

    public class ThreadPoolExecutorProvider
    extends AbstractThreadPoolProvider<ThreadPoolExecutor>
    implements ExecutorServiceProvider

Default implementation of the Jersey `executor service provider SPI`.

This provider creates and provisions a shared `ThreadPoolExecutor` instance using the customizable values for :

- `number of core pool threads`
- `maximum number of threads in the pool`
- `thread keep-alive time (in seconds)`
- `backing thread pool work queue`
- `backing thread factory`
- `rejected task handler`

Subclasses may override the respective methods to customize the parameters of the provisioned thread pool executor.

Since:
  2.18
Author:
  Marek Potociar

### Field Summary

### Fields inherited from class org.glassfish.jersey.spi.AbstractThreadPoolProvider

`DEFAULT_TERMINATION_TIMEOUT`

### Constructor Summary

Constructors
Constructor and Description
`ThreadPoolExecutorProvider(String name)` Create a new instance of the thread pool executor provider.

### Method Summary

All Methods Instance Methods Concrete Methods
`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.

### Methods inherited from class org.glassfish.jersey.spi.AbstractThreadPoolProvider

`close, getBackingThreadFactory, getCorePoolSize, getExecutor, getRejectedExecutionHandler, getTerminationTimeout, isClosed, onClose`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### ThreadPoolExecutorProvider

    public ThreadPoolExecutorProvider(String name)

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 Detail

#### getExecutorService

    public ExecutorService getExecutorService()

Description copied from interface: `ExecutorServiceProvider`
Get 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:
  `getExecutorService` in interface `ExecutorServiceProvider`
Returns:
  an executor service. Must not return `null`.

#### createExecutor

    protected final ThreadPoolExecutor createExecutor(int corePoolSize,
                                                      ThreadFactory threadFactory,
                                                      RejectedExecutionHandler handler)

Description copied from class: `AbstractThreadPoolProvider`
Create a new instance of the thread pool executor that should be provided by the `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.

Specified by:
  `createExecutor` in class `AbstractThreadPoolProvider<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:
  `AbstractThreadPoolProvider.getExecutor()`, `AbstractThreadPoolProvider.close()`, `AbstractThreadPoolProvider.getCorePoolSize()`, `AbstractThreadPoolProvider.getBackingThreadFactory()`, `AbstractThreadPoolProvider.getRejectedExecutionHandler()`

#### createExecutor

    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.

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.
Returns:
  new configured thread pool instance.
Throws:
  `IllegalArgumentException` - if one of the following holds:
  `corePoolSize < 0`
  `keepAliveTime < 0`
  `maximumPoolSize <= 0`
  `maximumPoolSize < corePoolSize`
  `NullPointerException` - if `workQueue` or `threadFactory` or `handler` is `null`.

#### 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:

- in case the `maximum pool size` is equal to `Integer.MAX_VALUE`
- `0L` in case the maximum pool size is lower than `java.lang.Integer#MAX_VALUE`

The default value computation closely corresponds to the thread pool executor configurations used in `Executors.newCachedThreadPool()` and `Executors.newFixedThreadPool(int)` methods.

Returns:
  thread keep-alive time (in seconds) for the provisioned thread pool executor.

#### getWorkQueue

    protected BlockingQueue<Runnable> 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 `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`

The default value computation closely corresponds to the thread pool executor configurations used in `Executors.newCachedThreadPool()` and `Executors.newFixedThreadPool(int)` methods.

Returns:
  work queue for the provisioned thread pool executor.

#### dispose

    public void dispose(ExecutorService executorService)

Description copied from interface: `ExecutorServiceProvider`
Invoked when Jersey runtime no longer requires use of the provided executor service.

Specified by:
  `dispose` in interface `ExecutorServiceProvider`
Parameters:
  `executorService` - executor service to be disposed.

#### preDestroy

    @PreDestroy
    public void preDestroy()

Container pre-destroy handler method.

Invoking the method `closes` this provider.