Package com.linecorp.armeria.common
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 Summary
Modifier and Type Method Description <T extends RequestContext>
TcurrentOrNull()
Returns theRequestContext
in the storage.static void
hook(Function<? super RequestContextStorage,? extends RequestContextStorage> function)
Customizes the currentRequestContextStorage
by applying the specifiedFunction
to it.void
pop(RequestContext current, RequestContext toRestore)
Pops the currentRequestContext
in the storage and pushes back the specifiedtoRestore
.<T extends RequestContext>
Tpush(RequestContext toPush)
Pushes the specifiedRequestContext
into the storage.static RequestContextStorage
threadLocal()
Returns the defaultRequestContextStorage
which stores theRequestContext
in the thread-local.default RequestContextStorage
unwrap()
Unwraps this object and returns the object being decorated.Methods inherited from interface com.linecorp.armeria.common.util.Unwrappable
as
-
Method Details
-
hook
static void hook(Function<? super RequestContextStorage,? extends RequestContextStorage> function)Customizes the currentRequestContextStorage
by applying the specifiedFunction
to it. This method is useful when you need to perform an additional operation when aRequestContext
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.
- All
-
threadLocal
Returns the defaultRequestContextStorage
which stores theRequestContext
in the thread-local. -
push
Pushes the specifiedRequestContext
into the storage.- Returns:
- the old
RequestContext
which was in the storage before the specifiedtoPush
is pushed.null
, if there was noRequestContext
.
-
pop
Pops the currentRequestContext
in the storage and pushes back the specifiedtoRestore
.toRestore
is theRequestContext
returned from when push(current) is called, so it can benull
.The specified
current
must be theRequestContext
in the storage. If it's not, it means thatRequestContext.push()
is not called usingtry-with-resources
block, so the previousRequestContext
is not popped properly. -
currentOrNull
-
unwrap
Description copied from interface:Unwrappable
Unwraps this object and returns the object being decorated. If thisUnwrappable
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 interfaceUnwrappable
-