Context propagation mechanism for Java applications that enables carrying scoped values across API boundaries and between threads
npx @tessl/cli install tessl/maven-io-grpc--grpc-context@1.73.0gRPC Context is a Java library that provides a context propagation mechanism for carrying scoped values across API boundaries and between threads. It enables secure transmission of security principals, credentials, and distributed tracing information through immutable Context objects with automatic inheritance, cancellation propagation, and timeout-based automatic cancellation.
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-context</artifactId>
<version>1.73.0</version>
</dependency>import io.grpc.Context;
import io.grpc.Deadline;import io.grpc.Context;
import java.util.concurrent.TimeUnit;
// Create a context key
Context.Key<String> USER_ID_KEY = Context.key("userId");
// Set a value in the current context
Context withUser = Context.current().withValue(USER_ID_KEY, "user123");
// Run code within the context scope
withUser.run(() -> {
String userId = USER_ID_KEY.get(); // "user123"
processUserRequest(userId);
});
// Create a cancellable context with timeout
try (Context.CancellableContext withTimeout = Context.current()
.withDeadlineAfter(5, TimeUnit.SECONDS, scheduler)) {
Context toRestore = withTimeout.attach();
try {
// Long-running operation that will be cancelled after 5 seconds
performLongRunningTask();
} finally {
withTimeout.detach(toRestore);
}
}gRPC Context is built around several key components:
Core context creation, attachment, and value propagation functionality. Essential for maintaining scoped state across API boundaries and thread switches.
public static final Context ROOT;
public static Context current();
public static <T> Context.Key<T> key(String debugString);
public static <T> Context.Key<T> keyWithDefault(String debugString, T defaultValue);
public <V> Context withValue(Context.Key<V> key, V value);
public Context fork();
public Context attach();
public void detach(Context toAttach);Cancellable contexts with hierarchical cancellation propagation, listener notification, and cause tracking for managing the lifecycle of operations.
public Context.CancellableContext withCancellation();
public boolean isCancelled();
public Throwable cancellationCause();
public void addListener(Context.CancellationListener listener, Executor executor);
public interface CancellationListener {
void cancelled(Context context);
}
public static final class CancellableContext extends Context implements Closeable {
public boolean cancel(Throwable cause);
public void close();
}Absolute time-based deadline management with automatic cancellation and timeout conversion capabilities for time-bounded operations.
public Context.CancellableContext withDeadline(Deadline deadline, ScheduledExecutorService scheduler);
public Context.CancellableContext withDeadlineAfter(long duration, TimeUnit unit, ScheduledExecutorService scheduler);
public Deadline getDeadline();
public static Deadline after(long duration, TimeUnit units);
public boolean isExpired();
public long timeRemaining(TimeUnit unit);Utilities for wrapping Runnables, Callables, and Executors to automatically propagate context across thread boundaries and asynchronous operations.
public void run(Runnable r);
public <V> V call(Callable<V> c) throws Exception;
public Runnable wrap(Runnable r);
public <C> Callable<C> wrap(Callable<C> c);
public Executor fixedContextExecutor(Executor e);
public static Executor currentContextExecutor(Executor e);public static final class Key<T> {
public T get();
public T get(Context context);
public String toString();
}
public abstract static class Storage {
public abstract Context doAttach(Context toAttach);
public abstract void detach(Context toDetach, Context toRestore);
public abstract Context current();
}
public abstract static class Ticker {
public abstract long nanoTime();
}