public class EventDispatcher extends Object
Event
instances can be published over this class and dispatched to all
subscribers.
Individual events can be published to subscribers using publish(Event)
while they can be used to consumed
through on(Class)
giving the proper Event
class as argument.
Uses an underlying FluxProcessor
that must be supplied on construction, as well as a Scheduler
to
define subscriber thread affinity.
Each event can be consumed using the following pattern:
dispatcher.on(MessageCreatedEvent.class) .subscribe(event -> event.getMessage());While events can be published through:
fluxOfEvents.doOnNext(dispatcher::publish) .subscribe();
Constructor and Description |
---|
EventDispatcher(FluxProcessor<Event,Event> processor,
Scheduler scheduler)
Creates a new event dispatcher using the given processor and thread model.
|
public EventDispatcher(FluxProcessor<Event,Event> processor, Scheduler scheduler)
processor
- a FluxProcessor of Event types, used to bridge gateway events to the dispatcher subscribersscheduler
- a Scheduler to ensure a certain thread model on each published signalpublic <E extends Event> Flux<E> on(Class<E> eventClass)
Flux
with elements of the given Event
type.
Note: Errors occurring while processing events will terminate your sequence. If you wish to use
a version capable of handling errors for you, use on(Class, Function)
. See
Reactive Streams Spec
explaining this behavior.
A recommended pattern to use this method is wrapping your code that may throw exceptions within a flatMap
block and use Mono.onErrorResume(Function)
, Flux.onErrorResume(Function)
or
equivalent methods to maintain the sequence active:
client.getEventDispatcher().on(MessageCreateEvent.class) .flatMap(event -> myCodeThatMightThrow(event) .onErrorResume(error -> { // log and then discard the error to keep the sequence alive log.error("Failed to handle event!", error); return Mono.empty(); })) .subscribe();
For more alternatives to handling errors, please see Error Handling wiki page.
E
- the type of the event classeventClass
- the event class to obtain events fromFlux
with the requested eventspublic <E extends Event,T> Flux<T> on(Class<E> eventClass, Function<E,Publisher<T>> mapper)
Flux
with elements of the given Event
type, processing them through a given
Function
. Errors occurring within the mapper will be logged and discarded, preventing the termination of
the "infinite" event sequence.
There are multiple ways of using this event handling method, for example:
client.getEventDispatcher() .on(MessageCreateEvent.class, event -> { // myCodeThatMightThrow should return a Reactor type (Mono or Flux) return myCodeThatMightThrow(event); }) .subscribe(); client.getEventDispatcher() .on(MessageCreateEvent.class, event -> { // myCodeThatMightThrow *can* be blocking myCodeThatMightThrow(event); return Mono.empty(); // but we have to return a Reactor type }) .subscribe();
Continuing the chain after on(class, event -> ...)
will require your own error handling strategy.
Check the docs for on(Class)
for more details.
E
- the type of the event classT
- the type of the event mapper functioneventClass
- the event class to obtain events frommapper
- an event mapping function called on each event. If you do not wish to perform further operations
you can return Mono.empty()
.Flux
with the type resulting from the given event mapper