Class AggregateLifecycle

java.lang.Object
org.axonframework.messaging.Scope
org.axonframework.modelling.command.AggregateLifecycle
Direct Known Subclasses:
AnnotatedAggregate, StubAggregateLifecycle

public abstract class AggregateLifecycle extends Scope
Abstract base class of a component that models an aggregate's life cycle.
  • Constructor Details

    • AggregateLifecycle

      public AggregateLifecycle()
  • Method Details

    • apply

      public static ApplyMore apply(Object payload, MetaData metaData)
      Apply a DomainEventMessage with given payload and metadata (metadata from interceptors will be combined with the provided metadata). Applying events means they are immediately applied (published) to the aggregate and scheduled for publication to other event handlers.

      The event is applied on all entities part of this aggregate. If the event is applied from an event handler of the aggregate and additional events need to be applied that depends on state changes brought about by the first event use the returned ApplyMore instance.

      Parameters:
      payload - the payload of the event to apply
      metaData - any meta-data that must be registered with the Event
      Returns:
      a gizmo to apply additional events after the given event has been processed by the entire aggregate
      See Also:
    • apply

      public static ApplyMore apply(Object payload)
      Apply a DomainEventMessage with given payload without metadata (though interceptors can also be used to provide metadata). Applying events means they are immediately applied (published) to the aggregate and scheduled for publication to other event handlers.

      The event is applied on all entities part of this aggregate. If the event is applied from an event handler of the aggregate and additional events need to be applied that depends on state changes brought about by the first event use the returned ApplyMore instance.

      Parameters:
      payload - the payload of the event to apply
      Returns:
      a gizmo to apply additional events after the given event has been processed by the entire aggregate
      See Also:
    • createNew

      public static <T> Aggregate<T> createNew(Class<T> aggregateType, Callable<T> factoryMethod) throws Exception
      Creates a new aggregate instance. In order for new aggregate to be created, a Repository should be available to the current aggregate. Repository of an aggregate to be created is exposed to the current aggregate via RepositoryProvider.
      Type Parameters:
      T - type of new aggregate to be created
      Parameters:
      aggregateType - type of new aggregate to be created
      factoryMethod - factory method which creates new aggregate
      Returns:
      a new aggregate instance
      Throws:
      Exception - thrown if something goes wrong during instantiation of new aggregate
    • isLive

      public static boolean isLive()
      Indicates whether this Aggregate instance is 'live'. Events applied to a 'live' Aggregate represent events that are currently happening, as opposed to events representing historic decisions used to reconstruct the Aggregate's state.
      Returns:
      true if the aggregate is 'live', false if the aggregate is initializing state based on historic events
    • getVersion

      public static Long getVersion()
      Gets the version of the aggregate.
      Returns:
      the current version of the aggregate
    • markDeleted

      public static void markDeleted()
      Marks this aggregate as deleted, instructing a repository to remove that aggregate at an appropriate time.

      Note that different repository implementations may react differently to aggregates marked for deletion. Typically, Event Sourced Repositories will ignore the marking and expect deletion to be provided as part of Event information.

    • getInstance

      protected static AggregateLifecycle getInstance()
      Get the current AggregateLifecycle instance for the current thread. If none exists an IllegalStateException is thrown.
      Returns:
      the thread's current AggregateLifecycle
    • getIsLive

      protected abstract boolean getIsLive()
      Indicates whether this Aggregate instance is 'live'. This means events currently applied represent events that are currently happening, as opposed to events representing historic decisions.
      Returns:
      true if the aggregate is 'live', false if the aggregate is initializing state based on historic events
    • version

      protected abstract Long version()
      Gets the version of the aggregate.
      Returns:
      the current version of the aggregate
    • doMarkDeleted

      protected abstract void doMarkDeleted()
      Marks this aggregate as deleted. Implementations may react differently to aggregates marked for deletion. Typically, Event Sourced Repositories will ignore the marking and expect deletion to be provided as part of Event information.
    • doApply

      protected abstract <T> ApplyMore doApply(T payload, MetaData metaData)
      Apply a DomainEventMessage with given payload and metadata (metadata from interceptors will be combined with the provided metadata). The event should be applied to the aggregate immediately and scheduled for publication to other event handlers.

      The event should be applied on all entities part of this aggregate. If the event is applied from an event handler of the aggregate and additional events need to be applied that depends on state changes brought about by the first event the returned ApplyMore instance should allow for additional events to be applied after this event.

      Parameters:
      payload - the payload of the event to apply
      metaData - any meta-data that must be registered with the Event
      Returns:
      a gizmo to apply additional events after the given event has been processed by the entire aggregate
      See Also:
    • doCreateNew

      protected abstract <T> Aggregate<T> doCreateNew(Class<T> aggregateType, Callable<T> factoryMethod) throws Exception
      Creates a new aggregate instance. In order for new aggregate to be created, a Repository should be available to the current aggregate. Repository of an aggregate to be created is exposed to the current aggregate via RepositoryProvider.
      Type Parameters:
      T - type of new aggregate to be created
      Parameters:
      aggregateType - type of new aggregate to be created
      factoryMethod - factory method which creates new aggregate
      Returns:
      a new aggregate instance
      Throws:
      Exception - thrown if something goes wrong during instantiation of new aggregate
    • execute

      protected void execute(Runnable task)
      Executes the given task. While the task is being executed the current aggregate will be registered with the current thread as the 'current' aggregate.
      Parameters:
      task - the task to execute on the aggregate
    • describeScope

      public ScopeDescriptor describeScope()
      Description copied from class: Scope
      Provide a description of this Scope.
      Specified by:
      describeScope in class Scope
      Returns:
      a ScopeDescriptor describing this Scope
    • type

      protected abstract String type()
      Retrieve a String denoting the type of this Aggregate.
      Returns:
      a String denoting the type of this Aggregate
    • identifier

      protected abstract Object identifier()
      Retrieve a Object denoting the identifier of this Aggregate.
      Returns:
      a Object denoting the identifier of this Aggregate