1.4.8. Thread Context Holder


You are looking at documentation for an older release. Not what you want? See the current release documentation.

Thread Context Holder contract

A thread context holder defines a component that holds variables of ThreadLocal type whose value is required by the component to work normally and cannot be recovered. This component is mainly used when we want to do a task asynchronously. To ensure that the task will be executed in the same conditions as if it would be executed synchronously, we need to transfer the thread context from the original thread to the executor thread.

public interface ThreadContextHolder

    * Gives the value corresponding to the context of the thread
    * @return a new instance of {@link ThreadContext} if there are some
    * valuable {@link ThreadLocal} variables to share otherwise <code>null</code>
    * is expected
   ThreadContext getThreadContext();


This interface must be used with caution, only the most important components that have ThreadLocal variables whose value cannot be recovered should implement this interface.

Thread Context Handler

To be able to transfer the values of all the ThreadLocal variables (provided thanks to a ThreadContext instance) of all the registered components of ThreadContextHolder type, you can simply use a thread context handler as below:


// Steps to be done in the context of the initial thread
// Create a new instance of ThreadContextHandler for a given ExoContainer
ThreadContextHandler handler = new ThreadContextHandler(container);
// Stores into memory the current values of all the Thread Local variables
// of all the registered ThreadContextHolder of the eXo container.
// Steps to be done in the context of the executor thread
try {
  // Pushes values stored into memory into all the Thread Local variables
  // of all the registered ThreadContextHolder of the eXo Container
} finally {
  // Restores all the Thread Local variables of all the registered ThreadContextHolder
  // of the eXo Container
Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus