Class SnapshottingSourcingHandler<I,E>
- Type Parameters:
I- the type of the entity identifierE- the type of the entity
- All Implemented Interfaces:
DescribableComponent,SourcingHandler<I,E>
SourcingHandler implementation that reconstructs an entity using snapshots
when available, falling back to a full sourcing of events from the EventStore
if necessary.
This handler first attempts to load a snapshot of the entity via the provided Snapshotter.
If a snapshot is present, reconstruction starts from the snapshot's state and position, reducing
the number of events that need to be replayed. If loading the snapshot fails or none exists,
the entity is fully reconstructed from the beginning of its event stream.
All events retrieved from the EventStore are applied sequentially using the
provided InitializingEntityEvolver. After sourcing completes, a snapshot is created
if the SnapshotPolicy indicated one was needed.
- Since:
- 5.1.0
- Author:
- John Hendrikx
-
Constructor Summary
ConstructorsConstructorDescriptionSnapshottingSourcingHandler(EventStore eventStore, CriteriaResolver<I> criteriaResolver, MessageType messageType, SnapshotPolicy snapshotPolicy, Snapshotter<I, E> snapshotter) Creates a newSnapshottingSourcingHandler. -
Method Summary
Modifier and TypeMethodDescriptionvoiddescribeTo(ComponentDescriptor descriptor) Describe the properties ofthis DescribableComponentwith the givendescriptor.source(I identifier, InitializingEntityEvolver<I, E> evolver, ProcessingContext pc) Sources the entity identified by the givenidentifier.
-
Constructor Details
-
SnapshottingSourcingHandler
public SnapshottingSourcingHandler(EventStore eventStore, CriteriaResolver<I> criteriaResolver, MessageType messageType, SnapshotPolicy snapshotPolicy, Snapshotter<I, E> snapshotter) Creates a newSnapshottingSourcingHandler.- Parameters:
eventStore- theEventStoreused to source events, cannot benullcriteriaResolver- the resolver to use to create theEventCriteriafor sourcing, cannot benullmessageType- theMessageType, cannot benullsnapshotPolicy- theSnapshotPolicy, cannot benullsnapshotter- theSnapshotterused to load and handle snapshots, cannot benull- Throws:
NullPointerException- when a non-null argument wasnull
-
-
Method Details
-
source
public CompletableFuture<E> source(I identifier, InitializingEntityEvolver<I, E> evolver, ProcessingContext pc) Description copied from interface:SourcingHandlerSources the entity identified by the givenidentifier.The
InitializingEntityEvolveris used to either create the entity (if it does not exist) or evolve it through the events retrieved from the underlying event stream.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 benullevolver- theInitializingEntityEvolverused to initialize and evolve the entity, 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.
-