Class CurrentUnitOfWork
java.lang.Object
org.axonframework.messaging.unitofwork.CurrentUnitOfWork
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 TypeMethodDescriptionstatic voidclear(UnitOfWork<?> unitOfWork) Clears the UnitOfWork currently bound to the current thread, if that UnitOfWork is the givenunitOfWork.static voidcommit()Commits the current UnitOfWork.static MetaDataReturns the Correlation Data attached to the current Unit of Work, or an emptyMetaDatainstance if no Unit of Work is started.static UnitOfWork<?> get()Gets the UnitOfWork bound to the current thread.static booleanifStarted(Consumer<UnitOfWork<?>> consumer) If a UnitOfWork is started, invokes the givenconsumerwith the active Unit of Work.static booleanIndicates 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 givenfunctionon it.static voidset(UnitOfWork<?> unitOfWork) Binds the givenunitOfWorkto the current thread.
-
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
If a UnitOfWork is started, invokes the givenconsumerwith the active Unit of Work. Otherwise, it does nothing- Parameters:
consumer- The consumer to invoke if a Unit of Work is active- Returns:
trueif a unit of work is active,falseotherwise
-
map
If a Unit of Work is started, execute the givenfunctionon 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
Gets the UnitOfWork bound to the current thread. If no UnitOfWork has been started, anIllegalStateExceptionis thrown. To verify whether a UnitOfWork is already active, useisStarted().- 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, anIllegalStateExceptionis thrown.- Throws:
IllegalStateException- if no UnitOfWork is currently started.- See Also:
-
set
Binds the givenunitOfWorkto 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
Clears the UnitOfWork currently bound to the current thread, if that UnitOfWork is the givenunitOfWork.- 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
Returns the Correlation Data attached to the current Unit of Work, or an emptyMetaDatainstance 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:
-