Class LocalCommandBus
- java.lang.Object
-
- dk.cloudcreate.essentials.reactive.command.LocalCommandBus
-
public class LocalCommandBus extends Object
TheLocalCommandBusprovides an indirection between a command and theCommandHandlerthat's capable of handling the command.
Commands can be sent synchronously usingsend(Object)or asynchronously usingsendAsync(Object)that returns aMono.
The handling of a command usually doesn't return any value (according to the principles of CQRS), however theLocalCommandBusAPI allows aCommandHandlerto return a value if needed (e.g. such as a server generated id)
One important rule is that there can only be oneCommandHandlerinstance that can handle any given command type.
If multipleCommandHandlerclaim that they all can handle a given command type thensend(Object)/sendAsync(Object)will throwMultipleCommandHandlersFoundException
If noCommandHandler's can handle the given command type thensend(Object)/sendAsync(Object)will throwNoCommandHandlerFoundException
Example:
In case you need to colocate multiple related command handling methods inside a single class then you should have your command handling class extendvar commandBus = new LocalCommandBus(); commandBus.addCommandHandler(new CreateOrderCommandHandler(...)); commandBus.addCommandHandler(new ImburseOrderCommandHandler(...)); var optionalResult = commandBus.send(new CreateOrder(...)); // or var monoWithOptionalResult = commandBus.sendAsync(new ImbuseOrder(...)) .block(Duration.ofMillis(1000));AnnotatedCommandHandler.
Example:{@code public class OrdersCommandHandler extends AnnotatedCommandHandler {- See Also:
AnnotatedCommandHandler
-
-
Constructor Summary
Constructors Constructor Description LocalCommandBus()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description LocalCommandBusaddCommandHandler(CommandHandler commandHandler)Add a new command handler.protected CommandHandlerfindCommandHandlerCapableOfHandling(Object command)Find aCommandHandlercapable of handling the given commandLocalCommandBusremoveCommandHandler(CommandHandler commandHandler)Remove a command handler.<R,C>
Rsend(C command)The send command synchronously and process the command on the sending thread<R,C>
reactor.core.publisher.Mono<R>sendAsync(C command)The send command asynchronously and process the command on aSchedulers.boundedElastic()thread
-
-
-
Method Detail
-
addCommandHandler
public LocalCommandBus addCommandHandler(CommandHandler commandHandler)
Add a new command handler.- Parameters:
commandHandler- the command handler to add- Returns:
- this bus instance
-
removeCommandHandler
public LocalCommandBus removeCommandHandler(CommandHandler commandHandler)
Remove a command handler.- Parameters:
commandHandler- the command handler to remove- Returns:
- this bus instance
-
send
public <R,C> R send(C command)
The send command synchronously and process the command on the sending thread- Type Parameters:
R- the return typeC- the command type- Parameters:
command- the command to send- Returns:
- the result of the command processing (if any)
- Throws:
MultipleCommandHandlersFoundException- If multipleCommandHandlerclaim that they can handle a given commandNoCommandHandlerFoundException- If noCommandHandler's can handle the given command
-
sendAsync
public <R,C> reactor.core.publisher.Mono<R> sendAsync(C command)
The send command asynchronously and process the command on aSchedulers.boundedElastic()thread- Type Parameters:
R- the return typeC- the command type- Parameters:
command- the command to send- Returns:
- a
Monothat will contain the result of the command processing (if any) - Throws:
MultipleCommandHandlersFoundException- If multipleCommandHandlerclaim that they can handle a given commandNoCommandHandlerFoundException- If noCommandHandler's can handle the given command
-
findCommandHandlerCapableOfHandling
protected CommandHandler findCommandHandlerCapableOfHandling(Object command)
Find aCommandHandlercapable of handling the given command- Parameters:
command- the command to handle- Returns:
- the single
CommandHandlerthat's capable of handling the given command - Throws:
MultipleCommandHandlersFoundException- If multipleCommandHandlerclaim that they can handle a given commandNoCommandHandlerFoundException- If noCommandHandler's can handle the given command
-
-