Package com.linecorp.armeria.common
Class AbstractRequestContext
- java.lang.Object
-
- com.linecorp.armeria.common.AbstractRequestContext
-
- All Implemented Interfaces:
RequestContext
,AttributeMap
- Direct Known Subclasses:
NonWrappingRequestContext
,RequestContextWrapper
public abstract class AbstractRequestContext extends Object implements RequestContext
A skeletalRequestContext
implementation.
-
-
Constructor Summary
Constructors Constructor Description AbstractRequestContext()
-
Method Summary
Modifier and Type Method Description EventLoop
contextAwareEventLoop()
Returns anEventLoop
that will make sure thisRequestContext
is set as the current context before executing any callback.Executor
contextAwareExecutor()
Returns anExecutor
that will make sure thisRequestContext
is set as the current context before executing any callback.boolean
equals(Object obj)
Executor
executor()
int
hashCode()
boolean
isTimedOut()
Returns whether thisRequestContext
has been timed-out (e.g., when the corresponding request passes a deadline).ChannelFutureListener
makeContextAware(ChannelFutureListener listener)
Returns aChannelFutureListener
that makes sure the currentRequestContext
is set and then invokes the inputlistener
.<T> FutureListener<T>
makeContextAware(FutureListener<T> listener)
Returns aFutureListener
that makes sure the currentRequestContext
is set and then invokes the inputlistener
.<T extends Future<?>>
GenericFutureListener<T>makeContextAware(GenericFutureListener<T> listener)
Returns aGenericFutureListener
that makes sure the currentRequestContext
is set and then invokes the inputlistener
.Runnable
makeContextAware(Runnable runnable)
Returns aRunnable
that makes sure the currentRequestContext
is set and then invokes the inputrunnable
.<T> Callable<T>
makeContextAware(Callable<T> callable)
Returns aCallable
that makes sure the currentRequestContext
is set and then invokes the inputcallable
.<T> CompletableFuture<T>
makeContextAware(CompletableFuture<T> future)
Returns aCompletableFuture
that makes sure the currentCompletableFuture
is set and then invokes the inputfuture
.<T> CompletionStage<T>
makeContextAware(CompletionStage<T> stage)
Returns aCompletionStage
that makes sure the currentCompletionStage
is set and then invokes the inputstage
.Executor
makeContextAware(Executor executor)
Returns anExecutor
that will execute callbacks in the givenexecutor
, making sure to propagate the currentRequestContext
into the callback execution.ExecutorService
makeContextAware(ExecutorService executor)
Returns anExecutorService
that will execute callbacks in the givenexecutor
, making sure to propagate the currentRequestContext
into the callback execution.ScheduledExecutorService
makeContextAware(ScheduledExecutorService executor)
Returns aScheduledExecutorService
that will execute callbacks in the givenexecutor
, making sure to propagate the currentRequestContext
into the callback execution.<T,U>
BiConsumer<T,U>makeContextAware(BiConsumer<T,U> action)
Returns aBiConsumer
that makes sure the currentRequestContext
is set and then invokes the inputaction
.<T,U,V>
BiFunction<T,U,V>makeContextAware(BiFunction<T,U,V> function)
Returns aBiFunction
that makes sure the currentRequestContext
is set and then invokes the inputfunction
.<T> Consumer<T>
makeContextAware(Consumer<T> action)
Returns aConsumer
that makes sure the currentRequestContext
is set and then invokes the inputaction
.<T,R>
Function<T,R>makeContextAware(Function<T,R> function)
Returns aFunction
that makes sure the currentRequestContext
is set and then invokes the inputfunction
.void
onEnter(Runnable callback)
Registerscallback
to be run when re-entering thisRequestContext
, usually when using theRequestContext.makeContextAware(java.util.concurrent.Executor)
family of methods.void
onExit(Runnable callback)
Registerscallback
to be run when re-exiting thisRequestContext
, usually when using theRequestContext.makeContextAware(java.util.concurrent.Executor)
family of methods.SafeCloseable
push()
Pushes the specified context to the thread-local stack.SafeCloseable
push(boolean runCallbacks)
Pushes the specified context to the thread-local stack.SafeCloseable
pushIfAbsent()
Pushes this context to the thread-local stack if there is no current context.void
rejectPromise(Promise<?> promise, Throwable cause)
Rejects the specifiedpromise
with the specifiedcause
.void
resolvePromise(Promise<?> promise, Object result)
Resolves the specifiedpromise
with the specifiedresult
so that thepromise
is marked as 'done'.void
setTimedOut()
Deprecated.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty.util.AttributeMap
attr, hasAttr
-
Methods inherited from interface com.linecorp.armeria.common.RequestContext
alloc, attrs, decodedPath, eventLoop, id, invokeOnChildCallbacks, invokeOnEnterCallbacks, invokeOnExitCallbacks, localAddress, log, logBuilder, meterRegistry, method, newDerivedContext, onChild, onEnter, onExit, path, query, remoteAddress, request, rpcRequest, sessionProtocol, sslSession, updateRequest, updateRpcRequest
-
-
-
-
Method Detail
-
executor
public final Executor executor()
Description copied from interface:RequestContext
- Specified by:
executor
in interfaceRequestContext
-
contextAwareExecutor
public final Executor contextAwareExecutor()
Description copied from interface:RequestContext
Returns anExecutor
that will make sure thisRequestContext
is set as the current context before executing any callback. This should almost always be used for executing asynchronous callbacks in service code to make sure features that require theRequestContext
work properly. Most asynchronous libraries likeCompletableFuture
provide methods that accept anExecutor
to run callbacks on.- Specified by:
contextAwareExecutor
in interfaceRequestContext
-
contextAwareEventLoop
public final EventLoop contextAwareEventLoop()
Description copied from interface:RequestContext
Returns anEventLoop
that will make sure thisRequestContext
is set as the current context before executing any callback.- Specified by:
contextAwareEventLoop
in interfaceRequestContext
-
push
public final SafeCloseable push()
Description copied from interface:RequestContext
Pushes the specified context to the thread-local stack. To pop the context from the stack, callSafeCloseable.close()
, which can be done using atry-with-resources
block:try (SafeCloseable ignored = ctx.push()) { ... }
The callbacks added by
RequestContext.onEnter(Consumer)
andRequestContext.onExit(Consumer)
will be invoked when the context is pushed to and removed from the thread-local stack respectively.NOTE: In case of re-entrance, the callbacks will never run.
- Specified by:
push
in interfaceRequestContext
-
push
public final SafeCloseable push(boolean runCallbacks)
Description copied from interface:RequestContext
Pushes the specified context to the thread-local stack. To pop the context from the stack, callSafeCloseable.close()
, which can be done using atry-with-resources
block:try (PushHandle ignored = ctx.push(true)) { ... }
NOTE: This method is only useful when it is undesirable to invoke the callbacks, such as replacing the current context with another. Prefer
RequestContext.push()
otherwise.- Specified by:
push
in interfaceRequestContext
- Parameters:
runCallbacks
- iftrue
, the callbacks added byRequestContext.onEnter(Consumer)
andRequestContext.onExit(Consumer)
will be invoked when the context is pushed to and removed from the thread-local stack respectively. Iffalse
, no callbacks will be executed. NOTE: In case of re-entrance, the callbacks will never run.
-
pushIfAbsent
public final SafeCloseable pushIfAbsent()
Description copied from interface:RequestContext
Pushes this context to the thread-local stack if there is no current context. If there is and it is not same with this context (i.e. not reentrance), this method will throw anIllegalStateException
. To pop the context from the stack, callSafeCloseable.close()
, which can be done using atry-with-resources
block.- Specified by:
pushIfAbsent
in interfaceRequestContext
-
makeContextAware
public final Executor makeContextAware(Executor executor)
Description copied from interface:RequestContext
Returns anExecutor
that will execute callbacks in the givenexecutor
, making sure to propagate the currentRequestContext
into the callback execution. It is generally preferred to useRequestContext.contextAwareEventLoop()
to ensure the callback stays on the same thread as well.- Specified by:
makeContextAware
in interfaceRequestContext
-
makeContextAware
public final ExecutorService makeContextAware(ExecutorService executor)
Description copied from interface:RequestContext
Returns anExecutorService
that will execute callbacks in the givenexecutor
, making sure to propagate the currentRequestContext
into the callback execution.- Specified by:
makeContextAware
in interfaceRequestContext
-
makeContextAware
public final ScheduledExecutorService makeContextAware(ScheduledExecutorService executor)
Description copied from interface:RequestContext
Returns aScheduledExecutorService
that will execute callbacks in the givenexecutor
, making sure to propagate the currentRequestContext
into the callback execution.- Specified by:
makeContextAware
in interfaceRequestContext
-
makeContextAware
public final <T> Callable<T> makeContextAware(Callable<T> callable)
Description copied from interface:RequestContext
Returns aCallable
that makes sure the currentRequestContext
is set and then invokes the inputcallable
.- Specified by:
makeContextAware
in interfaceRequestContext
-
makeContextAware
public final Runnable makeContextAware(Runnable runnable)
Description copied from interface:RequestContext
Returns aRunnable
that makes sure the currentRequestContext
is set and then invokes the inputrunnable
.- Specified by:
makeContextAware
in interfaceRequestContext
-
makeContextAware
public final <T,R> Function<T,R> makeContextAware(Function<T,R> function)
Description copied from interface:RequestContext
Returns aFunction
that makes sure the currentRequestContext
is set and then invokes the inputfunction
.- Specified by:
makeContextAware
in interfaceRequestContext
-
makeContextAware
public final <T,U,V> BiFunction<T,U,V> makeContextAware(BiFunction<T,U,V> function)
Description copied from interface:RequestContext
Returns aBiFunction
that makes sure the currentRequestContext
is set and then invokes the inputfunction
.- Specified by:
makeContextAware
in interfaceRequestContext
-
makeContextAware
public final <T> Consumer<T> makeContextAware(Consumer<T> action)
Description copied from interface:RequestContext
Returns aConsumer
that makes sure the currentRequestContext
is set and then invokes the inputaction
.- Specified by:
makeContextAware
in interfaceRequestContext
-
makeContextAware
public final <T,U> BiConsumer<T,U> makeContextAware(BiConsumer<T,U> action)
Description copied from interface:RequestContext
Returns aBiConsumer
that makes sure the currentRequestContext
is set and then invokes the inputaction
.- Specified by:
makeContextAware
in interfaceRequestContext
-
makeContextAware
public final <T> FutureListener<T> makeContextAware(FutureListener<T> listener)
Description copied from interface:RequestContext
Returns aFutureListener
that makes sure the currentRequestContext
is set and then invokes the inputlistener
.- Specified by:
makeContextAware
in interfaceRequestContext
-
makeContextAware
public final ChannelFutureListener makeContextAware(ChannelFutureListener listener)
Description copied from interface:RequestContext
Returns aChannelFutureListener
that makes sure the currentRequestContext
is set and then invokes the inputlistener
.- Specified by:
makeContextAware
in interfaceRequestContext
-
makeContextAware
public final <T extends Future<?>> GenericFutureListener<T> makeContextAware(GenericFutureListener<T> listener)
Description copied from interface:RequestContext
Returns aGenericFutureListener
that makes sure the currentRequestContext
is set and then invokes the inputlistener
. Unlike other versions ofmakeContextAware
, this one will invoke the listener with the future's result even if the context has already been timed out.- Specified by:
makeContextAware
in interfaceRequestContext
-
makeContextAware
public final <T> CompletionStage<T> makeContextAware(CompletionStage<T> stage)
Description copied from interface:RequestContext
Returns aCompletionStage
that makes sure the currentCompletionStage
is set and then invokes the inputstage
.- Specified by:
makeContextAware
in interfaceRequestContext
-
makeContextAware
public final <T> CompletableFuture<T> makeContextAware(CompletableFuture<T> future)
Description copied from interface:RequestContext
Returns aCompletableFuture
that makes sure the currentCompletableFuture
is set and then invokes the inputfuture
.- Specified by:
makeContextAware
in interfaceRequestContext
-
isTimedOut
public boolean isTimedOut()
Description copied from interface:RequestContext
Returns whether thisRequestContext
has been timed-out (e.g., when the corresponding request passes a deadline).- Specified by:
isTimedOut
in interfaceRequestContext
-
setTimedOut
@Deprecated public void setTimedOut()
Deprecated.Marks thisRequestContext
as having been timed out. Any callbacks created withmakeContextAware
that are run after this will be failed withCancellationException
.
-
onEnter
public final void onEnter(Runnable callback)
Description copied from interface:RequestContext
Registerscallback
to be run when re-entering thisRequestContext
, usually when using theRequestContext.makeContextAware(java.util.concurrent.Executor)
family of methods. Any thread-local state associated with this context should be restored by this callback.- Specified by:
onEnter
in interfaceRequestContext
-
onExit
public final void onExit(Runnable callback)
Description copied from interface:RequestContext
Registerscallback
to be run when re-exiting thisRequestContext
, usually when using theRequestContext.makeContextAware(java.util.concurrent.Executor)
family of methods. Any thread-local state associated with this context should be reset by this callback.- Specified by:
onExit
in interfaceRequestContext
-
resolvePromise
public final void resolvePromise(Promise<?> promise, Object result)
Description copied from interface:RequestContext
Resolves the specifiedpromise
with the specifiedresult
so that thepromise
is marked as 'done'. Ifpromise
is done already, this method does the following:- Log a warning about the failure, and
- Release
result
if it is a reference-counted object, such asByteBuf
andFullHttpResponse
.
Promise
can be done already even if you did not call this method in the following cases:- Invocation timeout - The invocation associated with the
Promise
has been timed out. - User error - A service implementation called any of the following methods more than once:
- Specified by:
resolvePromise
in interfaceRequestContext
-
rejectPromise
public final void rejectPromise(Promise<?> promise, Throwable cause)
Description copied from interface:RequestContext
Rejects the specifiedpromise
with the specifiedcause
. Ifpromise
is done already, this method logs a warning about the failure. Note that aPromise
can be done already even if you did not call this method in the following cases:- Invocation timeout - The invocation associated with the
Promise
has been timed out. - User error - A service implementation called any of the following methods more than once:
- Specified by:
rejectPromise
in interfaceRequestContext
- Invocation timeout - The invocation associated with the
-
-