cogShare contextual information between components
TypeJAR
Developed by

XWiki Development Team

Rating
Rate!
0 Votes
LicenseGNU Lesser General Public License 2.1
Bundled With

XWiki Enterprise, XWiki Standard

Description

This module control the ExecutionContext and related common tools.

It was first introduced as a replaced for the XWikiContext which used to be passed as parameter in most XWiki classes methods. The ExecutionContext context is stored in a ThreadLocal and as such as be reached from anywhere should it be needed, the main entry point to access it is the component Execution.

Push/pop ExecutionContext

It's possible to push (and put) a new ExecutionContext in the current thread for various isolation needs.

Since 11.9 it's recommended to do that trough the ExecutionContextManager component:

   @Inject
   private ExecutionContextManager contextManager;

   private void foo() throws ExecutionContextException
   {
       this.contextManager.push(new ExecutionContext(), true);

       try {
           [...]
       } finally {
           this.contextManager.push(new ExecutionContext(), true);
       }
   }

Before 11.9 you can will use the APIs provided by Execution component.

Save and restore contextual information

Since 10.10RC1.

Saving and restoring specific contextual information is useful for example when execution asynchronous code which behavior still depends on various information located in the initial context.

This is done trough org.xwiki.context.concurrent.ContextStoreManager.

Save

   @Inject
   private ContextStoreManager contextStore;

   private void addTask() throws ComponentLookupException
   {
        Map<String, Serializable> storedContext = this.contextStore.save(Arrays.asList("wiki", "user", "locale"));

       [...]
   }

Restore

   @Inject
   private ContextStoreManager contextStore;

   private void startTask(Map<String, Serializable> storedContext) throws ComponentLookupException
   {
       this.contextStore.restore(storedContext);

       [...]
   }
Tags:
Created by Thomas Mortagne on 2018/11/19 15:33
    

Get Connected