Configuring thread context service instances
You can configure ContextService
instances to capture a managed thread
context and apply it to invocations of specified interface methods on any thread.
About this task
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.A default thread context service instance
(DefaultContextService
) is created by the server and configured to capture and
propagate at least the classloaderContext
, jeeMetadataContext
and
securityContext
elements. You can configure thread context propagation to include
the following types of thread context:
- classloaderContext
- Makes the 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 OSGi Web Application Bundles is not currently supported.
- jeeMetadataContext
- Makes the namespace of the application component that submitted the task available to the task.
- securityContext
- You must enable the
appSecurity-2.0
feature in the server.xml file to use this type of thread context. Makes the caller subject and invocation subject of the submitter of the task available to the task. This is accomplished by logging in with theWSPrincipal
credentials of the submitter using a JAAS login module. For details on what information in the submitter's subject is not in the security context, see the concurrent-1.0 feature restrictions.
- syncToOSThreadContext
- Synchronizes the identity of the runAs Subject for the unit of work with the operating system identity.
- zosWLMContext
- Used to classify work.
Procedure
Enable the thread context service in the
server.xml
file. The thread
context service is available under the <concurrent-1.0>
feature.
<featureManager>
<feature>concurrent-1.0</feature>
</featureManager>
Example
Configure thread context service instances in the server.xml file:
- Thread context service that is registered in JNDI with the name,
concurrent/threadContextSvc1
, that captures and propagatesjeeMetadataContext
only:<contextService id="threadContextSvc1" jndiName="concurrent/${id}"> <jeeMetadataContext/> </contextService>
- Thread context service with
classloaderContext
andsecurityContext
:<contextService jndiName="concurrent/threadContextSvc2"> <classloaderContext/> <securityContext/> </securityContext/>
- Thread context service that inherits
jeeMetadataContext
fromthreadContextSvc1
and addssecurityContext
:<contextService jndiName="concurrent/threadContextSvc3" baseContextRef="threadContextSvc1"> <securityContext> </contextService>
Example that looks up the default context service:
ContextService threadContextSvc =
(ContextService) new InitialContext().lookup(
"java:comp/DefaultContextService");
myContextualAsyncCallback = threadContextSvc.createContextualProxy(
myAsyncCallback, MyAsyncCallback.class);
doSomethingAsync(arg1, arg2, myContextualAsyncCallback);
Examples to inject thread context service instances into application components (by using
@Resource
) or look up with resource environment references
(resource-env-ref
).
- 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 specifies
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); ...