Class AnnotatedSagaRepository<T>

java.lang.Object
org.axonframework.modelling.saga.repository.LockingSagaRepository<T>
org.axonframework.modelling.saga.repository.AnnotatedSagaRepository<T>
Type Parameters:
T - generic type specifying the Saga type stored by this SagaRepository
All Implemented Interfaces:
SagaRepository<T>

public class AnnotatedSagaRepository<T> extends LockingSagaRepository<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
  • Field Details

    • WRITE_SAGA

      public static final ProcessingLifecycle.Phase WRITE_SAGA
      The phase in which a Saga is written back to the SagaStore, ordered after PREPARE_COMMIT and before COMMIT.

      The write has to happen after the event handler mutated the Saga, and before the caller's transaction commits. Registering it for PREPARE_COMMIT satisfies both only as long as the repository is called from an earlier phase, because a ProcessingContext rejects a registration for the phase it is already running. That rules out any Saga reached from within PREPARE_COMMIT itself, which is where a SimpleEventBus delivers events published with a ProcessingContext, and therefore where a subscribing event processor invokes its components. Ordering the write into the gap above PREPARE_COMMIT keeps 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 Phase declaring this same order shares a slot with it, and that a Saga reached from this phase or later cannot be written at all.

  • Constructor Details

  • Method Details

    • builder

      public static <T> AnnotatedSagaRepository.Builder<T> builder()
      Instantiate a Builder to be able to create an AnnotatedSagaRepository.

      This Builder either allows directly setting a SagaModel of generic type T, or it will generate it based of the required sagaType field of type Class. Same for the MessageHandlerInterceptorMemberChain Thus, either the SagaModel or the sagaType should be provided. All Saga in this repository must be instanceOf this saga type. Additionally, the sagaType and SagaStore are hard requirements and as such should be provided.

      Type Parameters:
      T - a generic specifying the Saga type contained in this SagaRepository implementation
      Returns:
      a Builder to be able to create a AnnotatedSagaRepository
    • doLoad

      public @Nullable AnnotatedSaga<T> doLoad(String sagaIdentifier, ProcessingContext context)
      Description copied from class: LockingSagaRepository
      Loads 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 return null in case a Saga doesn't exists, as opposed to throwing an exception.
      Specified by:
      doLoad in class LockingSagaRepository<T>
      Parameters:
      sagaIdentifier - The unique identifier of the Saga to load
      context - the ProcessingContext the loaded Saga is managed in
      Returns:
      The Saga instance, or null if no such saga exists
    • doCreateInstance

      public AnnotatedSaga<T> doCreateInstance(String sagaIdentifier, Supplier<T> sagaFactory, ProcessingContext context)
      Description copied from class: LockingSagaRepository
      Creates a new Saga instance. The returned Saga will delegate event handling to the instance supplied by the given factoryMethod.
      Specified by:
      doCreateInstance in class LockingSagaRepository<T>
      Parameters:
      sagaIdentifier - the identifier to use for the new saga instance
      sagaFactory - Used to create a new Saga delegate
      context - the ProcessingContext the new Saga is managed in
      Returns:
      a new Saga instance wrapping an instance of type T
    • unsavedSagaResource

      protected Set<String> unsavedSagaResource(ProcessingContext context)
      Returns a set of identifiers of sagas that may have changed in the given context and have not been saved yet.
      Parameters:
      context - the processing context to inspect for unsaved sagas
      Returns:
      set of saga identifiers of unsaved sagas
    • commit

      protected void commit(AnnotatedSaga<T> saga)
      Commits the given modified saga to 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

      public Set<String> find(AssociationValue associationValue, ProcessingContext context)
      Description copied from interface: SagaRepository
      Find saga instances of the given type that have been associated with the given associationValue.

      Parameters:
      associationValue - The value that the returned Sagas must be associated with
      context - the ProcessingContext this 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

      protected void deleteSaga(AnnotatedSaga<T> saga)
      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

      protected void updateSaga(AnnotatedSaga<T> saga)
      Update a stored Saga, by replacing it with the given saga instance.
      Parameters:
      saga - The saga that has been modified and needs to be updated in the storage
    • storeSaga

      protected void storeSaga(AnnotatedSaga<T> saga)
      Stores a newly created Saga instance.
      Parameters:
      saga - The newly created Saga instance to store.
    • doLoadSaga

      protected AnnotatedSaga<T> doLoadSaga(String sagaIdentifier)
      Loads the saga with given sagaIdentifier from the underlying saga store and returns it as a AnnotatedSaga.
      Parameters:
      sagaIdentifier - the identifier of the saga to load
      Returns:
      AnnotatedSaga instance with the loaded saga