How to change the emission thread?#
Except indicated otherwise, Mutiny invokes the next stage using the thread emitting the event from upstream. So, in the following code, the transform stage is invoked from the thread emitting the event.
You can switch to another thread using the emitOn
operator.
The emitOn
operator lets you switch the thread used to dispatch (upstream -> downstream) events, so items, failure and completion events.
Just pass the executor you want to use.
Note
You cannot pass a specific thread, but you can implement a simple Executor
dispatching on that specific thread, or use a single threaded executor.
Warning
Be careful as this operator can lead to concurrency problems with non thread-safe objects such as CDI request-scoped beans. It might also break reactive-streams semantics with items being emitted concurrently.