kotlindependency-injectionguicecoroutinerequestscope

Will Kotlin coroutine always run on same thread? If not then how can we make it work with Guice Request Scope semantics?


Request scope enables us to track request wise variables throughout the request processing. But I think it depends on thread local variables. I assume using Kotlin coroutines will that break the Guice Injection of Request Scope semantics..


Solution

  • Coroutines do not always run on the same thread and therefore you will have problems with thread local variables, e.g. Guice Request Scope.

    But it is possible to transfer thread local variables between coroutines: https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/coroutine-context-and-dispatchers.md#thread-local-data

    I don't know Guice and so I don't know if there is a way to integrate ThreadContextElement into this framework.

    See also: How to use code that relies on ThreadLocal with Kotlin coroutines