Class ReplayingEventDispatcher

java.lang.Object
discord4j.core.event.ReplayingEventDispatcher
All Implemented Interfaces:
EventDispatcher

public class ReplayingEventDispatcher extends Object implements EventDispatcher
Distributes events to active subscribers, while using a fallback storage if no subscribers are present that can be replayed to future late subscribers.

Allows configuring the main FluxProcessor backend, its publishing Scheduler for thread affinity, the ReplayProcessor for storage and a Predicate to determine which events should be stored for replay.

While under no subscribers, this event dispatcher will store all incoming events that match the given filter to its replay processor. Once the first subscriber has connected, the given stop trigger is subscribed and once it terminates or is cancelled, no further events will be stored for replay, until all existing subscribers have disconnected.

During the replay window, late subscribers benefit from all events that are pushed to this dispatcher, therefore enabling scenarios where very early events are not missed despite connecting late.

  • Constructor Details

    • ReplayingEventDispatcher

      public ReplayingEventDispatcher(FluxProcessor<Event,Event> eventProcessor, FluxSink.OverflowStrategy overflowStrategy, Scheduler eventScheduler, ReplayProcessor<Event> replayEventProcessor, FluxSink.OverflowStrategy replayEventOverflowStrategy, Predicate<Event> replayEventFilter, Scheduler timedTaskScheduler, Publisher<?> stopReplayingTrigger)
      Creates a new event dispatcher that is able to retain events to replay to multiple late subscribers, using the given parameters.
      Parameters:
      eventProcessor - a processor used to bridge gateway events to the dispatcher connected subscribers
      overflowStrategy - an overflow strategy, see FluxSink.OverflowStrategy for the available strategies
      eventScheduler - a Scheduler to ensure a certain thread model on each published signal
      replayEventProcessor - a processor used to store events while no subscribers are connected and will be forwarded to all late subscribers
      replayEventOverflowStrategy - an overflow strategy while pushing events to the replay processor
      replayEventFilter - a filter used to decide whether an Event should be published to the replay processor
      timedTaskScheduler - a time-capable Scheduler used to detect whether all events have replayed to a subscriber
      stopReplayingTrigger - a trigger Publisher that is subscribed upon the first subscriber connects to this dispatcher. Upon completion, error or cancellation, the replaying window will be closed until all subscribers have disconnected
  • Method Details

    • on

      public <E extends Event> Flux<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 docs 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
    • create

      public static EventDispatcher create()
      Return a new default ReplayingEventDispatcher.
      Returns:
      a new replay event dispatcher
    • builder

      public static ReplayingEventDispatcher.Builder builder()
      Return a new builder for ReplayingEventDispatcher.
      Returns:
      a new builder