T H E C O M M A N D B U S @Component public class CommandBus { @Autowired public CommandBus(
Collection<? extends CommandHandler> handlers, EventBus eventBus) { //… this.eventBus = eventBus; } public <R, C extends Command<R>> Try<R> dispatch(C command) { return handlers.get(command.getClass()) .map(h -> execute(h, command)) .getOrElse(() -> Try.failure( new HandlerNotFoundException(command))); } private <R, C extends Command<R>> Try<R> execute(
CommandHandler<R, C> h, C command) { return Try.of(() -> { Tuple2<R, List<Event>> result = h.handle(command); result._2.forEach(eventBus::publish); return result._1; }); } }