Annotation Interface CommandHandler


Annotation to be placed on methods that can handle commands, thus making them CommandHandlers.

Command handler annotated methods are typically subscribed with a CommandBus as part of an AnnotatedCommandHandlingComponent. This annotation can also be placed directly on entity children (read: an entity contained in another entity) to have it handle the commands directly.

The parameters of the annotated method are resolved using parameter resolvers. Axon provides a number of parameter resolvers that allow you to use the following parameter types:

  • The first parameter is always the payload of the CommandMessage.
  • Parameters annotated with MetadataValue will resolve to the Metadata value with the key as indicated on the annotation. If required is false (default), null is passed when the metadata value is not present. If required is true, the resolver will not match and prevent the method from being invoked when the metadata value is not present.
  • Parameters of type Metadata will have the entire command message metadata injected.
  • Parameters assignable to Message will have the entire CommandMessage injected (if the message is assignable to that parameter). If the first parameter is of type message, it effectively matches a command of any type. Due to type erasure, Axon cannot detect what parameter is expected. In such case, it is best to declare a parameter of the payload type, followed by a parameter of type Message.
  • A parameter of type ProcessingContext will inject the active processing context at that moment in time.
Since:
0.5.0
Author:
Allard Buijze
  • Element Details

    • commandName

      String commandName
      The name of the command this handler listens to, matching to the MessageType.name() from the Message.type().

      When not defined the MessageTypeResolver will derive the name based on the payload type (thus the first parameter) of the annotated method.

      Returns:
      The name of the command this handler listens to, matching to the MessageType.name() from the Message.type().
      Default:
      ""
    • routingKey

      String routingKey
      The property of the command to be used as a routing key towards this command handler instance.

      If multiple handlers instances are available, a sending component is responsible to route commands with the same routing key value to the correct instance.

      Returns:
      The property of the command to use as routing key.
      Default:
      ""
    • payloadType

      Class<?> payloadType
      The type of payload expected by this handler.

      Defaults to the expected types expresses by (primarily the first) parameters of the annotated method.

      Returns:
      The payload type expected by this handler.
      Default:
      java.lang.Object.class