public class Reactor extends java.lang.Object implements Observable
Event Consumers that can
subsequently be notified of events. A consumer is typically registered with a Selector which, by matching on
the notification key, governs which events the consumer will receive. When a Reactor is notified of
an Event, a task is dispatched using the reactor's Dispatcher which causes it to be executed on a
thread based on the implementation of the Dispatcher being used.| Modifier and Type | Class and Description |
|---|---|
class |
Reactor.ReplyToConsumer<E extends Event<?>,V> |
static class |
Reactor.ReplyToEvent<T> |
| Constructor and Description |
|---|
Reactor(Dispatcher dispatcher)
Create a new Reactor that uses the given
Dispatcher. |
Reactor(Dispatcher dispatcher,
EventRouter eventRouter)
Create a new Reactor that uses the given
Dispatcher. |
Reactor(Dispatcher dispatcher,
EventRouter eventRouter,
Consumer<java.lang.Throwable> dispatchErrorHandler,
Consumer<java.lang.Throwable> uncaughtErrorHandler) |
Reactor(Registry<Consumer<? extends Event<?>>> consumerRegistry,
Dispatcher dispatcher,
EventRouter eventRouter,
Consumer<java.lang.Throwable> dispatchErrorHandler,
Consumer<java.lang.Throwable> uncaughtErrorHandler)
Create a new Reactor that uses the given
dispatacher and eventRouter. |
| Modifier and Type | Method and Description |
|---|---|
<T> Consumer<java.lang.Iterable<Event<T>>> |
batchNotify(java.lang.Object key)
Notify the key with all any accepted iterable group of events by the returned
Consumer. |
<T> Consumer<java.lang.Iterable<Event<T>>> |
batchNotify(java.lang.Object key,
Consumer<java.lang.Void> completeConsumer)
Notify the key with all any accepted iterable group of events by the returned
Consumer. |
boolean |
equals(java.lang.Object o) |
Registry<Consumer<? extends Event<?>>> |
getConsumerRegistry()
|
Dispatcher |
getDispatcher()
Get the
Dispatcher currently in use. |
Consumer<java.lang.Throwable> |
getDispatchErrorHandler() |
EventRouter |
getEventRouter()
Get the
EventRouter used to route events to Consumers. |
java.util.UUID |
getId()
Get the unique, time-used
UUID of this Reactor. |
Consumer<java.lang.Throwable> |
getUncaughtErrorHandler() |
Reactor |
notify(java.lang.Object key)
Notify this component that the consumers registered with a
Selector that matches the key should be
triggered with a null input argument. |
<E extends Event<?>> |
notify(java.lang.Object key,
E ev)
Notify this component that an
Event is ready to be processed. |
<E extends Event<?>> |
notify(java.lang.Object key,
E ev,
Consumer<E> onComplete)
|
<S extends Supplier<? extends Event<?>>> |
notify(java.lang.Object key,
S supplier)
Notify this component that the given
Supplier can provide an event that's ready to be
processed. |
<E extends Event<?>> |
on(Selector selector,
Consumer<E> consumer)
|
<T> Consumer<Event<T>> |
prepare(java.lang.Object key)
Create an optimized path for publishing notifications to the given key.
|
<E extends Event<?>,V> |
receive(Selector sel,
Function<E,V> fn)
|
boolean |
respondsToKey(java.lang.Object key)
|
<T> void |
schedule(Consumer<T> consumer,
T data)
Schedule an arbitrary
Consumer to be executed on the current Reactor Dispatcher, passing the given . |
<E extends Event<?>> |
send(java.lang.Object key,
E ev)
|
<E extends Event<?>> |
send(java.lang.Object key,
E ev,
Observable replyTo)
Notify this component of the given
Event and register an internal Consumer that will take the
output of a previously-registered Function and respond to the key set on the Event's replyTo property and will call the notify method on the given Observable. |
<S extends Supplier<? extends Event<?>>> |
send(java.lang.Object key,
S supplier)
|
<S extends Supplier<? extends Event<?>>> |
send(java.lang.Object key,
S supplier,
Observable replyTo)
|
<REQ extends Event<?>,RESP extends Event<?>> |
sendAndReceive(java.lang.Object key,
REQ ev,
Consumer<RESP> reply)
|
<REQ extends Event<?>,RESP extends Event<?>,S extends Supplier<REQ>> |
sendAndReceive(java.lang.Object key,
S supplier,
Consumer<RESP> reply)
|
public Reactor(@Nullable
Dispatcher dispatcher)
Dispatcher. The reactor will use a default EventRouter that broadcast events to all of the registered consumers that match
the notification key and does not perform any type conversion.dispatcher - The Dispatcher to use. May be null in which case a new SynchronousDispatcher is usedpublic Reactor(@Nullable
Dispatcher dispatcher,
@Nullable
EventRouter eventRouter)
Dispatcher. The reactor will use a default CachingRegistry.dispatcher - The Dispatcher to use. May be null in which case a new synchronous dispatcher is used.eventRouter - The EventRouter used to route events to Consumers. May be null in which case the
default event router that broadcasts events to all of the registered consumers that match the notification key and does not perform any type conversion will be used.public Reactor(@Nullable
Dispatcher dispatcher,
@Nullable
EventRouter eventRouter,
@Nullable
Consumer<java.lang.Throwable> dispatchErrorHandler,
@Nullable
Consumer<java.lang.Throwable> uncaughtErrorHandler)
public Reactor(@Nonnull
Registry<Consumer<? extends Event<?>>> consumerRegistry,
@Nullable
Dispatcher dispatcher,
@Nullable
EventRouter eventRouter,
@Nullable
Consumer<java.lang.Throwable> dispatchErrorHandler,
@Nullable
Consumer<java.lang.Throwable> uncaughtErrorHandler)
dispatacher and eventRouter.dispatcher - The Dispatcher to use. May be null in which case a new synchronous dispatcher is used.eventRouter - The EventRouter used to route events to Consumers. May be null in which case the
default event router that broadcasts events to all of the registered consumers that match the notification key and does not perform any type conversion will be used.consumerRegistry - The Registry to be used to match Selector and dispatch to Consumer.public java.util.UUID getId()
UUID of this Reactor.UUID of this Reactor.public Registry<Consumer<? extends Event<?>>> getConsumerRegistry()
Registry in use.public Dispatcher getDispatcher()
Dispatcher currently in use.Dispatcher.public EventRouter getEventRouter()
EventRouter used to route events to Consumers.EventRouter.public Consumer<java.lang.Throwable> getDispatchErrorHandler()
public Consumer<java.lang.Throwable> getUncaughtErrorHandler()
public boolean respondsToKey(java.lang.Object key)
ObservablerespondsToKey in interface Observablekey - The key to be matched by Selectorspublic <E extends Event<?>> Registration<Consumer<E>> on(Selector selector, Consumer<E> consumer)
Observableon in interface ObservableE - The type of the Eventselector - The Selector to be used for matchingconsumer - The Consumer to be triggeredRegistration object that allows the caller to interact with the given mappingpublic <E extends Event<?>,V> Registration<Consumer<E>> receive(Selector sel, Function<E,V> fn)
Observablereceive in interface ObservableE - The type of the EventV - The type of the response datasel - The Selector to be used for matchingfn - The transformative Function to call to receive an EventRegistration object that allows the caller to interact with the given mappingpublic <E extends Event<?>> Reactor notify(java.lang.Object key, E ev, Consumer<E> onComplete)
ObservableEvent is ready to be processed and accept onComplete after dispatching.notify in interface ObservableE - The type of the Eventkey - The key to be matched by Selectorsev - The EventonComplete - The callback Consumerpublic <E extends Event<?>> Reactor notify(java.lang.Object key, E ev)
ObservableEvent is ready to be processed.notify in interface ObservableE - The type of the Eventkey - The key to be matched by Selectorsev - The Eventpublic <S extends Supplier<? extends Event<?>>> Reactor notify(java.lang.Object key, S supplier)
ObservableSupplier can provide an event that's ready to be
processed.public Reactor notify(java.lang.Object key)
ObservableSelector that matches the key should be
triggered with a null input argument.notify in interface Observablekey - The key to be matched by Selectorspublic <E extends Event<?>> Reactor send(java.lang.Object key, E ev)
ObservableEvent and register an internal Consumer that will take the
output of a previously-registered Function and respond using the key set on the Event's replyTo property.send in interface ObservableE - The type of the Eventkey - The key to be matched by Selectorsev - The Eventpublic <S extends Supplier<? extends Event<?>>> Reactor send(java.lang.Object key, S supplier)
ObservableSupplier will provide an Event and register an internal Consumer that will take the output of a previously-registered Function and respond using the key set on
the Event's replyTo property.send in interface Observablekey - The key to be matched by Selectorssupplier - The Supplier that will provide the actual Event instancepublic <E extends Event<?>> Reactor send(java.lang.Object key, E ev, Observable replyTo)
ObservableEvent and register an internal Consumer that will take the
output of a previously-registered Function and respond to the key set on the Event's replyTo property and will call the notify method on the given Observable.send in interface ObservableE - The type of the Eventkey - The key to be matched by Selectorsev - The EventreplyTo - The Observable on which to invoke the notify methodpublic <S extends Supplier<? extends Event<?>>> Reactor send(java.lang.Object key, S supplier, Observable replyTo)
ObservableSupplier will provide an Event and register an internal Consumer that will take the output of a previously-registered Function and respond to the key set on the
Event's replyTo property and will call the notify method on the given Observable.send in interface ObservableS - The type of the Supplierkey - The key to be matched by Selectorssupplier - The Supplier that will provide the actual Event instancereplyTo - The Observable on which to invoke the notify methodpublic <REQ extends Event<?>,RESP extends Event<?>> Reactor sendAndReceive(java.lang.Object key, REQ ev, Consumer<RESP> reply)
ObservableConsumer on an anonymous Selector and
set the given event's replyTo property to the corresponding anonymous key, then register the consumer to
receive replies from the Function assigned to handle the given key.sendAndReceive in interface ObservableREQ - The type of the request event.RESP - The type of the response event.key - The key to be matched by Selectorsev - The event to notify.reply - The consumer to register as a reply handler.public <REQ extends Event<?>,RESP extends Event<?>,S extends Supplier<REQ>> Reactor sendAndReceive(java.lang.Object key, S supplier, Consumer<RESP> reply)
ObservableConsumer on an anonymous Selector and
set the event's replyTo property to the corresponding anonymous key, then register the consumer to receive
replies from the Function assigned to handle the given key.sendAndReceive in interface ObservableREQ - The type of the request event.RESP - The type of the response event.S - The type of the supplier.key - The key to be matched by Selectorssupplier - The supplier to supply the event.reply - The consumer to register as a reply handler.public <T> Consumer<Event<T>> prepare(java.lang.Object key)
Observableprepare in interface Observablekey - The key to be matched by SelectorsConsumer to invoke with the Events to publishpublic <T> Consumer<java.lang.Iterable<Event<T>>> batchNotify(java.lang.Object key)
ObservableConsumer. The implementation
will take care of reducing the consumer selection to one per batch. The candidate consumers are selected with the
key , possibly on each batch to refresh the result list.batchNotify in interface Observablekey - The key to be matched by SelectorsConsumer to invoke with the Events to publishpublic <T> Consumer<java.lang.Iterable<Event<T>>> batchNotify(java.lang.Object key, Consumer<java.lang.Void> completeConsumer)
ObservableConsumer. The implementation
will take care of reducing the consumer selection to one per batch. The candidate consumers are selected with the
key , possibly on each batch to refresh the result list.batchNotify in interface Observablekey - The key to be matched by SelectorscompleteConsumer - The consumer to trigger after batch completionConsumer to invoke with the Events to publishpublic boolean equals(java.lang.Object o)
equals in class java.lang.Objectpublic <T> void schedule(Consumer<T> consumer, T data)
Consumer to be executed on the current Reactor Dispatcher, passing the given .T - The type of the data.consumer - The Consumer to invoke.data - The data to pass to the consumer.