Class SimpleEventHandlingComponent

java.lang.Object
org.axonframework.messaging.eventhandling.SimpleEventHandlingComponent
All Implemented Interfaces:
DescribableComponent, MessageHandler, EventHandler, EventHandlerRegistry<SimpleEventHandlingComponent>, EventHandlingComponent, ReplayStatusChangedHandler, ReplayStatusChangedHandlerRegistry<SimpleEventHandlingComponent>, ResetHandler, ResetHandlerRegistry<SimpleEventHandlingComponent>

Simple implementation of the EventHandlingComponent, EventHandlerRegistry, and ResetHandlerRegistry.

As such, it contains a collection of EventHandlers to invoke on handle, and a set of ResetHandlers to invoke on reset.

Since:
5.0.0
Author:
Steven van Beelen
  • Constructor Details

  • Method Details

    • create

      public static SimpleEventHandlingComponent create(String name)
      Instantiates a simple EventHandlingComponent that is able to handle events and delegate them to subcomponents.

      Uses a default sequencing policy that will first try for the SequentialPerAggregatePolicy, falling back to the SequentialPolicy when the former returns no sequence value.

      Parameters:
      name - The name of the component, used for describing the component.
      Returns:
      A simple EventHandlingComponent instance with the given name.
    • create

      public static SimpleEventHandlingComponent create(String name, SequencingPolicy<? super EventMessage> sequencingPolicy)
      Instantiates a simple EventHandlingComponent that is able to handle events and delegate them to subcomponents, using the given sequencingPolicy to decide how to sequence incoming events.
      Parameters:
      name - The name of the component, used for describing the component.
      sequencingPolicy - The SequencingPolicy to use for sequencing events through, for example, sequenceIdentifierFor(EventMessage, ProcessingContext).
      Returns:
      A simple EventHandlingComponent instance with the given name.
    • handle

      public MessageStream.Empty<Message> handle(EventMessage event, ProcessingContext context)
      Description copied from interface: EventHandler
      Handles the given event within the given context.

      The result of handling is an empty stream.

      Specified by:
      handle in interface EventHandler
      Parameters:
      event - The event to handle.
      context - The context to the given event is handled in.
      Returns:
      An empty stream containing nothing.
    • subscribe

      public SimpleEventHandlingComponent subscribe(Set<QualifiedName> names, EventHandler handler)
      Description copied from interface: EventHandlerRegistry
      Subscribe the given handler for events of the given names.

      If a subscription already exists for any name in the given set, the behavior is undefined. Implementations may throw an exception to refuse duplicate subscription or alternatively decide whether the existing or new handler gets the subscription.

      Specified by:
      subscribe in interface EventHandlerRegistry<SimpleEventHandlingComponent>
      Parameters:
      names - The names of the given commandHandler can handle.
      handler - The handler instance that handles events for the given names.
      Returns:
      This registry for fluent interfacing.
    • subscribe

      public SimpleEventHandlingComponent subscribe(QualifiedName name, EventHandler handler)
      Description copied from interface: EventHandlerRegistry
      Subscribe the given handler for events of the given name.

      If a subscription already exists for the name, the behavior is undefined. Implementations may throw an exception to refuse duplicate subscription or alternatively decide whether the existing or new handler gets the subscription.

      Specified by:
      subscribe in interface EventHandlerRegistry<SimpleEventHandlingComponent>
      Parameters:
      name - The name the given commandHandler can handle.
      handler - The handler instance that handles events for the given name.
      Returns:
      This registry for fluent interfacing.
    • supportedEvents

      public Set<QualifiedName> supportedEvents()
      Description copied from interface: EventHandlingComponent
      All supported events, referenced through a QualifiedName.
      Specified by:
      supportedEvents in interface EventHandlingComponent
      Returns:
      All supported events, referenced through a QualifiedName.
    • sequenceIdentifierFor

      public Object sequenceIdentifierFor(EventMessage event, ProcessingContext context)
      Returns the sequence identifier for the given event. When two events have the same sequence identifier (as defined by their equals method), they will be executed sequentially. Important: All EventHandlers for the same QualifiedName within a single EventHandlingComponent must return the same sequence identifier for a given event. Mixing different sequence identifiers within the scope of a single EventHandlingComponent is not supported and may lead to undefined behavior.

      The implementation automatically chooses the appropriate sequencing policy based on the event context:

      • If the aggregate identifier is present in the ProcessingContext (i.e., the event is an aggregate event), it uses SequentialPerAggregatePolicy to ensure events for the same aggregate are processed sequentially while allowing concurrent processing of events from different aggregates.
      • If no aggregate identifier is present (i.e., the event is not an aggregate event), it uses SequentialPolicy which ensures all events are processed sequentially (no concurrency) as the safest default option.

      Override this method to provide custom sequencing behavior. Or use a SequenceOverridingEventHandlingComponent if you cannot inherit from a certain EventHandlingComponent implementation.

      Specified by:
      sequenceIdentifierFor in interface EventHandlingComponent
      Parameters:
      event - The event for which to get the sequencing identifier.
      context - The processing context in which the event is being handled.
      Returns:
      A sequence identifier for the given event.
    • handle

      public MessageStream.Empty<Message> handle(ResetContext resetContext, ProcessingContext context)
      Description copied from interface: ResetHandler
      Handles the given ResetContext message, performing any necessary reset operations.

      This method is invoked before the processor begins replaying events. Handlers typically use this opportunity to clean up state that will be rebuilt during replay.

      If this method completes exceptionally, the reset operation will be aborted and no replay will occur.

      Specified by:
      handle in interface ResetHandler
      Parameters:
      resetContext - The reset context message containing reset information and optional payload.
      context - The processing context for this operation.
      Returns:
      An empty message stream after handling completes successfully.
    • subscribe

      public SimpleEventHandlingComponent subscribe(ResetHandler resetHandler)
      Description copied from interface: ResetHandlerRegistry
      Subscribes a reset handler to this registry. The handler will be invoked when a reset operation is triggered.

      Multiple handlers can be subscribed, and all will be invoked during reset.

      Specified by:
      subscribe in interface ResetHandlerRegistry<SimpleEventHandlingComponent>
      Parameters:
      resetHandler - the reset handler to subscribe, must not be null
      Returns:
      This registry instance for method chaining.
    • handle

      public MessageStream.Empty<Message> handle(ReplayStatusChanged statusChange, ProcessingContext context)
      Description copied from interface: ReplayStatusChangedHandler
      Handles the given ReplayStatusChanged message, allowing for tasks to be performed when the replay starts and ends.

      This method is invoked on the moment the ReplayStatus is about to change as part of the event handling ProcessingContext. In doing so, this handler has two concrete moments when it is invoked:

      1. When the ReplayStatus changes from ReplayStatus.REGULAR to ReplayStatus.REPLAY, exactly before the first replayed event is processed
      2. When the ReplayStatus changes from ReplayStatus.REPLAY to ReplayStatus.REGULAR, exactly after processing the final event of the replay

      If this operation returns a failed MessageStream, event handling that occurs within the given context is impacted. The failure will be passed to the ErrorHandler, typically resulting in a rollback of the invoked event handling tasks.

      Specified by:
      handle in interface EventHandlingComponent
      Specified by:
      handle in interface ReplayStatusChangedHandler
      Parameters:
      statusChange - the replay status context message containing replay status information
      context - the processing context for this operation
      Returns:
      an empty message stream after handling completes successfully
    • subscribe

      public SimpleEventHandlingComponent subscribe(ReplayStatusChangedHandler replayStatusChangedHandler)
      Description copied from interface: ReplayStatusChangedHandlerRegistry
      Subscribes a replay status changed handler to this registry.

      The handler will be invoked when the ReplayStatus changed. Multiple handlers can be subscribed, and all will be invoked when the replay status changes.

      Specified by:
      subscribe in interface ReplayStatusChangedHandlerRegistry<SimpleEventHandlingComponent>
      Parameters:
      replayStatusChangedHandler - the replay status changed handler to subscribe, must not be null
      Returns:
      this registry instance for method chaining
    • describeTo

      public void describeTo(ComponentDescriptor descriptor)
      Description copied from interface: DescribableComponent
      Describe the properties of this DescribableComponent with the given descriptor.

      Components should call the appropriate describeProperty methods on the descriptor to register their properties. The descriptor is responsible for determining how these properties are formatted and structured in the final output.

      Best Practices: As a general rule, all relevant fields of a DescribableComponent implementation should be described in this method. However, developers have discretion to include only the fields that make sense in the context. Not every field may be meaningful for description purposes, especially internal implementation details. Furthermore, components might want to expose different information based on their current state. The final decision on what properties to include lies with the person implementing the describeTo method, who should focus on providing information that is useful for understanding the component's configuration and state.

      Example implementation:

       public void describeTo(ComponentDescriptor descriptor) {
           descriptor.describeProperty("name", this.name);
           descriptor.describeProperty("enabled", this.enabled);
           descriptor.describeProperty("configuration", this.configuration); // A nested component
           descriptor.describeProperty("handlers", this.eventHandlers);      // A collection
       }
       
      Specified by:
      describeTo in interface DescribableComponent
      Parameters:
      descriptor - The component descriptor to describe this DescribableComponentn its properties in.