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>
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 configuredModifiableAggregateRoot.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 theEventPublication
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 activeModifiableAggregateRoot
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:
-
Nested Class Summary
Nested Classes -
Field Summary
Fields inherited from class io.fluxcapacitor.javaclient.modeling.DelegatingEntity
delegate
Fields inherited from interface io.fluxcapacitor.javaclient.modeling.Entity
AGGREGATE_ID_METADATA_KEY, AGGREGATE_SN_METADATA_KEY, AGGREGATE_TYPE_METADATA_KEY, applying, loading
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
ModifiableAggregateRoot
(Entity<T> delegate, boolean commitInBatch, EventPublication eventPublication, EventPublicationStrategy publicationStrategy, EntityHelper entityHelper, Serializer serializer, DispatchInterceptor dispatchInterceptor, ModifiableAggregateRoot.CommitHandler commitHandler) -
Method Summary
Modifier and TypeMethodDescriptionApplies the given message to the entity.assertAndApply
(Object payloadOrMessage) Verifies that the provided update is legal given the current state of the aggregate and on success applies it to the aggregate.assertLegal
(Object update) Verifies that the provided update is legal given the current state of the aggregate.commit()
Commits the current state of the entity, persisting any changes made to it.Collection
<? extends Entity<?>> entities()
Retrieves child entities of this entity.getActiveAggregatesFor
(@NonNull Object entityId) static <T> Optional
<ModifiableAggregateRoot<T>> getIfActive
(Object aggregateId) handleUpdate
(UnaryOperator<Entity<T>> update) 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) previous()
Retrieves the previous version of this entity.update
(UnaryOperator<T> function) Updates the current entity's value using the specified unary operator and returns a new entity containing the updated value.protected void
whenBatchCompletes
(Throwable error) protected void
whenHandlerCompletes
(Throwable error) Methods inherited from class io.fluxcapacitor.javaclient.modeling.DelegatingEntity
aliases, get, id, idProperty, lastEventId, lastEventIndex, parent, sequenceNumber, timestamp, type, withEventIndex, withSequenceNumber, withType
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.fluxcapacitor.javaclient.modeling.AggregateRoot
lastEventId, lastEventIndex, parent, sequenceNumber, timestamp, withEventIndex, withSequenceNumber
Methods inherited from interface io.fluxcapacitor.javaclient.modeling.Entity
aliases, allEntities, ancestors, ancestorValue, apply, apply, apply, apply, apply, assertAndApply, assertAndApply, assertAndApply, associations, depth, dissociations, get, getEntity, highestEventIndex, id, idProperty, ifPresent, isEmpty, isPresent, isRoot, mapIfPresent, playBackToCondition, playBackToEvent, possibleTargets, relationships, root, rootAnnotation, type, withType
-
Constructor Details
-
ModifiableAggregateRoot
protected ModifiableAggregateRoot(Entity<T> delegate, boolean commitInBatch, EventPublication eventPublication, EventPublicationStrategy publicationStrategy, EntityHelper entityHelper, Serializer serializer, DispatchInterceptor dispatchInterceptor, ModifiableAggregateRoot.CommitHandler commitHandler)
-
-
Method Details
-
getIfActive
-
getActiveAggregatesFor
-
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
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 interfaceEntity<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
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 interfaceEntity<T>
- Parameters:
payloadOrMessage
- the input object to be applied; can be a payload or a message- Returns:
- the resulting entity after application
-
update
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. -
apply
Description copied from interface:Entity
Applies the given message to the entity. -
apply
-
handleUpdate
-
whenHandlerCompletes
-
whenBatchCompletes
-
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. -
entities
Description copied from interface:Entity
Retrieves child entities of this entity. -
previous
Description copied from interface:Entity
Retrieves the previous version of this entity.- Specified by:
previous
in interfaceAggregateRoot<T>
- Specified by:
previous
in interfaceEntity<T>
- Overrides:
previous
in classDelegatingEntity<T>
- Returns:
- the previous state of the entity, or null if this is the first known version
-