public interface RequestContext
extends io.netty.util.AttributeMap
Request, its Response and related utilities.
A server-side Request has a ServiceRequestContext and
a client-side Request has a ClientRequestContext.| Modifier and Type | Method and Description |
|---|---|
default io.netty.buffer.ByteBufAllocator |
alloc()
Returns the
ByteBufAllocator for this RequestContext. |
Iterator<io.netty.util.Attribute<?>> |
attrs()
Returns all
Attributes set in this context. |
default io.netty.channel.EventLoop |
contextAwareEventLoop()
Returns an
EventLoop that will make sure this RequestContext is set as the current
context before executing any callback. |
default Executor |
contextAwareExecutor()
Returns an
Executor that will make sure this RequestContext is set as the current
context before executing any callback. |
static <T extends RequestContext> |
current()
Returns the context of the
Request that is being handled in the current thread. |
static <T extends RequestContext> |
currentOrNull()
Returns the context of the
Request that is being handled in the current thread. |
String |
decodedPath()
Returns the absolute path part of the current
Request URI, excluding the query part,
decoded in UTF-8. |
io.netty.channel.EventLoop |
eventLoop()
Returns the
EventLoop that is handling the current Request. |
default Executor |
executor()
|
RequestId |
id()
|
void |
invokeOnChildCallbacks(RequestContext newCtx)
Invokes all
onChild(BiConsumer) callbacks. |
void |
invokeOnEnterCallbacks()
Invokes all
onEnter(Consumer) callbacks. |
void |
invokeOnExitCallbacks()
Invokes all
onExit(Consumer) callbacks. |
boolean |
isTimedOut()
Deprecated.
|
<A extends SocketAddress> |
localAddress()
Returns the local address of this request, or
null if the connection is not established yet. |
RequestLog |
log()
Returns the
RequestLog that contains the information about the current Request. |
RequestLogBuilder |
logBuilder()
Returns the
RequestLogBuilder that collects the information about the current Request. |
<T,U> BiConsumer<T,U> |
makeContextAware(BiConsumer<T,U> action)
Returns a
BiConsumer that makes sure the current RequestContext is set and then invokes
the input action. |
<T,U,V> BiFunction<T,U,V> |
makeContextAware(BiFunction<T,U,V> function)
Returns a
BiFunction that makes sure the current RequestContext is set and then invokes
the input function. |
<T> Callable<T> |
makeContextAware(Callable<T> callable)
Returns a
Callable that makes sure the current RequestContext is set and then invokes
the input callable. |
io.netty.channel.ChannelFutureListener |
makeContextAware(io.netty.channel.ChannelFutureListener listener)
Deprecated.
Use
CompletableFuture instead. |
default <T> CompletableFuture<T> |
makeContextAware(CompletableFuture<T> future)
Returns a
CompletableFuture that makes sure the current CompletableFuture is set and
then invokes the input future. |
<T> CompletionStage<T> |
makeContextAware(CompletionStage<T> stage)
Returns a
CompletionStage that makes sure the current CompletionStage is set and
then invokes the input stage. |
<T> Consumer<T> |
makeContextAware(Consumer<T> action)
Returns a
Consumer that makes sure the current RequestContext is set and then invokes
the input action. |
default Executor |
makeContextAware(Executor executor)
Returns an
Executor that will execute callbacks in the given executor, making sure to
propagate the current RequestContext into the callback execution. |
default ExecutorService |
makeContextAware(ExecutorService executor)
Returns an
ExecutorService that will execute callbacks in the given executor, making
sure to propagate the current RequestContext into the callback execution. |
<T,R> Function<T,R> |
makeContextAware(Function<T,R> function)
Returns a
Function that makes sure the current RequestContext is set and then invokes
the input function. |
<T> io.netty.util.concurrent.FutureListener<T> |
makeContextAware(io.netty.util.concurrent.FutureListener<T> listener)
Deprecated.
Use
CompletableFuture instead. |
<T extends io.netty.util.concurrent.Future<?>> |
makeContextAware(io.netty.util.concurrent.GenericFutureListener<T> listener)
Deprecated.
Use
CompletableFuture instead. |
Runnable |
makeContextAware(Runnable runnable)
Returns a
Runnable that makes sure the current RequestContext is set and then invokes
the input runnable. |
default ScheduledExecutorService |
makeContextAware(ScheduledExecutorService executor)
Returns a
ScheduledExecutorService that will execute callbacks in the given executor,
making sure to propagate the current RequestContext into the callback execution. |
static <T> T |
mapCurrent(Function<? super RequestContext,T> mapper,
Supplier<T> defaultValueSupplier)
Maps the context of the
Request that is being handled in the current thread. |
MeterRegistry |
meterRegistry()
Returns the
MeterRegistry that collects various stats. |
HttpMethod |
method()
Returns the HTTP method of the current
Request. |
RequestContext |
newDerivedContext(RequestId id,
HttpRequest req,
RpcRequest rpcReq)
Creates a new
RequestContext whose properties and Attributes are copied from this
RequestContext, except having a different pair of HttpRequest and RpcRequest
and its own RequestLog. |
void |
onChild(BiConsumer<? super RequestContext,? super RequestContext> callback)
Registers
callback to be run when this context is replaced by a child context. |
void |
onEnter(Consumer<? super RequestContext> callback)
Registers
callback to be run when re-entering this RequestContext, usually when using
the makeContextAware(java.util.concurrent.Executor) family of methods. |
default void |
onEnter(Runnable callback)
Deprecated.
Use
onEnter(Consumer) instead. |
void |
onExit(Consumer<? super RequestContext> callback)
Registers
callback to be run when re-exiting this RequestContext, usually when using
the makeContextAware(java.util.concurrent.Executor) family of methods. |
default void |
onExit(Runnable callback)
Deprecated.
Use
onExit(Consumer) instead. |
String |
path()
|
default SafeCloseable |
push()
Pushes the specified context to the thread-local stack.
|
default SafeCloseable |
push(boolean runCallbacks)
Pushes the specified context to the thread-local stack.
|
static SafeCloseable |
push(RequestContext ctx)
Deprecated.
Use
push(). |
static SafeCloseable |
push(RequestContext ctx,
boolean runCallbacks)
Deprecated.
Use
push(boolean). |
default SafeCloseable |
pushIfAbsent()
Pushes this context to the thread-local stack if there is no current context.
|
String |
query()
Returns the query part of the current
Request URI, without the leading '?' |
default void |
rejectPromise(io.netty.util.concurrent.Promise<?> promise,
Throwable cause)
Deprecated.
Use
CompletableFuture instead. |
<A extends SocketAddress> |
remoteAddress()
Returns the remote address of this request, or
null if the connection is not established yet. |
HttpRequest |
request()
Returns the
HttpRequest associated with this context, or null if there's no
HttpRequest associated with this context yet. |
default void |
resolvePromise(io.netty.util.concurrent.Promise<?> promise,
Object result)
Deprecated.
Use
CompletableFuture instead. |
RpcRequest |
rpcRequest()
Returns the
RpcRequest associated with this context, or null if there's no
RpcRequest associated with this context. |
SessionProtocol |
sessionProtocol()
Returns the
SessionProtocol of the current Request. |
SSLSession |
sslSession()
The
SSLSession for this request if the connection is made over TLS, or null if
the connection is not established yet or the connection is not a TLS connection. |
void |
updateRequest(HttpRequest req)
Replaces the
HttpRequest associated with this context with the specified one. |
void |
updateRpcRequest(RpcRequest rpcReq)
Replaces the
RpcRequest associated with this context with the specified one. |
static <T extends RequestContext> T current()
Request that is being handled in the current thread.IllegalStateException - if the context is unavailable in the current thread@Nullable static <T extends RequestContext> T currentOrNull()
Request that is being handled in the current thread.RequestContext available in the current thread, or null if unavailable.@Nullable static <T> T mapCurrent(Function<? super RequestContext,T> mapper, @Nullable Supplier<T> defaultValueSupplier)
Request that is being handled in the current thread.mapper - the Function that maps the RequestContextdefaultValueSupplier - the Supplier that provides the value when the context is unavailable
in the current thread. If null, the null will be returned
when the context is unavailable in the current thread.@Nullable HttpRequest request()
HttpRequest associated with this context, or null if there's no
HttpRequest associated with this context yet.@Nullable RpcRequest rpcRequest()
RpcRequest associated with this context, or null if there's no
RpcRequest associated with this context.void updateRequest(HttpRequest req)
HttpRequest associated with this context with the specified one.
This method is useful to a decorator that manipulates HTTP request headers.
Note that it is a bad idea to change the values of the pseudo headers (":method",
":path", ":scheme" and ":authority") when replacing an HttpRequest,
because the properties of this context, such as path(), are unaffected by such an attempt.
void updateRpcRequest(RpcRequest rpcReq)
RpcRequest associated with this context with the specified one.
This method is useful to a decorator that manipulates an RPC call.SessionProtocol sessionProtocol()
SessionProtocol of the current Request.@Nullable <A extends SocketAddress> A remoteAddress()
null if the connection is not established yet.@Nullable <A extends SocketAddress> A localAddress()
null if the connection is not established yet.@Nullable SSLSession sslSession()
SSLSession for this request if the connection is made over TLS, or null if
the connection is not established yet or the connection is not a TLS connection.RequestId id()
HttpMethod method()
Request.String path()
String decodedPath()
Request URI, excluding the query part,
decoded in UTF-8.RequestLog log()
RequestLog that contains the information about the current Request.RequestLogBuilder logBuilder()
RequestLogBuilder that collects the information about the current Request.MeterRegistry meterRegistry()
MeterRegistry that collects various stats.Iterator<io.netty.util.Attribute<?>> attrs()
Attributes set in this context.default Executor executor()
io.netty.channel.EventLoop eventLoop()
EventLoop that is handling the current Request.default io.netty.buffer.ByteBufAllocator alloc()
ByteBufAllocator for this RequestContext. Any buffers created by this
ByteBufAllocator must be
reference-counted. If you don't know
what this means, you should probably use byte[] or ByteBuffer directly instead
of calling this method.default Executor contextAwareExecutor()
Executor that will make sure this RequestContext 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 the RequestContext work properly.
Most asynchronous libraries like CompletableFuture provide methods that accept an
Executor to run callbacks on.default io.netty.channel.EventLoop contextAwareEventLoop()
EventLoop that will make sure this RequestContext is set as the current
context before executing any callback.@Deprecated static SafeCloseable push(RequestContext ctx)
push().SafeCloseable.close(), which can be done using a try-with-resources block.@Deprecated static SafeCloseable push(RequestContext ctx, boolean runCallbacks)
push(boolean).SafeCloseable.close(), which can be done using a try-with-resources block.default SafeCloseable push()
SafeCloseable.close(), which can be done using a try-with-resources block:
try (SafeCloseable ignored = ctx.push()) {
...
}
The callbacks added by onEnter(Consumer) and 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.
default SafeCloseable push(boolean runCallbacks)
SafeCloseable.close(), which can be done using a try-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 push() otherwise.
runCallbacks - if true, the callbacks added by onEnter(Consumer) and
onExit(Consumer) will be invoked when the context is pushed to and
removed from the thread-local stack respectively.
If false, no callbacks will be executed.
NOTE: In case of re-entrance, the callbacks will never run.default SafeCloseable pushIfAbsent()
IllegalStateException.
To pop the context from the stack, call SafeCloseable.close(),
which can be done using a try-with-resources block.default Executor makeContextAware(Executor executor)
Executor that will execute callbacks in the given executor, making sure to
propagate the current RequestContext into the callback execution. It is generally preferred to
use contextAwareEventLoop() to ensure the callback stays on the same thread as well.default ExecutorService makeContextAware(ExecutorService executor)
ExecutorService that will execute callbacks in the given executor, making
sure to propagate the current RequestContext into the callback execution.default ScheduledExecutorService makeContextAware(ScheduledExecutorService executor)
ScheduledExecutorService that will execute callbacks in the given executor,
making sure to propagate the current RequestContext into the callback execution.<T> Callable<T> makeContextAware(Callable<T> callable)
Callable that makes sure the current RequestContext is set and then invokes
the input callable.Runnable makeContextAware(Runnable runnable)
Runnable that makes sure the current RequestContext is set and then invokes
the input runnable.<T,R> Function<T,R> makeContextAware(Function<T,R> function)
Function that makes sure the current RequestContext is set and then invokes
the input function.<T,U,V> BiFunction<T,U,V> makeContextAware(BiFunction<T,U,V> function)
BiFunction that makes sure the current RequestContext is set and then invokes
the input function.<T> Consumer<T> makeContextAware(Consumer<T> action)
Consumer that makes sure the current RequestContext is set and then invokes
the input action.<T,U> BiConsumer<T,U> makeContextAware(BiConsumer<T,U> action)
BiConsumer that makes sure the current RequestContext is set and then invokes
the input action.@Deprecated <T> io.netty.util.concurrent.FutureListener<T> makeContextAware(io.netty.util.concurrent.FutureListener<T> listener)
CompletableFuture instead.FutureListener that makes sure the current RequestContext is set and then
invokes the input listener.@Deprecated io.netty.channel.ChannelFutureListener makeContextAware(io.netty.channel.ChannelFutureListener listener)
CompletableFuture instead.ChannelFutureListener that makes sure the current RequestContext is set and
then invokes the input listener.@Deprecated <T extends io.netty.util.concurrent.Future<?>> io.netty.util.concurrent.GenericFutureListener<T> makeContextAware(io.netty.util.concurrent.GenericFutureListener<T> listener)
CompletableFuture instead.GenericFutureListener that makes sure the current RequestContext is set and
then invokes the input listener. Unlike other versions of makeContextAware, this one will
invoke the listener with the future's result even if the context has already been timed out.<T> CompletionStage<T> makeContextAware(CompletionStage<T> stage)
CompletionStage that makes sure the current CompletionStage is set and
then invokes the input stage.default <T> CompletableFuture<T> makeContextAware(CompletableFuture<T> future)
CompletableFuture that makes sure the current CompletableFuture is set and
then invokes the input future.@Deprecated boolean isTimedOut()
ServiceRequestContext.isTimedOut().RequestContext has been timed-out (e.g., when the corresponding request
passes a deadline).void onEnter(Consumer<? super RequestContext> callback)
callback to be run when re-entering this RequestContext, usually when using
the makeContextAware(java.util.concurrent.Executor) family of methods. Any thread-local state associated with this context
should be restored by this callback.callback - a Consumer whose argument is this context@Deprecated default void onEnter(Runnable callback)
onEnter(Consumer) instead.callback to be run when re-entering this RequestContext, usually when using
the makeContextAware(java.util.concurrent.Executor) family of methods. Any thread-local state associated with this context
should be restored by this callback.void onExit(Consumer<? super RequestContext> callback)
callback to be run when re-exiting this RequestContext, usually when using
the makeContextAware(java.util.concurrent.Executor) family of methods. Any thread-local state associated with this context
should be reset by this callback.callback - a Consumer whose argument is this context@Deprecated default void onExit(Runnable callback)
onExit(Consumer) instead.callback to be run when re-exiting this RequestContext, usually when using
the makeContextAware(java.util.concurrent.Executor) family of methods. Any thread-local state associated with this context
should be reset by this callback.void onChild(BiConsumer<? super RequestContext,? super RequestContext> callback)
callback to be run when this context is replaced by a child context.
You could use this method to inherit an attribute of this context to the child contexts or
register a callback to the child contexts that may be created later:
ctx.onChild((curCtx, newCtx) -> {
assert ctx == curCtx && curCtx != newCtx;
// Inherit the value of the 'MY_ATTR' attribute to the child context.
newCtx.attr(MY_ATTR).set(curCtx.attr(MY_ATTR).get());
// Add a callback to the child context.
newCtx.onExit(() -> { ... });
});
callback - a BiConsumer whose first argument is this context and
whose second argument is the new context that replaces this contextvoid invokeOnEnterCallbacks()
onEnter(Consumer) callbacks. It is discouraged to use this method directly.
Use makeContextAware(Runnable) or push(boolean) instead so that the callbacks are
invoked automatically.void invokeOnExitCallbacks()
onExit(Consumer) callbacks. It is discouraged to use this method directly.
Use makeContextAware(Runnable) or push(boolean) instead so that the callbacks are
invoked automatically.void invokeOnChildCallbacks(RequestContext newCtx)
onChild(BiConsumer) callbacks. It is discouraged to use this method directly.
Use makeContextAware(Runnable) or push(boolean) instead so that the callbacks are
invoked automatically.@Deprecated default void resolvePromise(io.netty.util.concurrent.Promise<?> promise, Object result)
CompletableFuture instead.promise with the specified result so that the promise is
marked as 'done'. If promise is done already, this method does the following:
result if it is a reference-counted object,
such as ByteBuf and FullHttpResponse.Promise can be done already even if you did not call this method in the following
cases:
Promise has been timed out.resolvePromise(Promise, Object)rejectPromise(Promise, Throwable)Promise.setSuccess(Object)Promise.setFailure(Throwable)Future.cancel(boolean)@Deprecated default void rejectPromise(io.netty.util.concurrent.Promise<?> promise, Throwable cause)
CompletableFuture instead.promise with the specified cause. If promise is done
already, this method logs a warning about the failure. Note that a Promise can be done already
even if you did not call this method in the following cases:
Promise has been timed out.resolvePromise(Promise, Object)rejectPromise(Promise, Throwable)Promise.setSuccess(Object)Promise.setFailure(Throwable)Future.cancel(boolean)RequestContext newDerivedContext(RequestId id, @Nullable HttpRequest req, @Nullable RpcRequest rpcReq)
RequestContext whose properties and Attributes are copied from this
RequestContext, except having a different pair of HttpRequest and RpcRequest
and its own RequestLog.Copyright © 2020 LeanCloud. All rights reserved.