Configure thread context service instances
We can configure ContextService instances to capture a managed thread context and apply it to invocations of specified interface methods on any thread.
It is a best practice for Java EE applications to avoid directly managing their own threads; therefore, the ContextService provides a way to establish a previously captured thread context onto unmanaged threads, as well as managed threads, overlaying any thread context that is in place.
Enable the thread context service in server.xml. The thread context service is available under the <concurrent-1.0> feature.
<featureManager> <feature>concurrent-1.0</feature> </featureManager>
Results
A default thread context service instance (DefaultContextService) is created by the server and configured to capture and propagate at least classloaderContext, jeeMetadataContext and securityContext. We can configure thread context propagation to include the following types of thread context:
- classloaderContext
- Makes the thread context classloader of the submitter of the task available to the task.
If the context classloader is serialized, the classloader must be a thread context classloader from the application. Classloader serialization for Web Application Bundles is not currently supported.
- jeeMetadataContext
- Makes the namespace of the application component that submitted the task available to the task.
- securityContext
We must enable the appSecurity-2.0 feature in server.xml to use this type of thread context. Makes the caller subject and invocation subject of the submitter of the task available to the task, and this is accomplished by logging in with the submitter's WSPrincipal using JAAS login. For details on what information in the submitter's subject is not in the security context, see the concurrent-1.0 feature restrictions.
Important: Additional thread context providers might be made available by features in stack products. The optional baseContextRef attribute allows a context service instance to inherit from the context configuration of another context service instance.
Example
Inject thread context service instances into application components (using @Resource) or look up with resource environment references (resource-env-ref).
- Example configuration in server.xml:
- Thread context service that is registered in JNDI with the name, concurrent/threadContextSvc1, that captures and propagates jeeMetadataContext only:
<contextService id="threadContextSvc1" jndiName="concurrent/${id}"> <jeeMetadataContext/> </contextService>
- Thread context service with classloaderContext and securityContext:
<contextService jndiName="concurrent/threadContextSvc2"> <classloaderContext/> <securityContext/> </securityContext/>
- Thread context service that inherits jeeMetadataContext from threadContextSvc1 and adds securityContext:
<contextService jndiName="concurrent/threadContextSvc3" baseContextRef="threadContextSvc1"> <securityContext> </contextService>
- Example that uses @Resource:
@Resource(lookup="concurrent/threadContextSvc1") ContextService threadContextSvc1; ... Callable<Integer> processSalesOrderCompletion = new Callable<Integer>() { public Integer call() throws Exception { DataSource ds = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/ds1"); ...update various database tables return isSuccessful; } }; // capture thread context of current application component execProps = Collections.singletonMap(ManagedTask.TRANSACTION, ManagedTask.USE_TRANSACTION_OF_EXECUTION_THREAD); processSalesOrderCompletion = (Callable<Boolean>) threadContextSvc1.createContextualProxy(processSaleCompletion, execProps, Callable.class); //later from a different application component tran.begin(); ... successful = processSalesOrderCompletion.call(); if (successful) tran.commit(); else tran.rollback();
- Example that uses resource-env-ref in the web.xml file:
<resource-env-ref> <resource-env-ref-name>concurrent/threadContextSvc3</resource-env-ref-name> <resource-env-ref-type>javax.enterprise.concurrent.ContextService</resource- env-ref-type> </resource-env-ref>
- Example lookup that uses the resource environment reference:
ContextService threadContextSvc3 = (ContextService) new InitialContext().lookup("java:comp/env/concurrent/threadContextSvc3"); Runnable updateAndGetNextFromDatabase = threadContextSvc3.createContextualProxy (new Runnable() { public void run() { DataSource ds = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/ds1"); ... update the database and get next item to process } }, Runnable.class); barrier = new CyclicBarrier(3, updateAndGetNextFromDatabase); ...
Parent topic: Administer the Liberty profile manually