Class CurrentUnitOfWork

java.lang.Object
org.axonframework.messaging.unitofwork.CurrentUnitOfWork

public abstract class CurrentUnitOfWork extends Object
Default entry point to gain access to the current UnitOfWork. Components managing transactional boundaries can register and clear UnitOfWork instances, which components can use.
Since:
0.6
Author:
Allard Buijze
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    clear(UnitOfWork<?> unitOfWork)
    Clears the UnitOfWork currently bound to the current thread, if that UnitOfWork is the given unitOfWork.
    static void
    Commits the current UnitOfWork.
    static MetaData
    Returns the Correlation Data attached to the current Unit of Work, or an empty MetaData instance if no Unit of Work is started.
    static UnitOfWork<?>
    get()
    Gets the UnitOfWork bound to the current thread.
    static boolean
    If a UnitOfWork is started, invokes the given consumer with the active Unit of Work.
    static boolean
    Indicates whether a unit of work has already been started.
    static <T> Optional<T>
    map(Function<UnitOfWork<?>,T> function)
    If a Unit of Work is started, execute the given function on it.
    static void
    set(UnitOfWork<?> unitOfWork)
    Binds the given unitOfWork to the current thread.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isStarted

      public static boolean isStarted()
      Indicates whether a unit of work has already been started. This method can be used by interceptors to prevent nesting of UnitOfWork instances.
      Returns:
      whether a UnitOfWork has already been started.
    • ifStarted

      public static boolean ifStarted(Consumer<UnitOfWork<?>> consumer)
      If a UnitOfWork is started, invokes the given consumer with the active Unit of Work. Otherwise, it does nothing
      Parameters:
      consumer - The consumer to invoke if a Unit of Work is active
      Returns:
      true if a unit of work is active, false otherwise
    • map

      public static <T> Optional<T> map(Function<UnitOfWork<?>,T> function)
      If a Unit of Work is started, execute the given function on it. Otherwise, returns an empty Optional. Use this method when you wish to retrieve information from a Unit of Work, reverting to a default when no Unit of Work is started.
      Type Parameters:
      T - The type of return value expected
      Parameters:
      function - The function to apply to the unit of work, if present
      Returns:
      an optional containing the result of the function, or an empty Optional when no Unit of Work was started
      Throws:
      NullPointerException - when a Unit of Work is present and the function returns null
    • get

      public static UnitOfWork<?> get()
      Gets the UnitOfWork bound to the current thread. If no UnitOfWork has been started, an IllegalStateException is thrown.

      To verify whether a UnitOfWork is already active, use isStarted().

      Returns:
      The UnitOfWork bound to the current thread.
      Throws:
      IllegalStateException - if no UnitOfWork is active
    • commit

      public static void commit()
      Commits the current UnitOfWork. If no UnitOfWork was started, an IllegalStateException is thrown.
      Throws:
      IllegalStateException - if no UnitOfWork is currently started.
      See Also:
    • set

      public static void set(UnitOfWork<?> unitOfWork)
      Binds the given unitOfWork to the current thread. If other UnitOfWork instances were bound, they will be marked as inactive until the given UnitOfWork is cleared.
      Parameters:
      unitOfWork - The UnitOfWork to bind to the current thread.
    • clear

      public static void clear(UnitOfWork<?> unitOfWork)
      Clears the UnitOfWork currently bound to the current thread, if that UnitOfWork is the given unitOfWork.
      Parameters:
      unitOfWork - The UnitOfWork expected to be bound to the current thread.
      Throws:
      IllegalStateException - when the given UnitOfWork was not the current active UnitOfWork. This exception indicates a potentially wrong nesting of Units Of Work.
    • correlationData

      public static MetaData correlationData()
      Returns the Correlation Data attached to the current Unit of Work, or an empty MetaData instance if no Unit of Work is started.
      Returns:
      a MetaData instance representing the current Unit of Work's correlation data, or an empty MetaData instance if no Unit of Work is started.
      See Also: