Class UnitOfWork
java.lang.Object
org.axonframework.messaging.core.unitofwork.UnitOfWork
- All Implemented Interfaces:
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
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.axonframework.messaging.core.unitofwork.ProcessingLifecycle
ProcessingLifecycle.DefaultPhases, ProcessingLifecycle.ErrorHandler, ProcessingLifecycle.Phase -
Method Summary
Modifier and TypeMethodDescriptionexecute()Executes all the registered action in their respectivephases.<R> CompletableFuture<R> executeWithResult(Function<ProcessingContext, CompletableFuture<R>> action) Registers the givenactionfor theinvocation Phaseand executes this Unit of Work right away.booleanbooleanbooleanisError()booleanon(ProcessingLifecycle.Phase phase, Function<ProcessingContext, CompletableFuture<?>> action) Registers the providedactionto be executed in the givenphase.Registers the providedactionto be executed when thisProcessingLifecycleencounters an error during the action of anyProcessingLifecycle.Phase.toString()whenComplete(Consumer<ProcessingContext> action) Registers the providedactionto be executed when thisProcessingLifecyclecompletes all registered actions.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.axonframework.messaging.core.unitofwork.ProcessingLifecycle
doFinally, onAfterCommit, onCommit, onInvocation, onPostInvocation, onPreInvocation, onPrepareCommit, runOn, runOnAfterCommit, runOnCommit, runOnInvocation, runOnPostInvocation, runOnPreInvocation, runOnPrepareCommit
-
Method Details
-
isStarted
public boolean isStarted()Description copied from interface:ProcessingLifecycle- Specified by:
isStartedin interfaceProcessingLifecycle- Returns:
truewhen thisProcessingLifecycleis started,falseotherwise.
-
isError
public boolean isError()Description copied from interface:ProcessingLifecycleReturnstruewhen thisProcessingLifecycleis in error,falseotherwise.When
true, theregistered ErrorHandlerswill be invoked.- Specified by:
isErrorin interfaceProcessingLifecycle- Returns:
truewhen thisProcessingLifecycleis in error,falseotherwise.
-
isCommitted
public boolean isCommitted()Description copied from interface:ProcessingLifecycle- Specified by:
isCommittedin interfaceProcessingLifecycle- Returns:
truewhen thisProcessingLifecycleis committed,falseotherwise.
-
isCompleted
public boolean isCompleted()Description copied from interface:ProcessingLifecycleReturnstruewhen thisProcessingLifecycleis completed,falseotherwise.Note that this
ProcessingLifecycleis marked as completed for a successful and failed completion.- Specified by:
isCompletedin interfaceProcessingLifecycle- Returns:
truewhen thisProcessingLifecycleis completed,falseotherwise.
-
on
public UnitOfWork on(@Nonnull ProcessingLifecycle.Phase phase, @Nonnull Function<ProcessingContext, CompletableFuture<?>> action) Description copied from interface:ProcessingLifecycleRegisters the providedactionto be executed in the givenphase. Uses theCompletableFuturereturned by theactionto compose actions and to carry the action's result.Use this operation when the return value of the
actionis important.- Specified by:
onin interfaceProcessingLifecycle- Parameters:
phase- TheProcessingLifecycle.Phaseto execute the givenactionin.action- TheFunctionthat's given the activeProcessingContextand returns aCompletableFuturefor chaining purposes and to carry the action's result.- Returns:
- This
ProcessingLifecycleinstance for fluent interfacing.
-
onError
Description copied from interface:ProcessingLifecycleRegisters the providedactionto be executed when thisProcessingLifecycleencounters an error during the action of anyProcessingLifecycle.Phase. This includes failures from actions registered throughProcessingLifecycle.whenComplete(Consumer).When the given
ErrorHandlersare invokedProcessingLifecycle.isError()andProcessingLifecycle.isCompleted()will returntrue.- Specified by:
onErrorin interfaceProcessingLifecycle- Parameters:
action- The error handler to execute when thisProcessingLifecycleencounters an error during phase execution.- Returns:
- This
ProcessingLifecycleinstance for fluent interfacing.
-
whenComplete
Description copied from interface:ProcessingLifecycleRegisters the providedactionto be executed when thisProcessingLifecyclecompletes all registered actions.- Specified by:
whenCompletein interfaceProcessingLifecycle- Parameters:
action- AConsumerthat's given the activeProcessingContextto perform its action.- Returns:
- This
ProcessingLifecycleinstance for fluent interfacing.
-
execute
Executes all the registered action in their respectivephases.- Returns:
- A
CompletableFuturethat 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(@Nonnull Function<ProcessingContext, CompletableFuture<R>> action) Registers the givenactionfor theinvocation Phaseand executes this Unit of Work right away.The return value of the given
actionis returned when this Unit of Work is committed, disregarding intermittent results of actions registered in otherphases.- Type Parameters:
R- The type of return value returned by theaction.- Parameters:
action- TheFunctionthat's given the activeProcessingContextand returns aCompletableFuturefor chaining purposes and to carry the action's result.- Returns:
- A
CompletableFuturethat returns normally with the return value of the givenactionwhen the Unit Of Work has been committed. Or, an exceptionally completed future with the exception that caused this Unit of Work to fail.
-
toString
-