public class DefaultEventDispatcher extends Object implements EventDispatcher
FluxProcessor
as backend.
The underlying processor can be configured at construction time. Thread affinity can also be configured by
supplying a Scheduler
, while an FluxSink.OverflowStrategy
is applied to handle
back-pressure of inbound Events, especially during startup.
Modifier and Type | Class and Description |
---|---|
static class |
DefaultEventDispatcher.Builder
A builder to create
EventDispatcher instances. |
DEFAULT_EVENT_SCHEDULER
Constructor and Description |
---|
DefaultEventDispatcher(FluxProcessor<Event,Event> eventProcessor,
FluxSink.OverflowStrategy overflowStrategy,
Scheduler eventScheduler)
Creates a new event dispatcher using the given
FluxProcessor , backpressure-handling strategy and
threading model. |
Modifier and Type | Method and Description |
---|---|
<E extends Event> |
on(Class<E> eventClass)
|
void |
publish(Event event)
Publishes an
Event to the dispatcher. |
void |
shutdown()
Signal that this event dispatcher must terminate and release its resources.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
buffering, builder, replaying, replayingWithSize, replayingWithTimeout, withEarliestEvents, withLatestEvents
public DefaultEventDispatcher(FluxProcessor<Event,Event> eventProcessor, FluxSink.OverflowStrategy overflowStrategy, Scheduler eventScheduler)
FluxProcessor
, backpressure-handling strategy and
threading model.eventProcessor
- a FluxProcessor
of Event
, used to bridge gateway events to the dispatcher
subscribersoverflowStrategy
- an overflow strategy, see FluxSink.OverflowStrategy
for the available strategieseventScheduler
- a Scheduler
to ensure a certain thread model on each published signalpublic <E extends Event> Flux<E> on(Class<E> eventClass)
EventDispatcher
Flux
with elements of the given Event
type.
Note: Errors occurring while processing events will terminate your sequence. 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.
on
in interface EventDispatcher
E
- the type of the event classeventClass
- the event class to obtain events fromFlux
with the requested eventspublic void publish(Event event)
EventDispatcher
Event
to the dispatcher. Might throw an unchecked exception if the dispatcher can't
handle this event.publish
in interface EventDispatcher
event
- the Event
to publishpublic void shutdown()
EventDispatcher
shutdown
in interface EventDispatcher