Class DefaultEventDispatcher

  • All Implemented Interfaces:
    EventDispatcher

    public class DefaultEventDispatcher
    extends Object
    implements EventDispatcher
    Distributes events to subscribers using a Reactor 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.

    • Method Detail

      • on

        public <E extends EventFlux<E> on​(Class<E> eventClass)
        Description copied from interface: EventDispatcher
        Retrieves a Flux with elements of the given Event type. This Flux has to be subscribed to in order to start processing. See Event class 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 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.

        Specified by:
        on in interface EventDispatcher
        Type Parameters:
        E - the type of the event class
        Parameters:
        eventClass - the event class to obtain events from
        Returns:
        a new Flux with the requested events
      • publish

        public void publish​(Event event)
        Description copied from interface: EventDispatcher
        Publishes an Event to the dispatcher. Might throw an unchecked exception if the dispatcher can't handle this event.
        Specified by:
        publish in interface EventDispatcher
        Parameters:
        event - the Event to publish
      • shutdown

        public void shutdown()
        Description copied from interface: EventDispatcher
        Signal that this event dispatcher must terminate and release its resources.
        Specified by:
        shutdown in interface EventDispatcher