I
- the type of the entity IDs of this repositorypublic final class CommandRouting<I>
extends java.lang.Object
CommandDispatcher
for delivering a command to its handler.
A routing schema consists of a default route and custom routes per command class.
When finding a command target, the CommandRouting
will see if there is a custom route
set for the type of the command. 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.
|
CommandRoute<I,io.spine.base.CommandMessage> |
defaultRoute()
Obtains the default route used by the schema.
|
<M extends io.spine.base.CommandMessage> |
get(java.lang.Class<M> commandClass)
Obtains a route for the passed command class.
|
static <I> CommandRouting<I> |
newInstance(java.lang.Class<I> idClass)
Creates a new command routing.
|
void |
remove(java.lang.Class<? extends M> messageClass)
Removes a route for the passed message class.
|
CommandRouting<I> |
replaceDefault(CommandRoute<I,io.spine.base.CommandMessage> newDefault)
Sets new default route in the schema.
|
<M extends io.spine.base.CommandMessage> |
route(java.lang.Class<M> commandType,
CommandRoute<I,M> via)
Sets a custom route for the passed command type.
|
boolean |
supports(java.lang.Class<? extends M> messageType)
Checks if the passed message type is supported by this instance of routing.
|
public static <I> CommandRouting<I> newInstance(java.lang.Class<I> idClass)
I
- the type of entity identifiers returned by new routingidClass
- the class of target entity identifierspublic final CommandRoute<I,io.spine.base.CommandMessage> defaultRoute()
@CanIgnoreReturnValue public CommandRouting<I> replaceDefault(CommandRoute<I,io.spine.base.CommandMessage> newDefault)
newDefault
- the new route to be used as defaultthis
to allow chained calls when configuring the routing@CanIgnoreReturnValue public <M extends io.spine.base.CommandMessage> CommandRouting<I> route(java.lang.Class<M> commandType, CommandRoute<I,M> via) throws java.lang.IllegalStateException
Such mapping may be required for the following cases:
The type of the command 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(MyCommandClass.class, (event, context) -> { ... })
.route(MyCommandInterface.class, (event, context) -> { ... });
Defining an entry for an interface and then for the class which implements the interface will
result in IllegalStateException
.M
- the type of the command messagecommandType
- the type of the command messagevia
- the route to be used for this type of commandsthis
to allow chained calls when configuring the routingjava.lang.IllegalStateException
- if the route for this command class is already set either directly or
via a super-interfacepublic <M extends io.spine.base.CommandMessage> java.util.Optional<CommandRoute<I,M>> get(java.lang.Class<M> commandClass)
M
- the type of the command messagecommandClass
- the class of the command 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