Class AnnotatedSagaRepository<T>
- Type Parameters:
T- generic type specifying the Saga type stored by thisSagaRepository
- All Implemented Interfaces:
SagaRepository<T>
SagaRepository implementation extending from the LockingSagaRepository dealing with annotated Sagas.
Will take care of the uniqueness of Saga instances in the JVM. That means it will prevent multiple instances
of the same conceptual Saga (i.e. with same identifier) to exist within the JVM.
A Saga loaded or created here is written back to the SagaStore in the WRITE_SAGA phase of the
ProcessingContext it was requested in, which is what makes the write part of the caller's transaction.
- Since:
- 0.7
- Author:
- Allard Buijze
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class to instantiate aAnnotatedSagaRepository. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ProcessingLifecycle.PhaseThephasein which a Saga is written back to theSagaStore, ordered afterPREPARE_COMMITand beforeCOMMIT. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedInstantiate aAnnotatedSagaRepositorybased on the fields contained in theAnnotatedSagaRepository.Builder. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> AnnotatedSagaRepository.Builder<T> builder()Instantiate a Builder to be able to create anAnnotatedSagaRepository.protected voidcommit(AnnotatedSaga<T> saga) Commits the given modifiedsagato the underlying saga store.protected voiddeleteSaga(AnnotatedSaga<T> saga) Remove the given saga as well as all known association values pointing to it from the repository.doCreateInstance(String sagaIdentifier, Supplier<T> sagaFactory, ProcessingContext context) Creates a new Saga instance.@Nullable AnnotatedSaga<T> doLoad(String sagaIdentifier, ProcessingContext context) Loads a known Saga instance by its unique identifier.protected AnnotatedSaga<T> doLoadSaga(String sagaIdentifier) Loads the saga with givensagaIdentifierfrom the underlying saga store and returns it as aAnnotatedSaga.find(AssociationValue associationValue, ProcessingContext context) Find saga instances of the giventypethat have been associated with the givenassociationValue.protected voidstoreSaga(AnnotatedSaga<T> saga) Stores a newly created Saga instance.unsavedSagaResource(ProcessingContext context) Returns a set of identifiers of sagas that may have changed in the givencontextand have not been saved yet.protected voidupdateSaga(AnnotatedSaga<T> saga) Update a stored Saga, by replacing it with the givensagainstance.Methods inherited from class org.axonframework.modelling.saga.repository.LockingSagaRepository
createInstance, load
-
Field Details
-
WRITE_SAGA
Thephasein which a Saga is written back to theSagaStore, ordered afterPREPARE_COMMITand beforeCOMMIT.The write has to happen after the event handler mutated the Saga, and before the caller's transaction commits. Registering it for
PREPARE_COMMITsatisfies both only as long as the repository is called from an earlier phase, because aProcessingContextrejects a registration for the phase it is already running. That rules out any Saga reached from withinPREPARE_COMMITitself, which is where aSimpleEventBusdelivers events published with aProcessingContext, and therefore where a subscribing event processor invokes its components. Ordering the write into the gap abovePREPARE_COMMITkeeps it after the handler and inside the transaction no matter which phase the repository was reached from.Registered as a public constant so that surrounding code can order its own actions relative to the Saga write. Note that a
Phasedeclaring this sameordershares a slot with it, and that a Saga reached from this phase or later cannot be written at all.
-
-
Constructor Details
-
AnnotatedSagaRepository
Instantiate aAnnotatedSagaRepositorybased on the fields contained in theAnnotatedSagaRepository.Builder.Will assert that the
sagaTypeandSagaStoreare notnull, and will throw anAxonConfigurationExceptionif either of them isnull. Additionally, the provided builder's goal is to either build aSagaModelspecifying genericTas the Saga type to be stored or derive it based on the givensagaType. The same argument applies to theMessageHandlerInterceptorMemberChain. All Sagas in this repository must beinstanceOfthis saga type.- Parameters:
builder- theAnnotatedSagaRepository.Builderused to instantiate aAnnotatedSagaRepositoryinstance
-
-
Method Details
-
builder
Instantiate a Builder to be able to create anAnnotatedSagaRepository.This Builder either allows directly setting a
SagaModelof generic typeT, or it will generate it based of the requiredsagaTypefield of typeClass. Same for theMessageHandlerInterceptorMemberChainThus, either the SagaModel or thesagaTypeshould be provided. All Saga in this repository must beinstanceOfthis saga type. Additionally, thesagaTypeandSagaStoreare hard requirements and as such should be provided.- Type Parameters:
T- a generic specifying the Saga type contained in thisSagaRepositoryimplementation- Returns:
- a Builder to be able to create a
AnnotatedSagaRepository
-
doLoad
Description copied from class:LockingSagaRepositoryLoads a known Saga instance by its unique identifier. Due to the concurrent nature of Sagas, it is not unlikely for a Saga to have ceased to exist after it has been found based on associations. Therefore, a repository should returnnullin case a Saga doesn't exists, as opposed to throwing an exception.- Specified by:
doLoadin classLockingSagaRepository<T>- Parameters:
sagaIdentifier- The unique identifier of the Saga to loadcontext- theProcessingContextthe loaded Saga is managed in- Returns:
- The Saga instance, or
nullif no such saga exists
-
doCreateInstance
public AnnotatedSaga<T> doCreateInstance(String sagaIdentifier, Supplier<T> sagaFactory, ProcessingContext context) Description copied from class:LockingSagaRepositoryCreates a new Saga instance. The returned Saga will delegate event handling to the instance supplied by the givenfactoryMethod.- Specified by:
doCreateInstancein classLockingSagaRepository<T>- Parameters:
sagaIdentifier- the identifier to use for the new saga instancesagaFactory- Used to create a new Saga delegatecontext- theProcessingContextthe new Saga is managed in- Returns:
- a new Saga instance wrapping an instance of type
T
-
unsavedSagaResource
Returns a set of identifiers of sagas that may have changed in the givencontextand have not been saved yet.- Parameters:
context- the processing context to inspect for unsaved sagas- Returns:
- set of saga identifiers of unsaved sagas
-
commit
Commits the given modifiedsagato the underlying saga store. If the saga is not active anymore it will be deleted. Otherwise the stored saga and its associations will be updated.- Parameters:
saga- the saga to commit to the store
-
find
Description copied from interface:SagaRepositoryFind saga instances of the giventypethat have been associated with the givenassociationValue.- Parameters:
associationValue- The value that the returned Sagas must be associated withcontext- theProcessingContextthis lookup is part of- Returns:
- A Set containing the found Saga instances. If none are found, an empty Set is returned. Will never return
null.
-
deleteSaga
Remove the given saga as well as all known association values pointing to it from the repository. If no such saga exists, nothing happens.- Parameters:
saga- The saga instance to remove from the repository
-
updateSaga
Update a stored Saga, by replacing it with the givensagainstance.- Parameters:
saga- The saga that has been modified and needs to be updated in the storage
-
storeSaga
Stores a newly created Saga instance.- Parameters:
saga- The newly created Saga instance to store.
-
doLoadSaga
Loads the saga with givensagaIdentifierfrom the underlying saga store and returns it as aAnnotatedSaga.- Parameters:
sagaIdentifier- the identifier of the saga to load- Returns:
- AnnotatedSaga instance with the loaded saga
-