Class AnnotationRoutingStrategy

java.lang.Object
org.axonframework.messaging.commandhandling.annotation.AnnotationRoutingStrategy
All Implemented Interfaces:
RoutingStrategy

public class AnnotationRoutingStrategy extends Object implements RoutingStrategy
RoutingStrategy that expects an Command (meta-)annotation on the command message's payload. Commands are routed based on an identifier annotated with the Command. This approach ensures that commands with the same identifier, thus dealt with by the same target, are dispatched to the same node in a distributed Command Bus (e.g. AxonServerCommandBus or DistributedCommandBus).

An example would be the TargetAggregateIdentifier annotation, which is meta-annotated with RoutingKey. See the AnnotationCommandTargetResolver for more details on this approach.

This class requires the returned routing keys to implement a proper Object.toString() method. An inconsistent toString() method may result in different members using different routing keys for the same identifier.

Since:
2.0.0
Author:
Allard Buijze
See Also:
  • Constructor Details

    • AnnotationRoutingStrategy

      public AnnotationRoutingStrategy()
      Instantiate an annotation RoutingStrategy. The annotationType is defaulted to Command.
    • AnnotationRoutingStrategy

      public AnnotationRoutingStrategy(@Nonnull Class<? extends Annotation> annotationType)
      Instantiate an annotation RoutingStrategy. The annotationType attribute routingKey is used to indicate which field to use as routing key.
      Parameters:
      annotationType - The annotation specifying the field to check the payload for.
  • Method Details

    • getRoutingKey

      public String getRoutingKey(@Nonnull CommandMessage command)
      Description copied from interface: RoutingStrategy
      Generates a routing key for the given command.

      Commands that should be handled by the same segment, should result in the same routing key.

      Specified by:
      getRoutingKey in interface RoutingStrategy
      Parameters:
      command - The command to create a routing key for.
      Returns:
      The routing key for the command.