class ReactorContext(val context: Context) : AbstractCoroutineContextElement

Wraps Reactor's Context into a CoroutineContext element for seamless integration between Reactor and kotlinx.coroutines. Context.asCoroutineContext puts Reactor's Context elements into a CoroutineContext, which can be used to propagate the information about Reactor's Context through coroutines.

This context element is implicitly propagated through subscribers' context by all Reactive integrations, such as mono, flux, Publisher.asFlow, Flow.asPublisher and Flow.asFlux. Functions that subscribe to a reactive stream (e.g. Publisher.awaitFirst), too, propagate ReactorContext to the subscriber's Context.

Examples of Reactive context integration.

Propagating ReactorContext to Reactor's Context

val flux = myDatabaseService.getUsers()
.contextWrite { ctx -> println(ctx); ctx }
flux.awaitFirst() // Will print "null"

// Now add ReactorContext
withContext(Context.of("answer", "42").asCoroutineContext()) {
flux.awaitFirst() // Will print "Context{'key'='value'}"

Propagating subscriber's Context to ReactorContext:

val flow = flow {
println("Reactor context in Flow: " + currentCoroutineContext()[ReactorContext])
// No context
.subscribe() // Will print 'Reactor context in Flow: null'
// Add subscriber's context
.contextWrite { ctx -> ctx.put("answer", 42) }
.subscribe() // Will print "Reactor context in Flow: Context{'answer'=42}"


Link copied to clipboard
fun ReactorContext(contextView: ContextView)
Link copied to clipboard
fun ReactorContext(context: Context)


Link copied to clipboard
object Key : CoroutineContext.Key<ReactorContext>


Link copied to clipboard
open override fun toString(): String


Link copied to clipboard
val context: Context