Top Description Inners Fields Constructors Methods
io.grpc

public Class Context

extends Object
Class Inheritance
Known Direct Subclasses
io.grpc.Context.CancellableContext
Annotations
@CheckReturnValue
Imports
io.grpc.Context.CheckReturnValue, .PersistentHashArrayMappedTrie.Node, java.io.Closeable, java.util.ArrayList, java.util.concurrent.Callable, .Executor, .ScheduledExecutorService, .ScheduledFuture, .TimeUnit, .TimeoutException, java.util.concurrent.atomic.AtomicReference, java.util.logging.Level, .Logger

A context propagation mechanism which can carry scoped-values across API boundaries and between threads. Examples of state propagated via context include:

A Context object can be attached to the 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, through current. The scope is later exited by detaching the 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 CancellationListener to 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 to attach() an already cancelled context to make it current. To cancel a context (and its descendants) you first create a CancellableContext and when you need to signal cancellation call CancellableContext#cancel or CancellableContext#detachAndCancel.

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:

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
Context.CancellableContext

A context which inherits cancellation from its parent but which can also be independently cancelled and which will propagate cancellation to its descendants.

public static interface
Context.CancellationListener

A listener notified on context cancellation.

pack-priv static @interface
pack-priv static @interface
private static enum
private static class
Context.ExecutableListener

Stores listener and executor pair.

public static class
Context.Key<T>

Key for indexing values stored in a context.

private static class
public abstract static class
Context.Storage

Defines the mechanisms for attaching and detaching the "current" context.

Field Summary

Modifier and TypeField and Description
pack-priv final Context.CancellableContext
pack-priv static final int
pack-priv final int
pack-priv final PersistentHashArrayMappedTrie.Node<Context.Key<?>, Object>
pack-priv static final Logger
public static final Context
ROOT

The logical root context which is the ultimate ancestor of all contexts.

Constructor Summary

AccessConstructor and Description
private
Context(PersistentHashArrayMappedTrie.Node<Context.Key<?>, Object> keyValueEntries, int generation)

Construct a context that cannot be cancelled and will not cascade cancellation from its parent.

private
Context(Context parent, PersistentHashArrayMappedTrie.Node<Context.Key<?>, Object> keyValueEntries)

Construct a context that cannot be cancelled but will cascade cancellation from its parent if it is cancellable.

private
Context()

Construct for ROOT.

Method Summary

Modifier and TypeMethod and Description
public void
addListener(final Context.CancellationListener cancellationListener, final Executor executor)

Add a listener that will be notified when the context becomes cancelled.

public Context
attach()

Attach this context, thus enter a new scope within which this context is current.

public <V> V

Returns:

result of call.
call
(Callable<V>
Callable to call.
c
)

Immediately call a Callable with this context as the current context.

pack-priv static Context.CancellableContext
cancellableAncestor(Context parent)

Returns parent if it is a CancellableContext, otherwise returns the parent's cancellableAncestor.

public Throwable
cancellationCause()

If a context isCancelled() then return the cause of the cancellation or null if context was cancelled without a cause.

pack-priv static <T> T
checkNotNull(T reference, Object errorMessage)

public static Context
current()

Return the context associated with the current scope, will never return null.

public static Executor
currentContextExecutor(final Executor e)

Create an executor that propagates the current context when Executor#execute is called as the current context of the Runnable scheduled.

public void
detach(Context toAttach)

Reverse an attach(), restoring the previous context and exiting the current scope.

public Executor
fixedContextExecutor(final Executor e)

Wrap an Executor so that it always executes with this context as the current context.

public Context
fork()

Create a new context which propagates the values of this context but does not cascade its cancellation.

public Deadline

Returns:

A io.grpc.Deadline or null if no deadline is set.
getDeadline
()

A context may have an associated Deadline at which it will be automatically cancelled.

public boolean
isCancelled()

Is this context cancelled.

pack-priv boolean
public static <T> Context.Key<T>
key(String
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.
debugString
)

Create a Key with the given debug name.

public static <T> Context.Key<T>
keyWithDefault(String
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.
debugString
,
T defaultValue)

Create a Key with the given debug name and default value.

pack-priv int
public void
public void
run(Runnable
Runnable to run.
r
)

Immediately run a Runnable with this context as the current context.

pack-priv static Context.Storage
private static void
validateGeneration(int generation)

If the ancestry chain length is unreasonably long, then print an error to the log and record the stack trace.

public Context.CancellableContext
withCancellation()

Create a new context which is independently cancellable and also cascades cancellation from its parent.

public Context.CancellableContext
withDeadline(Deadline newDeadline, ScheduledExecutorService scheduler)

Create a new context which will cancel itself at the given Deadline.

public Context.CancellableContext
withDeadlineAfter(long duration, TimeUnit unit, ScheduledExecutorService scheduler)

Create a new context which will cancel itself after the given duration from now.

public <V> Context
withValue(Context.Key<V> k1, V v1)

Create a new context with the given key value set.

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.

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.

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.

public Runnable
wrap(final Runnable r)

Wrap a Runnable so that it executes with this context as the current context.

public <C> Callable<C>
wrap(final Callable<C> c)

Wrap a Callable so that it executes with this context as the current context.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait