Class Context
- java.lang.Object
-
- io.grpc.Context
-
- Direct Known Subclasses:
Context.CancellableContext
public class Context extends Object
A context propagation mechanism which can carry scoped-values across API boundaries and between threads. Examples of state propagated via context include:- Security principals and credentials.
- Local and distributed tracing information.
A Context object can be
attachedto theContext.Storage, which effectively forms a scope for the context. The scope is bound to the current thread. Within a scope, its Context is accessible even across API boundaries, throughcurrent(). The scope is later exited bydetachingthe Context.Context objects are immutable and inherit state from their parent. To add or overwrite the current state a new context object must be created and then attached, replacing the previously bound context. For example:
Context withCredential = Context.current().withValue(CRED_KEY, cred); withCredential.run(new Runnable() { public void run() { readUserRecords(userId, CRED_KEY.get()); } });Contexts are also used to represent a scoped unit of work. When the unit of work is done the context must be cancelled. This cancellation will cascade to all descendant contexts. You can add a
Context.CancellationListenerto a context to be notified when it or one of its ancestors has been cancelled. Cancellation does not release the state stored by a context and it's perfectly valid toattach()an already cancelled context to make it current. To cancel a context (and its descendants) you first create aContext.CancellableContextand when you need to signal cancellation callContext.CancellableContext.cancel(java.lang.Throwable)orContext.CancellableContext.detachAndCancel(io.grpc.Context, java.lang.Throwable).Contexts can also be created with a timeout relative to the system nano clock which will cause it to automatically cancel at the desired time.
Notes and cautions on use:
- Every
attach()should have adetach()in the same method. And every CancellableContext should be cancelled at some point. Breaking these rules may lead to memory leaks. - While Context objects are immutable they do not place such a restriction on the state they store.
- Context is not intended for passing optional parameters to an API and developers should take care to avoid excessive dependence on context when designing an API.
- Do not mock this class. Use
ROOTfor a non-null instance.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classContext.CancellableContextA context which inherits cancellation from its parent but which can also be independently cancelled and which will propagate cancellation to its descendants.static interfaceContext.CancellationListenerA listener notified on context cancellation.static classContext.Key<T>Key for indexing values stored in a context.static classContext.StorageDefines the mechanisms for attaching and detaching the "current" context.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddListener(Context.CancellationListener cancellationListener, Executor executor)Add a listener that will be notified when the context becomes cancelled.Contextattach()Attach this context, thus enter a new scope within which this context iscurrent().<V> Vcall(Callable<V> c)ThrowablecancellationCause()If a contextisCancelled()then return the cause of the cancellation ornullif context was cancelled without a cause.static Contextcurrent()Return the context associated with the current scope, will never returnnull.static ExecutorcurrentContextExecutor(Executor e)Create an executor that propagates thecurrent()context whenExecutor.execute(java.lang.Runnable)is called as thecurrent()context of theRunnablescheduled.voiddetach(Context toAttach)Reverse anattach(), restoring the previous context and exiting the current scope.ExecutorfixedContextExecutor(Executor e)Contextfork()Create a new context which propagates the values of this context but does not cascade its cancellation.DeadlinegetDeadline()A context may have an associatedDeadlineat which it will be automatically cancelled.booleanisCancelled()Is this context cancelled.static <T> Context.Key<T>key(String debugString)Create aContext.Keywith the given debug name.static <T> Context.Key<T>keyWithDefault(String debugString, T defaultValue)Create aContext.Keywith the given debug name and default value.voidremoveListener(Context.CancellationListener cancellationListener)Remove aContext.CancellationListener.voidrun(Runnable r)Context.CancellableContextwithCancellation()Create a new context which is independently cancellable and also cascades cancellation from its parent.Context.CancellableContextwithDeadline(Deadline newDeadline, ScheduledExecutorService scheduler)Create a new context which will cancel itself at the givenDeadline.Context.CancellableContextwithDeadlineAfter(long duration, TimeUnit unit, ScheduledExecutorService scheduler)Create a new context which will cancel itself after the givendurationfrom now.<V> ContextwithValue(Context.Key<V> k1, V v1)Create a new context with the given key value set.<V1,V2>
ContextwithValues(Context.Key<V1> k1, V1 v1, Context.Key<V2> k2, V2 v2)Create a new context with the given key value set.<V1,V2,V3>
ContextwithValues(Context.Key<V1> k1, V1 v1, Context.Key<V2> k2, V2 v2, Context.Key<V3> k3, V3 v3)Create a new context with the given key value set.<V1,V2,V3,V4>
ContextwithValues(Context.Key<V1> k1, V1 v1, Context.Key<V2> k2, V2 v2, Context.Key<V3> k3, V3 v3, Context.Key<V4> k4, V4 v4)Create a new context with the given key value set.Runnablewrap(Runnable r)<C> Callable<C>wrap(Callable<C> c)
-
-
-
Field Detail
-
ROOT
public static final Context ROOT
The logical root context which is the ultimate ancestor of all contexts. This context is not cancellable and so will not cascade cancellation or retain listeners.Never assume this is the default context for new threads, because
Context.Storagemay define a default context that is different from ROOT.
-
-
Method Detail
-
key
public static <T> Context.Key<T> key(String debugString)
Create aContext.Keywith the given debug name.- Parameters:
debugString- a name intended for debugging purposes and does not impact behavior. Multiple different keys may have the same debugString. The value should be not null.
-
keyWithDefault
public static <T> Context.Key<T> keyWithDefault(String debugString, T defaultValue)
Create aContext.Keywith the given debug name and default value.- Parameters:
debugString- a name intended for debugging purposes and does not impact behavior. Multiple different keys may have the same debugString. The value should be not null.
-
current
public static Context current()
Return the context associated with the current scope, will never returnnull.Will never return
Context.CancellableContexteven if one is attached, instead aContextis returned with the same properties and lifetime. This is to avoid code stealing the ability to cancel arbitrarily.
-
withCancellation
public Context.CancellableContext withCancellation()
Create a new context which is independently cancellable and also cascades cancellation from its parent. Callers must ensure that eitherContext.CancellableContext.cancel(Throwable)orContext.CancellableContext.detachAndCancel(Context, Throwable)are called at a later point, in order to allow this context to be garbage collected.Sample usage:
Context.CancellableContext withCancellation = Context.current().withCancellation(); try { withCancellation.run(new Runnable() { public void run() { Context current = Context.current(); while (!current.isCancelled()) { keepWorking(); } } }); } finally { withCancellation.cancel(null); }
-
withDeadlineAfter
public Context.CancellableContext withDeadlineAfter(long duration, TimeUnit unit, ScheduledExecutorService scheduler)
Create a new context which will cancel itself after the givendurationfrom now. The returned context will cascade cancellation of its parent. Callers may explicitly cancel the returned context prior to the deadline just as forwithCancellation(). If the unit of work completes before the deadline, the context should be explicitly cancelled to allow it to be garbage collected.Sample usage:
Context.CancellableContext withDeadline = Context.current() .withDeadlineAfter(5, TimeUnit.SECONDS, scheduler); try { withDeadline.run(new Runnable() { public void run() { Context current = Context.current(); while (!current.isCancelled()) { keepWorking(); } } }); } finally { withDeadline.cancel(null); }
-
withDeadline
public Context.CancellableContext withDeadline(Deadline newDeadline, ScheduledExecutorService scheduler)
Create a new context which will cancel itself at the givenDeadline. The returned context will cascade cancellation of its parent. Callers may explicitly cancel the returned context prior to the deadline just as forwithCancellation(). If the unit of work completes before the deadline, the context should be explicitly cancelled to allow it to be garbage collected.Sample usage:
Context.CancellableContext withDeadline = Context.current() .withDeadline(someReceivedDeadline, scheduler); try { withDeadline.run(new Runnable() { public void run() { Context current = Context.current(); while (!current.isCancelled() && moreWorkToDo()) { keepWorking(); } } }); } finally { withDeadline.cancel(null); }
-
withValue
public <V> Context withValue(Context.Key<V> k1, V v1)
Create a new context with the given key value set. The new context will cascade cancellation from its parent.Context withCredential = Context.current().withValue(CRED_KEY, cred); withCredential.run(new Runnable() { public void run() { readUserRecords(userId, CRED_KEY.get()); } });Note that multiple calls to
withValue(io.grpc.Context.Key<V>, V)can be chained together. That is,context.withValues(K1, V1, K2, V2); // is the same as context.withValue(K1, V1).withValue(K2, V2);
Nonetheless,
Contextshould not be treated like a general purpose map with a large number of keys and values — combine multiple related items together into a single key instead of separating them. But if the items are unrelated, have separate keys for them.
-
withValues
public <V1,V2> Context withValues(Context.Key<V1> k1, V1 v1, Context.Key<V2> k2, V2 v2)
Create a new context with the given key value set. The new context will cascade cancellation from its parent.
-
withValues
public <V1,V2,V3> Context withValues(Context.Key<V1> k1, V1 v1, Context.Key<V2> k2, V2 v2, Context.Key<V3> k3, V3 v3)
Create a new context with the given key value set. The new context will cascade cancellation from its parent.
-
withValues
public <V1,V2,V3,V4> Context withValues(Context.Key<V1> k1, V1 v1, Context.Key<V2> k2, V2 v2, Context.Key<V3> k3, V3 v3, Context.Key<V4> k4, V4 v4)
Create a new context with the given key value set. The new context will cascade cancellation from its parent.For more than 4 key-value pairs, note that multiple calls to
withValue(io.grpc.Context.Key<V>, V)can be chained together. That is,context.withValues(K1, V1, K2, V2); // is the same as context.withValue(K1, V1).withValue(K2, V2);
Nonetheless,
Contextshould not be treated like a general purpose map with a large number of keys and values — combine multiple related items together into a single key instead of separating them. But if the items are unrelated, have separate keys for them.
-
fork
public Context fork()
Create a new context which propagates the values of this context but does not cascade its cancellation.
-
attach
public Context attach()
Attach this context, thus enter a new scope within which this context iscurrent(). The previously current context is returned. It is allowed to attach contexts whereisCancelled()istrue.Instead of using
attach()anddetach(Context)most use-cases are better served by using therun(Runnable)orcall(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 correspondingdetach(Context)within the same method:Context previous = someContext.attach(); try { // Do work } finally { someContext.detach(previous); }
-
detach
public void detach(Context toAttach)
Reverse anattach(), 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 sameattach()call. If anattach()and adetach()meet above requirements, they match.It is expected that between any pair of matching
attach()anddetach(), allattach()es anddetach()es are called in matching pairs. If this method finds that this context is notcurrent, 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 useContext.current().detach(), as this will compromise this error-detecting mechanism.
-
isCancelled
public boolean isCancelled()
Is this context cancelled.
-
cancellationCause
public Throwable cancellationCause()
If a contextisCancelled()then return the cause of the cancellation ornullif context was cancelled without a cause. If the context is not yet cancelled will always returnnull.The cancellation cause is provided for informational purposes only and implementations should generally assume that it has already been handled and logged properly.
-
getDeadline
public Deadline getDeadline()
A context may have an associatedDeadlineat which it will be automatically cancelled.- Returns:
- A
Deadlineornullif no deadline is set.
-
addListener
public void addListener(Context.CancellationListener cancellationListener, Executor executor)
Add a listener that will be notified when the context becomes cancelled.
-
removeListener
public void removeListener(Context.CancellationListener cancellationListener)
Remove aContext.CancellationListener.
-
fixedContextExecutor
public Executor fixedContextExecutor(Executor e)
Wrap anExecutorso that it always executes with this context as thecurrent()context. It is generally expected thatcurrentContextExecutor(Executor)would be used more commonly than this method.One scenario in which this executor may be useful is when a single thread is sharding work to multiple threads.
- See Also:
currentContextExecutor(Executor)
-
currentContextExecutor
public static Executor currentContextExecutor(Executor e)
Create an executor that propagates thecurrent()context whenExecutor.execute(java.lang.Runnable)is called as thecurrent()context of theRunnablescheduled. Note that this is a static method.- See Also:
fixedContextExecutor(Executor)
-
-