Top Description Inners Methods
org.eclipse.microprofile.context

public Interface ManagedExecutor

extends ExecutorService
Imports
java.util.concurrent.CompletableFuture, .CompletionStage, .ExecutorService, java.util.function.Supplier, org.eclipse.microprofile.context.spi.ContextManagerProvider, .ContextManager

A container-managed executor service that creates instances of CompletableFuture, which it backs as the default asynchronous execution facility, both for the CompletableFuture itself as well as all dependent stages created from it, as well as all dependent stages created from those, and so on.

Example usage:

ManagedExecutor executor = ManagedExecutor.builder().propagated(ThreadContext.APPLICATION).cleared(ThreadContext.ALL_REMAINING).build();
...
CompletableFuture<Integer> future = executor
   .supplyAsync(supplier)
   .thenApplyAsync(function1)
   .thenApply(function2)
   ...

This specification allows for managed executors that propagate thread context as well as for those that do not, which might offer better performance. If thread context propagation is desired only for specific stages, or if you wish to override thread context propagation for specific stages, the ThreadContext.contextual* API methods can be used to propagate thread context to individual actions.

Example of single action with context propagation:

CompletableFuture<?> future = executor
   .runAsync(runnable1)
   .thenRun(threadContext.contextualRunnable(runnable2))
   .thenRunAsync(runnable3)
   ...

Managed executors that capture and propagate thread context must do so in a consistent and predictable manner, which is defined as follows,

Each type of thread context is applied (either as cleared or as previously captured) to the thread that runs the action. The applied thread context is removed after the action completes, whether successfully or exceptionally, restoring the thread's prior context.

When dependent stages are created from the completion stage, and likewise from any dependent stages created from those, and so on, thread context is captured or cleared in the same manner. This guarantees that the action performed by each stage always runs under the thread context of the code that creates the stage, unless the user explicitly overrides this by supplying a pre-contextualized action.

Thread context is similarly captured, cleared, applied, and afterward restored for the ExecutorService methods: execute, invokeAll, invokeAny, submit

This interface is intentionally kept compatible with ManagedExecutorService, with the hope that its methods might one day be contributed to that specification.

Managed executors that are created with the Builder or created for injection into applications via CDI must support the life cycle methods, including: awaitTermination, isShutdown, isTerminated, shutdown, shutdownNow. The application must invoke shutdown or shutdownNow to terminate the life cycle of each managed executor that it creates, once the managed executor is no longer needed. When the application stops, the container invokes shutdownNow for any remaining managed executors that the application did not already shut down. The shutdown methods signal the managed executor that it does not need to remain active to service subsequent requests and allow the container to properly clean up associated resources.

Managed executors which have a life cycle that is scoped to the container, including those obtained via mechanisms defined by EE Concurrency, must raise IllegalStateException upon invocation of the aforementioned life cycle methods, in order to preserve compatibility with that specification.

Managed executors can forward all contextualised async tasks to a backing executor service if set with ContextManager.Builder#withDefaultExecutorService(ExecutorService). Otherwise, a new backing executor service may be created, unless the implementation has its own default executor service.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface

Method Summary

Modifier and TypeMethod and Description
public static ManagedExecutor.Builder

Returns:

a new Builder instance.
builder
()

Creates a new Builder instance.

public <
result type of the completable future.
U
>
CompletableFuture<U>

Returns:

the new completable future.
completedFuture
(U
result with which the completable future is completed.
value
)

Returns a new CompletableFuture that is already completed with the specified value.

public <
result type of the completion stage.
U
>
CompletionStage<U>

Returns:

the new completion stage.
completedStage
(U
result with which the completable future is completed.
value
)

Returns a new CompletionStage that is already completed with the specified value.

public <
completable future result type.
T
>
CompletableFuture<T>

Returns:

the new completable future.
copy
(CompletableFuture<T>
a completable future whose completion triggers completion of the new completable future that is created by this method.
stage
)

Returns a new CompletableFuture that is completed by the completion of the specified stage.

public <
completion stage result type.
T
>
CompletionStage<T>

Returns:

the new completion stage.
copy
(CompletionStage<T>
a completion stage whose completion triggers completion of the new stage that is created by this method.
stage
)

Returns a new CompletionStage that is completed by the completion of the specified stage.

public <
result type of the completable future.
U
>
CompletableFuture<U>

Returns:

the new completable future.
failedFuture
(Throwable
exception or error with which the completable future is completed.
ex
)

Returns a new CompletableFuture that is already exceptionally completed with the specified Throwable.

public <
result type of the completion stage.
U
>
CompletionStage<U>

Returns:

the new completion stage.
failedStage
(Throwable
exception or error with which the stage is completed.
ex
)

Returns a new CompletionStage that is already exceptionally completed with the specified Throwable.

public ThreadContext

Returns:

a ThreadContext with the same propagation settings as this ManagedExecutor.
getThreadContext
()

Returns a ThreadContext which has the same propagation settings as this ManagedExecutor, which uses this ManagedExecutor as its default executor.

public <
result type of the completion stage.
U
>
CompletableFuture<U>

Returns:

the new completion stage.
newIncompleteFuture
()

Returns a new incomplete CompletableFuture.

public CompletableFuture<Void>

Returns:

the new completion stage.
runAsync
(Runnable
the action to run before completing the returned completion stage.
runnable
)

Returns a new CompletableFuture that is completed by a task running in this executor after it runs the given action.

public <
result type of the supplier and completion stage.
U
>
CompletableFuture<U>

Returns:

the new completion stage.
supplyAsync
(Supplier<U>
an action returning the value to be used to complete the returned completion stage.
supplier
)

Returns a new CompletableFuture that is completed by a task running in this executor after it runs the given action.

Inherited from java.util.concurrent.ExecutorService:
awaitTerminationcloseinvokeAllinvokeAllinvokeAnyinvokeAnyisShutdownisTerminatedshutdownshutdownNowsubmitsubmitsubmit

Method Detail

builderback to summary
public static ManagedExecutor.Builder builder()

Creates a new Builder instance.

Returns:ManagedExecutor.Builder

a new Builder instance.

completedFutureback to summary
public <U> CompletableFuture<U> completedFuture(U value)

Returns a new CompletableFuture that is already completed with the specified value.

This executor is the default asynchronous execution facility for the new completion stage that is returned by this method and all dependent stages that are created from it, and all dependent stages that are created from those, and so forth.

Parameters
<U>
result type of the completable future.
value:U

result with which the completable future is completed.

Returns:CompletableFuture<U>

the new completable future.

completedStageback to summary
public <U> CompletionStage<U> completedStage(U value)

Returns a new CompletionStage that is already completed with the specified value.

This executor is the default asynchronous execution facility for the new completion stage that is returned by this method and all dependent stages that are created from it, and all dependent stages that are created from those, and so forth.

Parameters
<U>
result type of the completion stage.
value:U

result with which the completable future is completed.

Returns:CompletionStage<U>

the new completion stage.

copyback to summary
public <T> CompletableFuture<T> copy(CompletableFuture<T> stage)

Returns a new CompletableFuture that is completed by the completion of the specified stage.

The new completable future is backed by the ManagedExecutor upon which copy is invoked, which serves as the default asynchronous execution facility for the new stage and all dependent stages created from it, and so forth.

When dependent stages are created from the new completable future, thread context is captured and/or cleared by the ManagedExecutor. This guarantees that the action performed by each stage always runs under the thread context of the code that creates the stage, unless the user explicitly overrides by supplying a pre-contextualized action.

Invocation of this method does not impact thread context propagation for the supplied completable future or any dependent stages created from it, other than the new dependent completable future that is created by this method.

Parameters
<T>
completable future result type.
stage:CompletableFuture<T>

a completable future whose completion triggers completion of the new completable future that is created by this method.

Returns:CompletableFuture<T>

the new completable future.

copyback to summary
public <T> CompletionStage<T> copy(CompletionStage<T> stage)

Returns a new CompletionStage that is completed by the completion of the specified stage.

The new completion stage is backed by the ManagedExecutor upon which copy is invoked, which serves as the default asynchronous execution facility for the new stage and all dependent stages created from it, and so forth.

When dependent stages are created from the new completion stage, thread context is captured and/or cleared by the ManagedExecutor. This guarantees that the action performed by each stage always runs under the thread context of the code that creates the stage, unless the user explicitly overrides by supplying a pre-contextualized action.

Invocation of this method does not impact thread context propagation for the supplied stage or any dependent stages created from it, other than the new dependent completion stage that is created by this method.

Parameters
<T>
completion stage result type.
stage:CompletionStage<T>

a completion stage whose completion triggers completion of the new stage that is created by this method.

Returns:CompletionStage<T>

the new completion stage.

failedFutureback to summary
public <U> CompletableFuture<U> failedFuture(Throwable ex)

Returns a new CompletableFuture that is already exceptionally completed with the specified Throwable.

This executor is the default asynchronous execution facility for the new completion stage that is returned by this method and all dependent stages that are created from it, and all dependent stages that are created from those, and so forth.

Parameters
<U>
result type of the completable future.
ex:Throwable

exception or error with which the completable future is completed.

Returns:CompletableFuture<U>

the new completable future.

failedStageback to summary
public <U> CompletionStage<U> failedStage(Throwable ex)

Returns a new CompletionStage that is already exceptionally completed with the specified Throwable.

This executor is the default asynchronous execution facility for the new completion stage that is returned by this method and all dependent stages that are created from it, and all dependent stages that are created from those, and so forth.

Parameters
<U>
result type of the completion stage.
ex:Throwable

exception or error with which the stage is completed.

Returns:CompletionStage<U>

the new completion stage.

getThreadContextback to summary
public ThreadContext getThreadContext()

Returns a ThreadContext which has the same propagation settings as this ManagedExecutor, which uses this ManagedExecutor as its default executor.

Returns:ThreadContext

a ThreadContext with the same propagation settings as this ManagedExecutor.

newIncompleteFutureback to summary
public <U> CompletableFuture<U> newIncompleteFuture()

Returns a new incomplete CompletableFuture.

This executor is the default asynchronous execution facility for the new completion stage that is returned by this method and all dependent stages that are created from it, and all dependent stages that are created from those, and so forth.

Parameters
<U>
result type of the completion stage.
Returns:CompletableFuture<U>

the new completion stage.

runAsyncback to summary
public CompletableFuture<Void> runAsync(Runnable runnable)

Returns a new CompletableFuture that is completed by a task running in this executor after it runs the given action.

This executor is the default asynchronous execution facility for the new completion stage that is returned by this method and all dependent stages that are created from it, and all dependent stages that are created from those, and so forth.

Parameters
runnable:Runnable

the action to run before completing the returned completion stage.

Returns:CompletableFuture<Void>

the new completion stage.

supplyAsyncback to summary
public <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)

Returns a new CompletableFuture that is completed by a task running in this executor after it runs the given action.

This executor is the default asynchronous execution facility for the new completion stage that is returned by this method and all dependent stages that are created from it, and all dependent stages that are created from those, and so forth.

Parameters
<U>
result type of the supplier and completion stage.
supplier:Supplier<U>

an action returning the value to be used to complete the returned completion stage.

Returns:CompletableFuture<U>

the new completion stage.

org.eclipse.microprofile.context back to summary

public Interface ManagedExecutor.Builder


Builder for ManagedExecutor instances.

Example usage:

 ManagedExecutor executor = ManagedExecutor.builder()
                                                 .maxAsync(5)
                                                 .maxQueued(20)
                                                 .propagated(ThreadContext.SECURITY)
                                                 .build();
...

Method Summary

Modifier and TypeMethod and Description
public ManagedExecutor

Returns:

new instance of ManagedExecutor.
build
()

Builds a new ManagedExecutor with the configuration that this builder represents as of the point in time when this method is invoked.

public ManagedExecutor.Builder

Returns:

the same builder instance upon which this method is invoked.
cleared
(String...
types of thread context to clear from threads that run actions and tasks.
types
)

Defines the set of thread context types to clear from the thread where the action or task executes.

public ManagedExecutor.Builder

Returns:

the same builder instance upon which this method is invoked.
maxAsync
(int
upper bound on async completion stage actions and executor tasks.
max
)

Establishes an upper bound on the number of async completion stage actions and async executor tasks that can be running at any given point in time.

public ManagedExecutor.Builder

Returns:

the same builder instance upon which this method is invoked.
maxQueued
(int
upper bound on async actions and tasks that can be queued.
max
)

Establishes an upper bound on the number of async actions and async tasks that can be queued up for execution.

public ManagedExecutor.Builder

Returns:

the same builder instance upon which this method is invoked.
propagated
(String...
types of thread context to capture and propagate.
types
)

Defines the set of thread context types to capture from the thread that creates a dependent stage (or that submits a task) and which to propagate to the thread where the action or task executes.

Method Detail

buildback to summary
public ManagedExecutor build()

Builds a new ManagedExecutor with the configuration that this builder represents as of the point in time when this method is invoked.

After build is invoked, the builder instance retains its configuration and may be further updated to represent different configurations and build additional ManagedExecutor instances.

All created instances of ManagedExecutor are destroyed when the application is stopped. The container automatically shuts down these managed executors.

Returns:ManagedExecutor

new instance of ManagedExecutor.

Exceptions
IllegalStateException:
for any of the following error conditions
  • if one or more of the same context types appear in both the cleared set and the propagated set
  • if a thread context type that is configured to be cleared or propagated is unavailable
  • if context configuration is neither specified on the builder nor via MicroProfile Config, and the builder implementation lacks vendor-specific defaults of its own.
  • if more than one provider provides the same thread context type
clearedback to summary
public ManagedExecutor.Builder cleared(String... types)

Defines the set of thread context types to clear from the thread where the action or task executes. The previous context is resumed on the thread after the action or task ends.

This set replaces the cleared set that was previously specified on the builder instance, if any.

For example, if the user specifies ThreadContext#TRANSACTION in this set, then a transaction is not active on the thread when the action or task runs, such that each action or task is able to independently start and end its own transactional work.

ThreadContext#ALL_REMAINING is automatically appended to the set of cleared context if the propagated set does not include ThreadContext#ALL_REMAINING.

Constants for specifying some of the core context types are provided on ThreadContext. Other thread context types must be defined by the specification that defines the context type or by a related MicroProfile specification.

The MicroProfile Config property, mp.context.ManagedExecutor.cleared, establishes a default that is used if no value is otherwise specified. The value of the MicroProfile Config property can be the empty string or a comma separated list of context type constant values.

Parameters
types:String[]

types of thread context to clear from threads that run actions and tasks.

Returns:ManagedExecutor.Builder

the same builder instance upon which this method is invoked.

maxAsyncback to summary
public ManagedExecutor.Builder maxAsync(int max)

Establishes an upper bound on the number of async completion stage actions and async executor tasks that can be running at any given point in time. There is no guarantee that async actions or tasks will start running immediately, even when the maxAsync constraint has not get been reached. Async actions and tasks remain queued until the ManagedExecutor starts executing them.

The default value of -1 indicates no upper bound, although practically, resource constraints of the system will apply. You can switch the default by specifying the MicroProfile Config property, mp.context.ManagedExecutor.maxAsync.

Parameters
max:int

upper bound on async completion stage actions and executor tasks.

Returns:ManagedExecutor.Builder

the same builder instance upon which this method is invoked.

Exceptions
IllegalArgumentException:
if max is 0 or less than -1.
maxQueuedback to summary
public ManagedExecutor.Builder maxQueued(int max)

Establishes an upper bound on the number of async actions and async tasks that can be queued up for execution. Async actions and tasks are rejected if no space in the queue is available to accept them.

The default value of -1 indicates no upper bound, although practically, resource constraints of the system will apply. You can switch the default by specifying the MicroProfile Config property, mp.context.ManagedExecutor.maxQueued.

Parameters
max:int

upper bound on async actions and tasks that can be queued.

Returns:ManagedExecutor.Builder

the same builder instance upon which this method is invoked.

Exceptions
IllegalArgumentException:
if max is 0 or less than -1.
propagatedback to summary
public ManagedExecutor.Builder propagated(String... types)

Defines the set of thread context types to capture from the thread that creates a dependent stage (or that submits a task) and which to propagate to the thread where the action or task executes.

This set replaces the propagated set that was previously specified on the builder instance, if any.

Constants for specifying some of the core context types are provided on ThreadContext. Other thread context types must be defined by the specification that defines the context type or by a related MicroProfile specification.

The MicroProfile Config property, mp.context.ManagedExecutor.propagated, establishes a default that is used if no value is otherwise specified. The value of the MicroProfile Config property can be the empty string or a comma separated list of context type constant values.

Thread context types which are not otherwise included in this set are cleared from the thread of execution for the duration of the action or task.

Parameters
types:String[]

types of thread context to capture and propagate.

Returns:ManagedExecutor.Builder

the same builder instance upon which this method is invoked.