[8.5.5.4 or later]
This topic applies to WebSphere Application Server Liberty V8.5.5.9 and earlier. For the latest Liberty topics, see the WebSphere Application Server Liberty documentation.

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 classloaderContext, jeeMetadataContext and securityContext. You 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. Liberty Repository[8.5.5.4 or later]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
[8.5.5.4 or later] 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, 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.

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 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 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);
    ...

Icon that indicates the type of topic Task topic



Timestamp icon Last updated: Tuesday, 12 December 2017
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=phil&product=was-libcore-mp&topic=twlp_config_contextservice
File name: twlp_config_contextservice.html