There’s not really a good answer for when to use EventBus over Stream other than the general guidance that EventBus is really meant to be a single, special kind of micro message bus that doesn’t easily support pipelines (by that I mean it’s not easy to send an event back to the EventBus under a different topic key). The EventBus was the primary component of Reactor 1.x because the Stream was just being introduced. By Reactor 2.0 and its implementation of Reactive Streams, the Stream has pretty well infected/taken over everything with just a few exceptions.
If you don’t have access to the “head” of a Stream onto which you want to publish values, you have the benefit of declaring only a single Consumer to handle that event (with no subsequent, dependent steps), and you need the flexibility of routing events to given Consumers based on possibly dynamic criteria, then an EventBus is probably still your best choice. If your processing can have multiple starting points, then the Selector is your friend. For the majority of other use cases, however, we’re finding that the Stream/Processor model actually works better and is more flexible. Rather than sending data to an EventBus, you would create an ah-hoc Stream using the input value as the starting point:
Streams.just(inputValue).flatMap(o -> explodeIntoAnotherStream(o)).consume(out -> handleOutputObject(out));
This uses lambdas efficiently to scope tasks to the input data rather than creating a long-lived Consumer singleton that is assigned to the EventBus under a given Selector registration. That may be good or bad depending on your throughput and memory pressure requirements. But it also introduces pipeline composition which the EventBus lacks. Being able to do retries, reduce, transform, and any number of combinatory or transformative operations is more flexible than the EventBus by its nature. These benefits may outweigh the efficiency of the EventBus in some circumstances. Note that this doesn’t require the lookup and caching of a Selector mapping. If you’re not careful, that can really burn you, especially if you’re publishing lots and lots of unique keys which will be cached.
I hope that gives you some idea of the differences between the two. As a general rule, I’d say: go with Stream for everything unless it’s clear you need the EventBus for a specific situation (the flexibility of Selectors and the efficiency of singleton Consumers).
Thanks!
Jon Brisbin
Reactor Project Lead