Package com.linecorp.armeria.common
Interface RequestContextStorage
@UnstableApi public interface RequestContextStorage
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.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.
-
Method Details
-
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
-