Annotation Interface EventSourcingHandler


@EventHandler @Documented @Retention(RUNTIME) @Target({METHOD,ANNOTATION_TYPE}) public @interface EventSourcingHandler
Annotation that marks a method in: - an Aggregate (either the root, or an entity) as a handler for Events generated by that aggregate. - a State model needed for CommandHandler as a handler for Events sourced in order to handle given CommandMessage. 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 Event message, unless the payloadType parameter is provided. In that case, the first parameter any other type of parameter described below.
  • 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 metadata of an Event Message injected.
  • Parameters of type Instant annotated with @Timestamp will resolve to the timestamp of the EventMessage. This is the time at which the Event was generated.
  • Parameters assignable to Message will have the entire EventMessage injected (if the message is assignable to that parameter). If the first parameter is of type message, it effectively matches an Event of any type, even if generic parameters would suggest otherwise. 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.
  • When using Spring and <axon:annotation-config/> is declared, any other parameters will resolve to autowired beans, if exactly one autowire candidate is available in the application context. This allows you to inject resources directly into @EventSourcingHandler annotated methods.

An overriding method counts as one handler and runs where the handler it overrides runs; call super to run the overridden one. Separate methods for the same event all run, parent handlers first, each receiving the entity returned by the previous one. Within one class the order is unspecified. If a handler returns an entity the next handler cannot run on, evolving fails with a StateEvolvingException. Handlers of the returned type do not run for the same event.

A handler matches an event by its event name, so a handler accepting Object does not receive other events.

Since:
2.1.0
Author:
Allard Buijze
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    The type of event this method handles.
  • Element Details

    • payloadType

      Class<?> payloadType
      The type of event this method handles. If specified, this handler will only be invoked for message that have a payload assignable to the given payload type.

      Optional. If unspecified, the first parameter of the method defines the type of supported event.

      Returns:
      The type of the event this method handles.
      Default:
      java.lang.Object.class