Interface ContextStorage


  • public interface ContextStorage
    The storage for storing and retrieving the current Context.

    If you want to implement your own storage or add some hooks when a Context is attached and restored, you should use ContextStorageProvider. Here's an example that sets MDC before Context is attached:

    
     > public class MyStorage implements ContextStorageProvider {
     >
     >   @Override
     >   public ContextStorage get() {
     >     ContextStorage threadLocalStorage = ContextStorage.defaultStorage();
     >     return new RequestContextStorage() {
     >       @Override
     >       public Scope T attach(Context toAttach) {
     >         Context current = current();
     >         setMdc(toAttach);
     >         Scope scope = threadLocalStorage.attach(toAttach);
     >         return () -> {
     >           clearMdc();
     >           setMdc(current);
     >           scope.close();
     >         }
     >       }
     >
     >       @Override
     >       public Context current() {
     >         return threadLocalStorage.current();
     >       }
     >     }
     >   }
     > }