Configure Enterprise JavaBeans asynchronous methods
We can configure the Enterprise JavaBeans (EJB) container to use a ContextService instance to control which managed thread contexts are captured when an asynchronous EJB method is called. The captured contexts are established on the asynchronous thread before starting the EJB method.
When application security is enabled, only the security context is propagated to the threads on which asynchronous EJB methods are started. Configure a ContextService instance to override the default behavior is useful when we need to propagate additional contexts to the asynchronous threads.
Before starting the EJB method on an asynchronous thread, the EJB container establishes the following contexts associated with the EJB:
- classloader context
- Java EE metadata context
Therefore, it is not necessary to propagate these contexts from the calling thread, as the EJB container replaces them with the contexts associated with the target EJB.
- Configure the application server to include both an EJB feature that supports
asynchronous methods and concurrent feature in the server.xml file. For example, add the following to the server.xml file:
<featureManager> <feature>appSecurity-2.0</feature> <feature>concurrent-1.0</feature> <feature>ejbLite-3.2</feature> </featureManager>
- Configure a ContextService instance to capture and propagate the desired
contexts in the server.xml file. This example defines a ContextService configuration that is equivalent to the default behavior for asynchronous methods:
<contextService id="SameAsNoConfigAsyncContextService"> <securityContext/> </contextService>
If the security context is not included in the ContextService definition, then it is not captured and propagated. The ContextService definition is a replacement of the default behavior, not an addition to it. This example defines a ContextService configuration that also captures and propagates the z/OS WLM Context:
<contextService id="EJBAsyncContextService"> <securityContext/> <zosWLMContext defaultTransactionClass="TRAN1"/> </contextService>
Additional feature and context-specific configuration might be required depending on the additional contexts being captured and propagated.
- Configure the EJB container to use the specific ContextService
configuration in the server.xml file. For example:
<ejbContainer> <asynchronous contextServiceRef="EJBAsyncContextService"/> </ejbContainer>