public static final class Context.CancellableContext extends Context
Context.CancellableContext, Context.CancellationListener, Context.Key<T>, Context.Storage
Modifier and Type | Method and Description |
---|---|
Context |
attach()
Attach this context, thus enter a new scope within which this context is
Context.current() . |
boolean |
cancel(Throwable cause)
Cancel this context and optionally provide a cause (can be
null ) for the
cancellation. |
Throwable |
cancellationCause()
If a context
Context.isCancelled() then return the cause of the cancellation or
null if context was cancelled without a cause. |
void |
detach(Context toAttach)
Reverse an
attach() , restoring the previous context and exiting the current scope. |
void |
detachAndCancel(Context toAttach,
Throwable cause)
Cancel this context and detach it as the current context.
|
Deadline |
getDeadline()
A context may have an associated
Deadline at which it will be automatically cancelled. |
boolean |
isCancelled()
Is this context cancelled.
|
boolean |
isCurrent()
Deprecated.
This method violates some GRPC class encapsulation and should not be used.
If you must know whether a Context is the current context, check whether it is the same
object returned by
Context.current() . |
addListener, call, current, currentContextExecutor, fixedContextExecutor, fork, key, keyWithDefault, removeListener, run, withCancellation, withDeadline, withDeadlineAfter, withValue, withValues, withValues, withValues, wrap, wrap
public Context attach()
Context
Context.current()
. The
previously current context is returned. It is allowed to attach contexts where Context.isCancelled()
is true
.
Instead of using attach()
and Context.detach(Context)
most use-cases are better
served by using the Context.run(Runnable)
or Context.call(java.util.concurrent.Callable)
to
execute work immediately within a context's scope. If work needs to be done in other threads it
is recommended to use the 'wrap' methods or to use a propagating executor.
All calls to attach()
should have a corresponding Context.detach(Context)
within
the same method:
Context previous = someContext.attach();
try {
// Do work
} finally {
someContext.detach(previous);
}
public void detach(Context toAttach)
Context
attach()
, restoring the previous context and exiting the current scope.
This context should be the same context that was previously attached
. The
provided replacement should be what was returned by the same attach()
call. If
an attach()
and a detach()
meet above requirements, they match.
It is expected that between any pair of matching attach()
and detach()
, all
attach()
es and detach()
es are called in matching pairs. If this method finds
that this context is not current
, either you or some code in-between are not
detaching correctly, and a SEVERE message will be logged but the context to attach will still
be bound. Never use Context.current().detach()
, as this will
compromise this error-detecting mechanism.
@Deprecated public boolean isCurrent()
Context.current()
.public boolean cancel(Throwable cause)
null
) for the
cancellation. This will trigger notification of listeners.true
if this context cancelled the context and notified listeners,
false
if the context was already cancelled.public void detachAndCancel(Context toAttach, Throwable cause)
toAttach
- context to make current.cause
- of cancellation, can be null
.public boolean isCancelled()
Context
isCancelled
in class Context
public Throwable cancellationCause()
Context
Context.isCancelled()
then return the cause of the cancellation or
null
if context was cancelled without a cause. If the context is not yet cancelled
will always return null
.
The cancellation cause is provided for informational purposes only and implementations should generally assume that it has already been handled and logged properly.
cancellationCause
in class Context