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:
attach() should have a detach() in the same method. And every
CancellableContext should be cancelled at some point. Breaking these rules may lead to memory
leaks.
ROOT for a non-null instance.
| Modifier and Type | Class 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 | |
| private static class | |
| public abstract static class | Context.Storage
Defines the mechanisms for attaching and detaching the "current" context. |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final Context. | |
| pack-priv static final int | |
| pack-priv final int | |
| pack-priv final PersistentHashArrayMappedTrie. | |
| pack-priv static final Logger | |
| public static final Context | ROOT
The logical root context which is the ultimate ancestor of all contexts. |
| Access | Constructor and Description |
|---|---|
| private | |
| private | |
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void | addListener(final Context.
Add a listener that will be notified when the context becomes cancelled. |
| public Context | |
| public <V> V | |
| pack-priv static Context. | cancellableAncestor(Context parent)
Returns |
| public Throwable | cancellationCause()
If a context |
| pack-priv static <T> T | |
| public static Context | |
| public static Executor | currentContextExecutor(final Executor e)
Create an executor that propagates the |
| public void | |
| public Executor | fixedContextExecutor(final Executor e)
Wrap an |
| public Context | fork()
Create a new context which propagates the values of this context but does not cascade its cancellation. |
| public Deadline | |
| public boolean | |
| pack-priv boolean | |
| public static <T> Context. | |
| public static <T> Context. | 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 |
| pack-priv int | |
| public void | |
| public void | |
| pack-priv static Context. | |
| 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. | withCancellation()
Create a new context which is independently cancellable and also cascades cancellation from its parent. |
| public Context. | withDeadline(Deadline newDeadline, ScheduledExecutorService scheduler)
Create a new context which will cancel itself at the given |
| public Context. | withDeadlineAfter(long duration, TimeUnit unit, ScheduledExecutorService scheduler)
Create a new context which will cancel itself after the given |
| public <V> Context | |
| public <V1, V2> Context | withValues(Context.
Create a new context with the given key value set. |
| public <V1, V2, V3> Context | |
| public <V1, V2, V3, V4> Context | |
| public Runnable | |
| public <C> Callable |