Context Journey

What We'll Learn

  • Working with ScopedValue-backed contexts as composable values
  • Threading request, security, and trace contexts through a workflow without ThreadLocal
  • Propagating context across virtual-thread boundaries with VTask
  • Combining contexts with the Effect Path API for production-shaped flows

Tutorials: 6 | Prerequisites: complete the Effect API Journey and the Concurrency: VTask Journey first.

Where This Fits in the Bigger Picture

A Context is the value-level form of "the request id, principal, or trace id every part of this workflow needs". In production, One Line, Six Layers almost always runs inside one or more contexts: RequestContext for tracing, SecurityContext for the principal, custom Context for tenant or feature-flag state. This journey teaches the patterns.

Each tutorial opens with a Pain → Promise header showing the imperative-Java pattern (ThreadLocal, MDC, SecurityContextHolder) the value-level Context replaces.

Tutorials

#TutorialFocus
01Context BasicsScopedValue-backed contexts, the ContextValue constructor pair
02Context CompositionflatMap, the Functor / Monad / Applicative instance
03RequestContext PatternsDistributed tracing, request id propagation
04SecurityContext PatternsAuthentication and authorisation as values
05Context with VTaskPropagation across virtual-thread boundaries
06Advanced Context PatternsError handling and recovery

How Each Exercise Is Structured

Same template as the rest of the chapter:

  • Pain → Promise header at the top of each file
  • Java idiom anchor mapping each Context type to its ThreadLocal / MDC / SecurityContextHolder cousin
  • Exercise bodies and solutions are unchanged from the original; per-exercise tiered hints and teaching-solution commentary are scheduled for the follow-up Phase 3.5 pass

Running the Tutorials

./gradlew :hkj-examples:tutorialTest --tests "*tutorial.context.*"

# Solutions
./gradlew :hkj-examples:test --tests "*solutions.context.*"

# Per-journey progress
./gradlew :hkj-examples:tutorialProgress

Previous: Concurrency: Scope & Resource Next: Optics Journeys