+

Search Tips   |   Advanced Search

Configure managed thread factories

We can configure ManagedThreadFactory instances to create new threads that run with a thread context of the thread from which the managed thread factory is looked up or injected. It is a best practice for Java EE applications to avoid directly managing their own threads; therefore, the ManagedThreadFactory extends the JSE ThreadFactory to provide a way to create managed threads within an application server environment. We might also configure the ManagedThreadFactory to capture a thread context that is relevant to Java EE applications and propagate it to the new thread.

The managed thread factory is available under the <concurrent-1.0> feature and enabled in server.xml as follows:

<featureManager>
 <feature>concurrent-1.0</feature>
</featureManager>

Thread context capture and propagation is managed by the context service. A default instance of the context service (DefaultContextService) is created by the server and configured to propagate at least classloaderContext, jeeMetadataContext and securityContext. This default context service instance is used if a ManagedThreadFactory is created without referring to a specific context service instance or configuring a context service instance directly within. For more information about context service instances, refer to the Configuring thread context service instances topic.

A default instance of ManagedThreadFactory (DefaultManagedThreadFactory) is available as java:comp/DefaultManagedThreadFactory and uses the default context service instance for thread context capture and propagation.

Example configuration in server.xml:


Example

Managed thread factories can be injected into application components (using @Resource) or looked up with resource environment references (resource-env-ref). Regardless of how the instance is obtained, it can be used interchangeably as javax.enterprise.concurrent.ManagedThreadFactory or java.util.concurrent.ThreadFactory.


Parent topic: Administer the Liberty profile manually