Class AbstractUnitOfWork<T extends Message<?>>

java.lang.Object
org.axonframework.messaging.unitofwork.AbstractUnitOfWork<T>
All Implemented Interfaces:
UnitOfWork<T>
Direct Known Subclasses:
BatchingUnitOfWork, DefaultUnitOfWork, DisruptorUnitOfWork

public abstract class AbstractUnitOfWork<T extends Message<?>> extends Object implements UnitOfWork<T>
Abstract implementation of the Unit of Work. It provides default implementations of all methods related to the processing of a Message.
Since:
3.0
Author:
Allard Buijze
  • Constructor Details

    • AbstractUnitOfWork

      public AbstractUnitOfWork()
  • Method Details

    • start

      public void start()
      Description copied from interface: UnitOfWork
      Starts the current unit of work. The UnitOfWork instance is registered with the CurrentUnitOfWork.
      Specified by:
      start in interface UnitOfWork<T extends Message<?>>
    • commit

      public void commit()
      Description copied from interface: UnitOfWork
      Commits the Unit of Work. This should be invoked after the Unit of Work Message has been processed. Handlers registered to the Unit of Work will be notified.

      After the commit (successful or not), any registered clean-up handlers (UnitOfWork.onCleanup(Consumer)}) will be invoked and the Unit of Work is deregistered from the CurrentUnitOfWork.

      If the Unit of Work fails to commit, e.g. because an exception is raised by one of its handlers, the Unit of Work is rolled back.

      Specified by:
      commit in interface UnitOfWork<T extends Message<?>>
    • rollback

      public void rollback(Throwable cause)
      Description copied from interface: UnitOfWork
      Initiates the rollback of this Unit of Work, invoking all registered rollback (and clean-up handlers #onCleanup(Consumer) respectively. Finally, the Unit of Work is deregistered from the CurrentUnitOfWork.
      Specified by:
      rollback in interface UnitOfWork<T extends Message<?>>
      Parameters:
      cause - The cause of the rollback. May be null.
    • parent

      public Optional<UnitOfWork<?>> parent()
      Description copied from interface: UnitOfWork
      Returns an optional for the parent of this Unit of Work. The optional holds the Unit of Work that was active when this Unit of Work was started. In case no other Unit of Work was active when this Unit of Work was started the optional is empty, indicating that this is the Unit of Work root.
      Specified by:
      parent in interface UnitOfWork<T extends Message<?>>
      Returns:
      an optional parent Unit of Work
    • resources

      public Map<String,Object> resources()
      Description copied from interface: UnitOfWork
      Returns a mutable map of resources registered with the Unit of Work.
      Specified by:
      resources in interface UnitOfWork<T extends Message<?>>
      Returns:
      mapping of resources registered with this Unit of Work
    • isRolledBack

      public boolean isRolledBack()
      Description copied from interface: UnitOfWork
      Check if the Unit of Work has been rolled back.
      Specified by:
      isRolledBack in interface UnitOfWork<T extends Message<?>>
      Returns:
      true if the unit of work was rolled back, false otherwise.
    • registerCorrelationDataProvider

      public void registerCorrelationDataProvider(CorrelationDataProvider correlationDataProvider)
      Description copied from interface: UnitOfWork
      Register given correlationDataProvider with this Unit of Work. Correlation data providers are used to provide meta data based on this Unit of Work's Message when UnitOfWork.getCorrelationData() is invoked.
      Specified by:
      registerCorrelationDataProvider in interface UnitOfWork<T extends Message<?>>
      Parameters:
      correlationDataProvider - the Correlation Data Provider to register
    • getCorrelationData

      public MetaData getCorrelationData()
      Description copied from interface: UnitOfWork
      Get the correlation data contained in the message being processed by the Unit of Work.

      By default this correlation data will be copied to other messages created in the context of this Unit of Work, so long as these messages extend from GenericMessage.

      Specified by:
      getCorrelationData in interface UnitOfWork<T extends Message<?>>
      Returns:
      The correlation data contained in the message processed by this Unit of Work
    • onPrepareCommit

      public void onPrepareCommit(Consumer<UnitOfWork<T>> handler)
      Description copied from interface: UnitOfWork
      Register given handler with the Unit of Work. The handler will be notified when the phase of the Unit of Work changes to UnitOfWork.Phase.PREPARE_COMMIT.
      Specified by:
      onPrepareCommit in interface UnitOfWork<T extends Message<?>>
      Parameters:
      handler - the handler to register with the Unit of Work
    • onCommit

      public void onCommit(Consumer<UnitOfWork<T>> handler)
      Description copied from interface: UnitOfWork
      Register given handler with the Unit of Work. The handler will be notified when the phase of the Unit of Work changes to UnitOfWork.Phase.COMMIT.
      Specified by:
      onCommit in interface UnitOfWork<T extends Message<?>>
      Parameters:
      handler - the handler to register with the Unit of Work
    • afterCommit

      public void afterCommit(Consumer<UnitOfWork<T>> handler)
      Description copied from interface: UnitOfWork
      Register given handler with the Unit of Work. The handler will be notified when the phase of the Unit of Work changes to UnitOfWork.Phase.AFTER_COMMIT.
      Specified by:
      afterCommit in interface UnitOfWork<T extends Message<?>>
      Parameters:
      handler - the handler to register with the Unit of Work
    • onRollback

      public void onRollback(Consumer<UnitOfWork<T>> handler)
      Description copied from interface: UnitOfWork
      Register given handler with the Unit of Work. The handler will be notified when the phase of the Unit of Work changes to UnitOfWork.Phase.ROLLBACK. On rollback, the cause for the rollback can obtained from the supplied
      Specified by:
      onRollback in interface UnitOfWork<T extends Message<?>>
      Parameters:
      handler - the handler to register with the Unit of Work
    • onCleanup

      public void onCleanup(Consumer<UnitOfWork<T>> handler)
      Description copied from interface: UnitOfWork
      Register given handler with the Unit of Work. The handler will be notified when the phase of the Unit of Work changes to UnitOfWork.Phase.CLEANUP.
      Specified by:
      onCleanup in interface UnitOfWork<T extends Message<?>>
      Parameters:
      handler - the handler to register with the Unit of Work
    • phase

      public UnitOfWork.Phase phase()
      Description copied from interface: UnitOfWork
      Returns the current phase of the Unit of Work.
      Specified by:
      phase in interface UnitOfWork<T extends Message<?>>
      Returns:
      the Unit of Work phase
    • setPhase

      protected void setPhase(UnitOfWork.Phase phase)
      Overwrite the current phase with the given phase.
      Parameters:
      phase - the new phase of the Unit of Work
    • changePhase

      protected void changePhase(UnitOfWork.Phase... phases)
      Ask the unit of work to transition to the given phases sequentially. In each of the phases the unit of work is responsible for invoking the handlers attached to each phase.

      By default this sets the Phase and invokes the handlers attached to the phase.

      Parameters:
      phases - The phases to transition to in sequential order
    • correlationDataProviders

      protected Collection<CorrelationDataProvider> correlationDataProviders()
      Provides the collection of registered Correlation Data Providers of this Unit of Work. The returned collection is a live view of the providers registered. Any changes in the registration are reflected in the returned collection.
      Returns:
      The Correlation Data Providers registered with this Unit of Work.
    • notifyHandlers

      protected abstract void notifyHandlers(UnitOfWork.Phase phase)
      Notify the handlers attached to the given phase.
      Parameters:
      phase - The phase for which to invoke registered handlers.
    • addHandler

      protected abstract void addHandler(UnitOfWork.Phase phase, Consumer<UnitOfWork<T>> handler)
      Register the given handler with the Unit of Work. The handler will be invoked when the Unit of Work changes its phase to the given phase.
      Parameters:
      phase - the Phase of the Unit of Work at which to invoke the handler
      handler - the handler to add
    • setExecutionResult

      protected abstract void setExecutionResult(ExecutionResult executionResult)
      Set the execution result of processing the current Message.
      Parameters:
      executionResult - the ExecutionResult of the currently handled Message
    • setRollbackCause

      protected abstract void setRollbackCause(Throwable cause)
      Sets the cause for rolling back this Unit of Work.
      Parameters:
      cause - The cause for rolling back this Unit of Work