Class DefaultEventDispatcher
- java.lang.Object
-
- discord4j.core.event.DefaultEventDispatcher
-
- All Implemented Interfaces:
EventDispatcher
public class DefaultEventDispatcher extends Object implements EventDispatcher
Distributes events to subscribers using a ReactorFluxProcessoras backend.The underlying processor can be configured at construction time. Thread affinity can also be configured by supplying a
Scheduler, while anFluxSink.OverflowStrategyis applied to handle back-pressure of inbound Events, especially during startup.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDefaultEventDispatcher.BuilderA builder to createEventDispatcherinstances.
-
Field Summary
-
Fields inherited from interface discord4j.core.event.EventDispatcher
DEFAULT_EVENT_SCHEDULER
-
-
Constructor Summary
Constructors Constructor Description DefaultEventDispatcher(FluxProcessor<Event,Event> eventProcessor, FluxSink.OverflowStrategy overflowStrategy, Scheduler eventScheduler)Creates a new event dispatcher using the givenFluxProcessor, backpressure-handling strategy and threading model.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <E extends Event>
Flux<E>on(Class<E> eventClass)voidpublish(Event event)Publishes anEventto the dispatcher.voidshutdown()Signal that this event dispatcher must terminate and release its resources.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface discord4j.core.event.EventDispatcher
on, on
-
-
-
-
Constructor Detail
-
DefaultEventDispatcher
public DefaultEventDispatcher(FluxProcessor<Event,Event> eventProcessor, FluxSink.OverflowStrategy overflowStrategy, Scheduler eventScheduler)
Creates a new event dispatcher using the givenFluxProcessor, backpressure-handling strategy and threading model.- Parameters:
eventProcessor- aFluxProcessorofEvent, used to bridge gateway events to the dispatcher subscribersoverflowStrategy- an overflow strategy, seeFluxSink.OverflowStrategyfor the available strategieseventScheduler- aSchedulerto ensure a certain thread model on each published signal
-
-
Method Detail
-
on
public <E extends Event> Flux<E> on(Class<E> eventClass)
Description copied from interface:EventDispatcherRetrieves aFluxwith elements of the givenEventtype. ThisFluxhas to be subscribed to in order to start processing. SeeEventclass for the list of possible event classes.Note: Errors occurring while processing events will terminate your sequence. If you wish to use a version capable of handling errors for you, use
EventDispatcher.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
flatMapblock and useMono.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 docs page.
- Specified by:
onin interfaceEventDispatcher- Type Parameters:
E- the type of the event class- Parameters:
eventClass- the event class to obtain events from- Returns:
- a new
Fluxwith the requested events
-
publish
public void publish(Event event)
Description copied from interface:EventDispatcherPublishes anEventto the dispatcher. Might throw an unchecked exception if the dispatcher can't handle this event.- Specified by:
publishin interfaceEventDispatcher- Parameters:
event- theEventto publish
-
shutdown
public void shutdown()
Description copied from interface:EventDispatcherSignal that this event dispatcher must terminate and release its resources.- Specified by:
shutdownin interfaceEventDispatcher
-
-