Class ClientRequestContext

java.lang.Object
com.loomcache.client.context.ClientRequestContext

public final class ClientRequestContext extends Object
Request context propagation for LoomClient using ThreadLocal.

ThreadLocals provide per-thread context propagation compatible with Java 17+. Each thread sees its own isolated copy of the context values.

This class provides client-side context for distributed cache operations: - Request ID: unique identifier for this request (for tracing) - Operation Name: the cache operation being performed (GET, PUT, etc.) - Map Name: the specific map/cache being accessed - Start Timestamp: when the request was initiated (for latency tracking)

Usage with try-with-resources

  var context = ClientRequestContext.newScope()
          .requestId("req-12345")
          .operationName("GET")
          .mapName("user-cache")
          .startTimestamp(System.nanoTime());
  context.run(() -> {
      // Within this block, context is accessible via:
      String reqId = ClientRequestContext.requestId();
      String op = ClientRequestContext.operationName();
      // ... rest of operation ...
  });
Since:
1.0
See Also:
  • Constructor Details

    • ClientRequestContext

      public ClientRequestContext()
  • Method Details

    • requestId

      public static @Nullable String requestId()
      Get the current request ID, or null if not set.
      Returns:
      the request ID or null if not set in current scope
    • operationName

      public static @Nullable String operationName()
      Get the current operation name, or null if not set.
      Returns:
      the operation name or null if not set in current scope
    • mapName

      public static @Nullable String mapName()
      Get the current map name, or null if not set.
      Returns:
      the map name or null if not set in current scope
    • startTimestamp

      public static @Nullable Long startTimestamp()
      Get the current start timestamp, or null if not set.
      Returns:
      the start timestamp or null if not set in current scope
    • isActive

      public static boolean isActive()
      Check if any context is currently set.
    • newScope

      public static ClientRequestContext.ContextScope newScope()
      Create a new scope builder for setting context values.
      Returns:
      a new builder