Class SnapshottingEntityLifecycleHandler<I,E>
- Type Parameters:
I- the type of the entity identifierE- the type of the entity
- All Implemented Interfaces:
DescribableComponent,EntityLifecycleHandler<I,,E> SourcingHandler<I,E>
EntityLifecycleHandler implementation that reconstructs an entity from the
EventStore, using snapshots to improve efficiency when available.
When sourcing an entity, this handler will use a previously stored snapshot as a starting point if one is available and compatible. If no suitable snapshot is found, the entity is reconstructed by replaying its full event stream.
All events are applied in order using the configured InitializingEntityEvolver to rebuild
the entity's state.
After reconstruction, a new snapshot may be stored in the SnapshotStore, depending on the
configured SnapshotPolicy. This policy determines whether snapshot creation is desirable
based on the characteristics of the sourcing operation or the events encountered.
If an existing snapshot cannot be used, the handler will automatically fall back to full reconstruction to ensure the entity can always be loaded.
- Since:
- 5.1.0
- Author:
- John Hendrikx
-
Constructor Summary
ConstructorsConstructorDescriptionSnapshottingEntityLifecycleHandler(EventStore eventStore, CriteriaResolver<I> criteriaResolver, InitializingEntityEvolver<I, E> evolver, SnapshotPolicy snapshotPolicy, MessageType messageType, Converter converter, Class<?> entityType, SnapshotStore snapshotStore) Constructs a new instance. -
Method Summary
Modifier and TypeMethodDescriptionvoiddescribeTo(ComponentDescriptor descriptor) Describe the properties ofthis DescribableComponentwith the givendescriptor.initialize(I identifier, ProcessingContext context) Creates a new instance of the entity for the given identifier.source(I identifier, ProcessingContext pc) Sources the entity identified by the givenidentifier.voidsubscribe(ManagedEntity<I, E> entity, ProcessingContext context) Subscribes the given managed entity to the event stream so it receives future state changes.
-
Constructor Details
-
SnapshottingEntityLifecycleHandler
public SnapshottingEntityLifecycleHandler(EventStore eventStore, CriteriaResolver<I> criteriaResolver, InitializingEntityEvolver<I, E> evolver, SnapshotPolicy snapshotPolicy, MessageType messageType, Converter converter, Class<?> entityType, SnapshotStore snapshotStore) Constructs a new instance.- Parameters:
eventStore- theEventStoreused to source events, cannot benullcriteriaResolver- the resolver to use to create theEventCriteriafor sourcing, cannot benullevolver- theInitializingEntityEvolverused to initialize and evolve the entity, cannot benullsnapshotPolicy- theSnapshotPolicy, cannot benullmessageType- theMessageType, cannot benullconverter- theConverterto use to decode snapshots, cannot benullentityType- the type a snapshot should decode to, cannot benullsnapshotStore- theSnapshotStoreto use for storing snapshots, cannot benull- Throws:
NullPointerException- when a non-null argument wasnull
-
-
Method Details
-
initialize
Description copied from interface:EntityLifecycleHandlerCreates a new instance of the entity for the given identifier.This operation is used when no prior state exists for the entity. The resulting instance represents the initial state before any events have been applied.
- Specified by:
initializein interfaceEntityLifecycleHandler<I,E> - Parameters:
identifier- the entity identifier, cannot benullcontext- the processing context, cannot benull- Returns:
- a newly initialized entity instance
-
subscribe
Description copied from interface:EntityLifecycleHandlerSubscribes the given managed entity to the event stream so it receives future state changes.After subscription, any newly appended events relevant to the entity are applied to its current state, keeping it synchronized with the event store for the remainder of its lifecycle.
- Specified by:
subscribein interfaceEntityLifecycleHandler<I,E> - Parameters:
entity- the managed entity to subscribecontext- the processing context
-
source
Description copied from interface:SourcingHandlerSources the entity identified by the givenidentifier.This method returns a
CompletableFuturethat completes when the entity has been fully reconstructed or evolved to its latest state.- Specified by:
sourcein interfaceSourcingHandler<I,E> - Parameters:
identifier- the identifier of the entity to source, cannot benullpc- theProcessingContextassociated with this sourcing operation, cannot benull- Returns:
- a
CompletableFuturethat completes with the sourced entity, nevernull
-
describeTo
Description copied from interface:DescribableComponentDescribe the properties ofthis DescribableComponentwith the givendescriptor.Components should call the appropriate
describePropertymethods 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
DescribableComponentimplementation 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 thedescribeTomethod, 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:
describeToin interfaceDescribableComponent- Parameters:
descriptor- The component descriptor to describethis DescribableComponentn its properties in.
-