public interface EventDispatcher
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.
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();
Modifier and Type | Interface and Description |
---|---|
static interface |
EventDispatcher.Builder |
Modifier and Type | Field and Description |
---|---|
static Supplier<Scheduler> |
DEFAULT_EVENT_SCHEDULER |
Modifier and Type | Method and Description |
---|---|
static EventDispatcher |
buffering()
Create an
EventDispatcher that will buffer incoming events to retain all startup events as each
shard connects at the cost of increased memory usage and potential OutOfMemoryError if events are not
consumed. |
static EventDispatcher.Builder |
builder()
Create an
EventDispatcher builder. |
<E extends Event> |
on(Class<E> eventClass)
|
void |
publish(Event event)
Publishes an
Event to the dispatcher. |
static EventDispatcher |
replaying()
Create an
EventDispatcher that is capable of replaying up to 2 minutes worth of important events like
GuildCreateEvent and GatewayLifecycleEvent that arrive while no subscribers are connected to
all late subscribers, as long as they subscribe within the replay window of 5 seconds. |
static EventDispatcher |
replayingWithSize(int historySize)
Create an
EventDispatcher that will replays up to historySize elements to late subscribers. |
static EventDispatcher |
replayingWithTimeout(Duration maxAge)
Create an
EventDispatcher that is time-bounded and retains all elements whose age is at most maxAge , replaying them to late subscribers. |
void |
shutdown()
Signal that this event dispatcher must terminate and release its resources.
|
static EventDispatcher |
withEarliestEvents(int bufferSize)
Create an
EventDispatcher that will buffer incoming events up to the given bufferSize elements,
where subsequent events will be dropped in favor of retaining the earliest ones. |
static EventDispatcher |
withLatestEvents(int bufferSize)
Create an
EventDispatcher that will buffer incoming events up to the given bufferSize elements,
where earliest events will be dropped in favor of retaining the latest ones. |
<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. 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 eventsvoid publish(Event event)
Event
to the dispatcher. Might throw an unchecked exception if the dispatcher can't
handle this event.event
- the Event
to publishvoid shutdown()
static EventDispatcher.Builder builder()
EventDispatcher
builder. It can be configured with a custom FluxProcessor
for
events, a custom FluxSink.OverflowStrategy
to handle backpressure and a custom Scheduler
to
dispatch events.EventDispatcher.Builder
static EventDispatcher buffering()
EventDispatcher
that will buffer incoming events to retain all startup events as each
shard connects at the cost of increased memory usage and potential OutOfMemoryError
if events are not
consumed. Since this factory uses EmitterProcessor
, it will only produce previously buffered events to
the first subscriber.EventDispatcher
backed by an EmitterProcessor
static EventDispatcher withEarliestEvents(int bufferSize)
EventDispatcher
that will buffer incoming events up to the given bufferSize
elements,
where subsequent events will be dropped in favor of retaining the earliest ones. Since this factory uses
EmitterProcessor
, it will only produce previously buffered events to the first subscriber.bufferSize
- the number of events to keep in the backlogEventDispatcher
keeping the earliest events backed by an EmitterProcessor
static EventDispatcher withLatestEvents(int bufferSize)
EventDispatcher
that will buffer incoming events up to the given bufferSize
elements,
where earliest events will be dropped in favor of retaining the latest ones. Since this factory uses
EmitterProcessor
, it will only produce previously buffered events to the first subscriber.bufferSize
- the number of events to keep in the backlogEventDispatcher
keeping the latest events backed by an EmitterProcessor
static EventDispatcher replaying()
EventDispatcher
that is capable of replaying up to 2 minutes worth of important events like
GuildCreateEvent
and GatewayLifecycleEvent
that arrive while no subscribers are connected to
all late subscribers, as long as they subscribe within the replay window of 5 seconds. After the replay window
has closed, it behaves like an emitter event dispatcher.
This allows controlling the memory overhead of dispatchers like buffering()
while still keeping a record
of important events to all late subscribers, even after login has completed.
This dispatcher can be customized through the use of ReplayingEventDispatcher.builder()
.
EventDispatcher
that is capable of replaying events to late subscribersstatic EventDispatcher replayingWithTimeout(Duration maxAge)
EventDispatcher
that is time-bounded and retains all elements whose age is at most maxAge
, replaying them to late subscribers. Be aware that using this type of dispatcher with operators such
as Flux.retry()
or Flux.repeat()
that re-subscribe to the dispatcher will observe the same
elements as the backlog contains.maxAge
- the maximum age of the contained itemsEventDispatcher
backed by a ReplayProcessor
with a time-bounded backlogReplayProcessor.createTimeout(Duration)
static EventDispatcher replayingWithSize(int historySize)
EventDispatcher
that will replays up to historySize
elements to late subscribers.
Be aware that using this type of dispatcher with operators such as Flux.retry()
or
Flux.repeat()
that re-subscribe to the dispatcher will observe the same elements as the backlog contains.historySize
- the backlog size or maximum items retained for replayEventDispatcher
backed by a ReplayProcessor
with a customized backlog sizeReplayProcessor.create(int)