title: How to use Reactor to dispatch events to multiple consumers and to filter events based on event data?
body: <p>I'm evaluating Reactor (<a href="https://github.com/reactor/reactor" rel="nofollow">https://github.com/reactor/reactor</a>) if it would be suitable for creating an event dispatching framework inside my Spring / enterprise application. </p>
  
  <p>First, consider a scenario in which you have an interface <code>A</code> and concrete event classes <code>B</code>, <code>C</code>, and so on. I want to dispatch concrete events to multiple consumers i.e. observers. Those are registered to a global <code>Reactor</code> instance during bean post processing. However, you can register them dynamically. In most cases there is one producer sending events to multiple consumers at high rate.</p>
  
  <p>I have used <code>Selector</code>s, namely, the <code>ClassSelector</code> to dispatch the correct event types to the correct consumers. This seems to work nicely.</p>
  
  <pre><code>Reactor reactor = ...
  B event = ...
  Consumer&lt;Event&lt;B&gt;&gt; consumer = ...
  
  // Registration is used to cancel the subscription later
  Registration&lt;?&gt; registration = reactor.on(T(event.getClass()), consumer);
  </code></pre>
  
  <p>To notify, use the type of the event as a key</p>
  
  <pre><code>B event = ...
  reactor.notify(event.getClass(), Event.wrap(event));
  </code></pre>
  
  <p>However, I'm wondering if this is the suggested way to dispatch events efficiently?</p>
  
  <p>Secondly, I was wondering that is it possible to filter events based on the event data? If I understand correctly, <code>Selector</code>s are only for inspecting the key. I'm not referring to event headers here but to the domain specific object properties. I was wondering of using <code>Streams</code> and <code>Stream.filter(Predicate&lt;T&gt; p)</code> for this but is it also possible to filter using <code>Reactor</code> and <code>Selector</code>s? Of course I could write a delegating consumer that inspects the data and delegates it to registered consumers if needed.</p>