Class ModifiableAggregateRoot<T>

java.lang.Object
io.fluxcapacitor.javaclient.modeling.DelegatingEntity<T>
io.fluxcapacitor.javaclient.modeling.ModifiableAggregateRoot<T>
Type Parameters:
T - the type of the aggregate's value.
All Implemented Interfaces:
AggregateRoot<T>, Entity<T>

public class ModifiableAggregateRoot<T> extends DelegatingEntity<T> implements AggregateRoot<T>
A mutable, stateful AggregateRoot implementation that allows in-place updates and applies events with commit support for persisting the state and synchronizing it with the Flux platform.

This class acts as a wrapper around an immutable entity (typically an ImmutableAggregateRoot), providing additional lifecycle management for:

  • Capturing uncommitted changes during an event or command handler execution.
  • Intercepting and applying updates and events with legality assertions.
  • Buffering applied events and committing them via a ModifiableAggregateRoot.CommitHandler callback.
  • Supporting batch-commit semantics for grouped event processing.
  • Tracking active aggregates for correlation and relationship resolution during handlers.

Commit Lifecycle

During handler invocation, intercepted updates and applied events are stored in a temporary buffer. When the handler completes successfully, events are committed via the configured ModifiableAggregateRoot.CommitHandler, which typically stores events in the EventStore and publishes them if needed.

If the handler fails (throws), state changes and captured events are discarded, and the aggregate is rolled back to the last stable state.

Publication Strategy

Events are published according to the EventPublication and EventPublicationStrategy specified either globally or on individual Apply-annotated methods. This allows fine-grained control over which events are stored or published.

Thread-Scoped Aggregates

All active ModifiableAggregateRoot instances are tracked per thread using a thread-local map, enabling relationship resolution and update tracking across aggregates within the same handler context.

This mechanism enables other parts of the framework to determine which aggregates are currently in use or updated within a processing thread, without requiring external state.

See Also:
  • Constructor Details

  • Method Details

    • getIfActive

      public static <T> Optional<ModifiableAggregateRoot<T>> getIfActive(Object aggregateId)
    • getActiveAggregatesFor

      public static Map<String,Class<?>> getActiveAggregatesFor(@NonNull @NonNull Object entityId)
    • load

      public static <T> Entity<T> load(Object aggregateId, Supplier<Entity<T>> loader, boolean commitInBatch, EventPublication eventPublication, EventPublicationStrategy publicationStrategy, EntityHelper entityHelper, Serializer serializer, DispatchInterceptor dispatchInterceptor, ModifiableAggregateRoot.CommitHandler commitHandler)
    • assertLegal

      public <E extends Exception> Entity<T> assertLegal(Object update) throws E
      Description copied from interface: Entity
      Verifies that the provided update is legal given the current state of the aggregate. If so, the entity is returned; otherwise, it throws an appropriate exception.
      Specified by:
      assertLegal in interface Entity<T>
      Type Parameters:
      E - the type of exception expected if the update is not legal
      Parameters:
      update - the update to be validated for compliance with the required rules
      Returns:
      the entity if the update is legal
      Throws:
      E - if the update fails to meet legal requirements
    • assertAndApply

      public Entity<T> assertAndApply(Object payloadOrMessage)
      Description copied from interface: Entity
      Verifies that the provided update is legal given the current state of the aggregate and on success applies it to the aggregate. If not, it throws an appropriate exception.
      Specified by:
      assertAndApply in interface Entity<T>
      Parameters:
      payloadOrMessage - the input object to be applied; can be a payload or a message
      Returns:
      the resulting entity after application
    • update

      public Entity<T> update(UnaryOperator<T> function)
      Description copied from interface: Entity
      Updates the current entity's value using the specified unary operator and returns a new entity containing the updated value.
      Specified by:
      update in interface Entity<T>
      Parameters:
      function - the unary operator to apply to the current entity's value
      Returns:
      a new entity containing the updated value after applying the function
    • apply

      public Entity<T> apply(Message message)
      Description copied from interface: Entity
      Applies the given message to the entity.
      Specified by:
      apply in interface Entity<T>
      Parameters:
      message - the message representing the event to be applied
      Returns:
      the updated or newly created Entity of type T
    • apply

      protected Entity<T> apply(Message message, boolean assertLegal)
    • handleUpdate

      protected Entity<T> handleUpdate(UnaryOperator<Entity<T>> update)
    • whenHandlerCompletes

      protected void whenHandlerCompletes(Throwable error)
    • whenBatchCompletes

      protected void whenBatchCompletes(Throwable error)
    • commit

      public Entity<T> commit()
      Description copied from interface: Entity
      Commits the current state of the entity, persisting any changes made to it. This method ensures that the modifications are saved. Typically, it is unnecessary to invoke this manually as it is automatically invoked after the current handler or consumer batch has completed.
      Specified by:
      commit in interface Entity<T>
      Returns:
      The updated entity after the commit operation is successfully completed.
    • entities

      public Collection<? extends Entity<?>> entities()
      Description copied from interface: Entity
      Retrieves child entities of this entity.
      Specified by:
      entities in interface Entity<T>
      Overrides:
      entities in class DelegatingEntity<T>
      Returns:
      a collection containing child entities.
    • previous

      public Entity<T> previous()
      Description copied from interface: Entity
      Retrieves the previous version of this entity.
      Specified by:
      previous in interface AggregateRoot<T>
      Specified by:
      previous in interface Entity<T>
      Overrides:
      previous in class DelegatingEntity<T>
      Returns:
      the previous state of the entity, or null if this is the first known version