I
- the type of the entity IDs to which events are routedpublic final class EventRouting<I> extends java.lang.Object implements EventRoute<I,io.spine.base.EventMessage>
A routing schema consists of a default route and custom routes per event class.
When calculating a set of event targets, EventRouting
will see if there is
a custom route set for the type of the event. If not found, the default route will be
applied.
Modifier and Type | Method and Description |
---|---|
R |
apply(M message,
C context)
Obtains IDs of entities to which the passed message should be delivered.
|
EventRoute<I,io.spine.base.EventMessage> |
defaultRoute()
Obtains the default route used by the schema.
|
<M extends io.spine.base.EventMessage> |
get(java.lang.Class<M> eventClass)
Obtains a route for the passed event class.
|
void |
remove(java.lang.Class<? extends M> messageClass)
Removes a route for the passed message class.
|
EventRouting<I> |
replaceDefault(EventRoute<I,io.spine.base.EventMessage> newDefault)
Sets new default route in the schema.
|
<E extends io.spine.base.EventMessage> |
route(java.lang.Class<E> eventType,
EventRoute<I,? super E> via)
Sets a custom route for the passed event type.
|
EventRouting<I> |
routeStateUpdates(StateUpdateRouting<I> routing)
Sets a custom routing schema for entity state updates.
|
boolean |
supports(java.lang.Class<? extends M> messageType)
Checks if the passed message type is supported by this instance of routing.
|
<E extends io.spine.base.EventMessage> |
unicast(java.lang.Class<E> eventType,
java.util.function.BiFunction<E,io.spine.core.EventContext,I> via)
Sets a custom route for the passed event type by obtaining the target entity
ID from the passed function over event message and its context.
|
<E extends io.spine.base.EventMessage> |
unicast(java.lang.Class<E> eventType,
java.util.function.Function<E,I> via)
Sets a custom route for the passed event type by obtaining the target entity
ID from the passed function.
|
static <I> EventRouting<I> |
withDefault(EventRoute<I,io.spine.base.EventMessage> defaultRoute)
Creates a new event routing with the passed default route.
|
static <I> EventRouting<I> |
withDefaultByProducerId()
Creates a new event routing with the default one by event producer ID.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
byFirstMessageField, byProducerId, noTargets, withId
@CanIgnoreReturnValue public static <I> EventRouting<I> withDefault(EventRoute<I,io.spine.base.EventMessage> defaultRoute)
I
- the type of entity identifiers returned by new routingdefaultRoute
- the default routepublic static <I> EventRouting<I> withDefaultByProducerId()
public final EventRoute<I,io.spine.base.EventMessage> defaultRoute()
Overrides for return type covariance.
@CanIgnoreReturnValue public EventRouting<I> replaceDefault(EventRoute<I,io.spine.base.EventMessage> newDefault)
newDefault
- the new route to be used as defaultthis
to allow chained calls when configuring the routing@CanIgnoreReturnValue public <E extends io.spine.base.EventMessage> EventRouting<I> route(java.lang.Class<E> eventType, EventRoute<I,? super E> via) throws java.lang.IllegalStateException
Such mapping may be required for the following cases:
<I>
.
The type of the event can be a class or an interface. If a routing schema needs to contain entries for specific classes and an interface that these classes implement, routes for interfaces should be defined after entries for the classes:
customRouting.route(MyEventClass.class, (event, context) -> { ... })
.route(MyEventInterface.class, (event, context) -> { ... });
Defining an entry for an interface and then for the class which implements the interface will
result in IllegalStateException
.
If there is no specific route for an event type, the default route will be used.
E
- the type of the event messageeventType
- the type of events to routevia
- the instance of the route to be usedthis
to allow chained calls when configuring the routingjava.lang.IllegalStateException
- if the route for this event type is already set either directly or
via a super-interface@CanIgnoreReturnValue public <E extends io.spine.base.EventMessage> EventRouting<I> unicast(java.lang.Class<E> eventType, java.util.function.Function<E,I> via)
This is a convenience method for configuring routing when an event is to be delivered to only one entity which ID is calculated from the event message. A simplest case of that would be passing a method reference for an accessor of a field of the event message which contains the ID of interest.
E
- the type of the event messageeventType
- the type of the event to routevia
- the function for obtaining the target entity IDthis
to allow chained calls when configuring routingEventRouting.route(Class, EventRoute)
,
EventRouting.unicast(Class, BiFunction)
@CanIgnoreReturnValue public <E extends io.spine.base.EventMessage> EventRouting<I> unicast(java.lang.Class<E> eventType, java.util.function.BiFunction<E,io.spine.core.EventContext,I> via)
This is a convenience method for configuring routing when an event is to be delivered to only one entity which ID is calculated from the event message and its context.
E
- the type of the event messageeventType
- the type of the event to routevia
- the supplier of the target entity IDthis
to allow chained calls when configuring routingEventRouting.route(Class, EventRoute)
,
EventRouting.unicast(Class, Function)
@CanIgnoreReturnValue public EventRouting<I> routeStateUpdates(StateUpdateRouting<I> routing)
routing
- the routing schema for entity state updatesthis
to allow chained calls when configuring the routingjava.lang.IllegalStateException
- if a route for EntityStateChanged
is already setpublic <M extends io.spine.base.EventMessage> java.util.Optional<EventRoute<I,M>> get(java.lang.Class<M> eventClass)
M
- the type of the event messageeventClass
- the class of the event messagespublic boolean supports(java.lang.Class<? extends M> messageType)
public void remove(java.lang.Class<? extends M> messageClass)
java.lang.IllegalStateException
- if a custom route for this message class was not previously setpublic R apply(M message, C context)
If there is no function for the passed message applies the default function.
apply
in interface Route<M extends com.google.protobuf.Message,C extends io.spine.base.MessageContext,R>
apply
in interface java.util.function.BiFunction<M extends com.google.protobuf.Message,C extends io.spine.base.MessageContext,R>
message
- the messagecontext
- the message context