Class LocalCommandBus


  • public class LocalCommandBus
    extends Object
    The LocalCommandBus provides an indirection between a command and the CommandHandler that's capable of handling the command.
    Commands can be sent synchronously using send(Object) or asynchronously using sendAsync(Object) that returns a Mono.
    The handling of a command usually doesn't return any value (according to the principles of CQRS), however the LocalCommandBus API allows a CommandHandler to return a value if needed (e.g. such as a server generated id)
    One important rule is that there can only be one CommandHandler instance that can handle any given command type.
    If multiple CommandHandler claim that they all can handle a given command type then send(Object)/sendAsync(Object) will throw MultipleCommandHandlersFoundException
    If no CommandHandler's can handle the given command type then send(Object)/sendAsync(Object) will throw NoCommandHandlerFoundException

    Example:
    
      var 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));
     
     

    In case you need to colocate multiple related command handling methods inside a single class then you should have your command handling class extend AnnotatedCommandHandler.
    Example:
    {@code
     public class OrdersCommandHandler extends AnnotatedCommandHandler {
    See Also:
    AnnotatedCommandHandler
    • Constructor Detail

      • LocalCommandBus

        public LocalCommandBus()
    • 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 type
        C - the command type
        Parameters:
        command - the command to send
        Returns:
        the result of the command processing (if any)
        Throws:
        MultipleCommandHandlersFoundException - If multiple CommandHandler claim that they can handle a given command
        NoCommandHandlerFoundException - If no CommandHandler'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 a Schedulers.boundedElastic() thread
        Type Parameters:
        R - the return type
        C - the command type
        Parameters:
        command - the command to send
        Returns:
        a Mono that will contain the result of the command processing (if any)
        Throws:
        MultipleCommandHandlersFoundException - If multiple CommandHandler claim that they can handle a given command
        NoCommandHandlerFoundException - If no CommandHandler's can handle the given command
      • hasCommandHandler

        public boolean hasCommandHandler​(CommandHandler commandHandler)