Class AnnotatedSaga<T>

All Implemented Interfaces:
EventMessageHandler, MessageHandler<EventMessage<?>>, Saga<T>

public class AnnotatedSaga<T> extends SagaLifecycle implements Saga<T>
Implementation of the interface that allows for a POJO instance with annotated message handlers to act as a Saga. The POJO instance can access static methods on SagaLifecycle as long as it is access via the execute(Consumer) or invoke(Function) methods.
Since:
3.0
Author:
Allard Buijze
  • Constructor Details

    • AnnotatedSaga

      public AnnotatedSaga(String sagaId, Set<AssociationValue> associationValues, T annotatedSaga, SagaModel<T> metaModel, MessageHandlerInterceptorMemberChain<T> chainedInterceptor)
      Creates an AnnotatedSaga instance to wrap the given annotatedSaga, identifier with the given sagaId and associated with the given associationValues. The metaModel provides the description of the structure of the Saga.
      Parameters:
      sagaId - The identifier of this Saga instance
      associationValues - The current associations of this Saga
      annotatedSaga - The object instance representing the Saga
      metaModel - The model describing Saga structure
      chainedInterceptor - The interceptor to be used for this Saga
  • Method Details

    • getSagaIdentifier

      public String getSagaIdentifier()
      Description copied from class: SagaLifecycle
      Retrieve a String denoting the identifier of this Saga.
      Specified by:
      getSagaIdentifier in interface Saga<T>
      Specified by:
      getSagaIdentifier in class SagaLifecycle
      Returns:
      a String denoting the identifier of this Saga
    • getAssociationValues

      public AssociationValues getAssociationValues()
      Description copied from class: SagaLifecycle
      SagaLifecycle instance method to retrieve the AssociationValues of the currently active Saga.
      Specified by:
      getAssociationValues in interface Saga<T>
      Specified by:
      getAssociationValues in class SagaLifecycle
      Returns:
      The AssociationValues of the current saga.
    • invoke

      public <R> R invoke(Function<T,R> invocation)
      Description copied from interface: Saga
      Execute the given invocation against the root object of this Saga instance.
      Specified by:
      invoke in interface Saga<T>
      Type Parameters:
      R - The type of return value expected
      Parameters:
      invocation - the function to invoke. The root object of the Saga is input to the function, the result is the result of the execution.
      Returns:
      The result of the invocation on the Saga.
    • execute

      public void execute(Consumer<T> invocation)
      Description copied from interface: Saga
      Execute the given invocation against the root object of this Saga instance.
      Specified by:
      execute in interface Saga<T>
      Parameters:
      invocation - the function to invoke. The root object of the Saga is input to the function.
    • canHandle

      public final boolean canHandle(EventMessage<?> event)
      Description copied from interface: MessageHandler
      Indicates whether this handler can handle the given message
      Specified by:
      canHandle in interface MessageHandler<T>
      Parameters:
      event - The message to verify
      Returns:
      true if this handler can handle the message, otherwise false
    • handle

      public final Object handle(EventMessage<?> event)
      Description copied from interface: EventMessageHandler
      Process the given event. The implementation may decide to process or skip the given event. It is highly unrecommended to throw any exception during the event handling process.
      Specified by:
      handle in interface EventMessageHandler
      Specified by:
      handle in interface MessageHandler<T>
      Parameters:
      event - the event to handle
      Returns:
      the result of the event handler invocation. Is generally ignored
    • isActive

      public boolean isActive()
      Description copied from interface: Saga
      Indicates whether or not this saga is active. A Saga is active when its life cycle has not been ended.
      Specified by:
      isActive in interface Saga<T>
      Returns:
      true if this saga is active, false otherwise.
    • root

      public T root()
      Returns the (annotated) Saga instance. This method should not be used to modify the Saga's state, as it doesn't allow the instance to access the static methods on SagaLifecycle.
      Returns:
      the Saga instance
    • doEnd

      protected void doEnd()
      Marks the saga as ended. Ended saga's may be cleaned up by the repository when they are committed.
      Specified by:
      doEnd in class SagaLifecycle
    • doAssociateWith

      protected void doAssociateWith(AssociationValue property)
      Registers a AssociationValue with the given saga. When the saga is committed, it can be found using the registered property.
      Specified by:
      doAssociateWith in class SagaLifecycle
      Parameters:
      property - The value to associate this saga with.
    • type

      protected String type()
      Description copied from class: SagaLifecycle
      Retrieve a String denoting the type of this Saga.
      Specified by:
      type in class SagaLifecycle
      Returns:
      a String denoting the type of this Saga
    • doRemoveAssociation

      protected void doRemoveAssociation(AssociationValue property)
      Removes the given association from this Saga. When the saga is committed, it can no longer be found using the given association. If the given property wasn't registered with the saga, nothing happens.
      Specified by:
      doRemoveAssociation in class SagaLifecycle
      Parameters:
      property - the association value to remove from the saga.