Interface RequestContextStorage

All Superinterfaces:
Unwrappable
All Known Implementing Classes:
RequestContextStorageWrapper

@UnstableApi
public interface RequestContextStorage
extends Unwrappable
The storage for storing RequestContext.

If you want to implement your own storage or add some hooks when a RequestContext is pushed and popped, you should use RequestContextStorageProvider. Here's an example that sets MDC before RequestContext is pushed:


 > public class MyStorage implements RequestContextStorageProvider {
 >
 >     @Override
 >     public RequestContextStorage newStorage() {
 >         RequestContextStorage threadLocalStorage = RequestContextStorage.threadLocal();
 >         return new RequestContextStorage() {
 >
 >             @Nullable
 >             @Override
 >             public <T extends RequestContext> T push(RequestContext toPush) {
 >                 setMdc(toPush);
 >                 return threadLocalStorage.push(toPush);
 >             }
 >
 >             @Override
 >             public void pop(RequestContext current, @Nullable RequestContext toRestore) {
 >                 clearMdc();
 >                 if (toRestore != null) {
 >                     setMdc(toRestore);
 >                 }
 >                 threadLocalStorage.pop(current, toRestore);
 >             }
 >             ...
 >          }
 >     }
 > }
 
  • Method Details

    • hook

      static void hook​(Function<? super RequestContextStorage,​? extends RequestContextStorage> function)
      Customizes the current RequestContextStorage by applying the specified Function to it. This method is useful when you need to perform an additional operation when a RequestContext is pushed or popped. Note:
      • All RequestContextStorage operations are highly performance-sensitive operation and thus it's not a good idea to run a time-consuming task.
      • This method must be invoked at the beginning of application startup, e.g. Never call this method in the middle of request processing.
    • threadLocal

      static RequestContextStorage threadLocal()
      Returns the default RequestContextStorage which stores the RequestContext in the thread-local.
    • push

      @Nullable <T extends RequestContext> T push​(RequestContext toPush)
      Pushes the specified RequestContext into the storage.
      Returns:
      the old RequestContext which was in the storage before the specified toPush is pushed. null, if there was no RequestContext.
    • pop

      void pop​(RequestContext current, @Nullable RequestContext toRestore)
      Pops the current RequestContext in the storage and pushes back the specified toRestore. toRestore is the RequestContext returned from when push(current) is called, so it can be null.

      The specified current must be the RequestContext in the storage. If it's not, it means that RequestContext.push() is not called using try-with-resources block, so the previous RequestContext is not popped properly.

    • currentOrNull

      @Nullable <T extends RequestContext> T currentOrNull()
      Returns the RequestContext in the storage. null if there is no RequestContext.
    • unwrap

      default RequestContextStorage unwrap()
      Description copied from interface: Unwrappable
      Unwraps this object and returns the object being decorated. If this Unwrappable is the innermost object, this method returns itself. For example:
      
       class Foo implements Unwrappable {}
      
       class Bar<T extends Unwrappable> extends AbstractUnwrappable<T> {
           Bar(T delegate) {
               super(delegate);
           }
       }
      
       class Qux<T extends Unwrappable> extends AbstractUnwrappable<T> {
           Qux(T delegate) {
               super(delegate);
           }
       }
      
       Foo foo = new Foo();
       assert foo.unwrap() == foo;
      
       Bar<Foo> bar = new Bar<>(foo);
       assert bar.unwrap() == foo;
      
       Qux<Bar<Foo>> qux = new Qux<>(bar);
       assert qux.unwrap() == bar;
       assert qux.unwrap().unwrap() == foo;
       
      Specified by:
      unwrap in interface Unwrappable