java.lang.Object
org.axonframework.messaging.core.unitofwork.UnitOfWork
All Implemented Interfaces:
ProcessingLifecycle

public class UnitOfWork extends Object implements ProcessingLifecycle
This class represents a Unit of Work that monitors the processing of a task.

As an implementation of the ProcessingLifecycle, steps can be attached in several phases of the Unit of Work to ensure the task-to-process is taken care off correctly. Furthermore, the Unit of Work implements resource management through the ProcessingContext, providing the possibility to carry along resources throughout the phases.

It is strongly recommended to interface with the ProcessingLifecycle and/or ProcessingContext instead of with the UnitOfWork directly.

Since:
0.6
Author:
Allard Buijze, Gerard Klijs, Milan Savić, Mitchell Herrijgers, Sara Pellegrini, Steven van Beelen
  • Method Details

    • isStarted

      public boolean isStarted()
      Description copied from interface: ProcessingLifecycle
      Returns true when this ProcessingLifecycle is started, false otherwise.
      Specified by:
      isStarted in interface ProcessingLifecycle
      Returns:
      true when this ProcessingLifecycle is started, false otherwise
    • isError

      public boolean isError()
      Description copied from interface: ProcessingLifecycle
      Returns true when this ProcessingLifecycle is in error, false otherwise.

      When true, the registered ErrorHandlers will be invoked.

      Specified by:
      isError in interface ProcessingLifecycle
      Returns:
      true when this ProcessingLifecycle is in error, false otherwise
    • isCommitted

      public boolean isCommitted()
      Description copied from interface: ProcessingLifecycle
      Returns true when this ProcessingLifecycle is committed, false otherwise.
      Specified by:
      isCommitted in interface ProcessingLifecycle
      Returns:
      true when this ProcessingLifecycle is committed, false otherwise
    • isCompleted

      public boolean isCompleted()
      Description copied from interface: ProcessingLifecycle
      Returns true when this ProcessingLifecycle is completed, false otherwise.

      Note that this ProcessingLifecycle is marked as completed for a successful and failed completion.

      Specified by:
      isCompleted in interface ProcessingLifecycle
      Returns:
      true when this ProcessingLifecycle is completed, false otherwise
    • on

      Description copied from interface: ProcessingLifecycle
      Registers the provided action to be executed in the given phase. Uses the CompletableFuture returned by the action to compose actions and to carry the action's result.

      Use this operation when the return value of the action is important.

      An action may register further actions while it runs, but only for a Phase with a higher order than the one it is running in. Registering for the Phase currently running, or for one already passed, is rejected.

      A component that is itself invoked from within a late Phase therefore cannot register for that same Phase. It can register for a custom Phase ordered in the gap above it: Phase is an interface over an arbitrary order and the ProcessingLifecycle.DefaultPhases leave 10000 between them, so such an action still runs once the current Phase completed and before the next default one begins.

      Specified by:
      on in interface ProcessingLifecycle
      Parameters:
      phase - the ProcessingLifecycle.Phase to execute the given action in
      action - the Function that's given the active ProcessingContext and returns a CompletableFuture for chaining purposes and to carry the action's result
      Returns:
      this ProcessingLifecycle instance for fluent interfacing
    • onError

      Description copied from interface: ProcessingLifecycle
      Registers the provided action to be executed when this ProcessingLifecycle encounters an error during the action of any ProcessingLifecycle.Phase. This includes failures from actions registered through ProcessingLifecycle.whenComplete(Consumer).

      When the given ErrorHandlers are invoked ProcessingLifecycle.isError() and ProcessingLifecycle.isCompleted() will return true.

      Specified by:
      onError in interface ProcessingLifecycle
      Parameters:
      action - the error handler to execute when this ProcessingLifecycle encounters an error during phase execution
      Returns:
      this ProcessingLifecycle instance for fluent interfacing
    • whenComplete

      public ProcessingLifecycle whenComplete(Consumer<ProcessingContext> action)
      Description copied from interface: ProcessingLifecycle
      Registers the provided action to be executed when this ProcessingLifecycle completes all registered actions.
      Specified by:
      whenComplete in interface ProcessingLifecycle
      Parameters:
      action - a Consumer that's given the active ProcessingContext to perform its action
      Returns:
      this ProcessingLifecycle instance for fluent interfacing
    • execute

      public CompletableFuture<Void> execute()
      Executes all the registered action in their respective phases.
      Returns:
      A CompletableFuture that returns normally when this Unit Of Work has been committed or exceptionally with the exception that caused the Unit of Work to fail.
    • executeWithResult

      public <R> CompletableFuture<R> executeWithResult(Function<ProcessingContext,CompletableFuture<R>> action)
      Registers the given action for the invocation Phase and executes this Unit of Work right away.

      The return value of the given action is returned when this Unit of Work is committed, disregarding intermittent results of actions registered in other phases.

      Type Parameters:
      R - The type of return value returned by the action.
      Parameters:
      action - The Function that's given the active ProcessingContext and returns a CompletableFuture for chaining purposes and to carry the action's result.
      Returns:
      A CompletableFuture that returns normally with the return value of the given action when the Unit Of Work has been committed. Or, an exceptionally completed future with the exception that caused this Unit of Work to fail.
    • toString

      public String toString()
      Overrides:
      toString in class Object