Skip to content

Command Router

The framework module provides CommandRouter as the core component responsible for command execution. Command execution can be triggered via one of the send() methods, providing a Command instance and an optional command meta-data map. It comprises the following steps:

  1. routing the Command instance to the responsible CommandHandler by means of a suitable CommandHandlerDefinition
  2. reading the relevant, upcasted Java event objects using the event repository
  3. reconstructing the command execution state using the StateRebuildingHandler instances defined within the configured StateRebuildingHandlerDefinition instances
  4. optionally caching the reconstructed state for future command executions
  5. executing the CommandHandler and capturing any Java object events published via the CommandEventPublisher and the optional command result (or exception)
  6. publishing the captured events atomically using the event repository with suitable preconditions to avoid inconsistent writes due to race conditions

Configuration

An instance of CommandRouter can be obtained, either by manually instantiating it or using Spring Boot autoconfiguration.

Manual Configuration

An instance of CommandRouter can be created providing the necessary configuration properties:

The following example shows, how to instantiate a CommandRouter using a provided EventRepository, a list of CommandHandlerDefinition (containing the command handling logic), and a list of StateRebuildingHandlerDefinition (containing the state rebuilding logic).

package com.opencqrs.framework.command;

import com.opencqrs.framework.persistence.EventRepository;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class CommandRouterConfiguration {

    public static CommandRouter commandRouter(
            EventRepository eventRepository,
            List<CommandHandlerDefinition> commandHandlerDefinitions,
            List<StateRebuildingHandlerDefinition> stateRebuildingHandlerDefinitions
    ) {
        return new CommandRouter(
                eventRepository,
                eventRepository,
                commandHandlerDefinitions,
                stateRebuildingHandlerDefinitions
        );
    }
}

The correct configuration can be confirmed, by sending a suitable command (covered by any of the supplied CommandHandlerDefinition) as follows:

public static void main(String[] args){
    var commandRouter = commandRouter(...);

    commandRouter.send(new MyCommand(42L)); // MyCommand class omitted for brevity
}

Spring Boot Auto-Configuration

For Spring Boot applications using the framework-spring-boot-starter module CommandRouterAutoConfiguration provides a fully configured CommandRouter Spring bean. The bean can be configured with respect to instance caching and meta-data propagation using the properties defined within CommandHandlingCacheProperties and MetaDataPropagationProperties, respectivly, e.g. as follows:

opencqrs.command-handling.cache.type=in_memory
opencqrs.metadata.propagation.keys=requestId

With that configuration in place the autoconfigured CommandRouter instance can be auto-wired within any other Spring bean, if needed. The configuration can be further customized by: