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

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.

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:

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.
Makes the namespace of the application component that submitted the task available to the task.
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 the WSPrincipal 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.


Enable the thread context service in the server.xml file. The thread context service is available under the <concurrent-1.0> feature.


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 propagates jeeMetadataContext only:
    <contextService id="threadContextSvc1" jndiName="concurrent/${id}">
  • Thread context service with classloaderContext and securityContext:
    <contextService jndiName="concurrent/threadContextSvc2">
  • Thread context service that inherits jeeMetadataContext from threadContextSvc1 and adds securityContext:
    <contextService jndiName="concurrent/threadContextSvc3" 

Example that looks up the default context service:

ContextService threadContextSvc =
    (ContextService) new InitialContext().lookup(
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:
    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, 
    processSalesOrderCompletion = (Callable<Boolean>)
     threadContextSvc1.createContextualProxy(processSaleCompletion, execProps, 
    //later from a different application component
    successful =;
    if (successful)
  • Example that specifies resource-env-ref in the web.xml file:
  • 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);