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
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.axonframework.messaging.unitofwork.UnitOfWork
UnitOfWork.Phase -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidaddHandler(UnitOfWork.Phase phase, Consumer<UnitOfWork<T>> handler) Register the givenhandlerwith the Unit of Work.voidafterCommit(Consumer<UnitOfWork<T>> handler) Register givenhandlerwith the Unit of Work.protected voidchangePhase(UnitOfWork.Phase... phases) Ask the unit of work to transition to the givenphasessequentially.voidcommit()Commits the Unit of Work.protected Collection<CorrelationDataProvider> Provides the collection of registered Correlation Data Providers of this Unit of Work.Get the correlation data contained in themessagebeing processed by the Unit of Work.booleanCheck if the Unit of Work has been rolled back.protected abstract voidnotifyHandlers(UnitOfWork.Phase phase) Notify the handlers attached to the givenphase.voidonCleanup(Consumer<UnitOfWork<T>> handler) Register givenhandlerwith the Unit of Work.voidonCommit(Consumer<UnitOfWork<T>> handler) Register givenhandlerwith the Unit of Work.voidonPrepareCommit(Consumer<UnitOfWork<T>> handler) Register givenhandlerwith the Unit of Work.voidonRollback(Consumer<UnitOfWork<T>> handler) Register givenhandlerwith the Unit of Work.Optional<UnitOfWork<?>> parent()Returns an optional for the parent of this Unit of Work.phase()Returns the current phase of the Unit of Work.voidregisterCorrelationDataProvider(CorrelationDataProvider correlationDataProvider) Register givencorrelationDataProviderwith this Unit of Work.Returns a mutable map of resources registered with the Unit of Work.voidInitiates the rollback of this Unit of Work, invoking all registered rollback (and clean-up handlers #onCleanup(Consumer)respectively.protected abstract voidsetExecutionResult(ExecutionResult executionResult) Set the execution result of processing the currentMessage.protected voidsetPhase(UnitOfWork.Phase phase) Overwrite the current phase with the givenphase.protected abstract voidsetRollbackCause(Throwable cause) Sets the cause for rolling back this Unit of Work.voidstart()Starts the current unit of work.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.axonframework.messaging.unitofwork.UnitOfWork
attachTransaction, execute, execute, executeWithResult, executeWithResult, getExecutionResult, getMessage, getOrComputeResource, getOrDefaultResource, getResource, isActive, isCurrent, isRoot, rollback, root, transformMessage
-
Constructor Details
-
AbstractUnitOfWork
public AbstractUnitOfWork()
-
-
Method Details
-
start
public void start()Description copied from interface:UnitOfWorkStarts the current unit of work. The UnitOfWork instance is registered with the CurrentUnitOfWork.- Specified by:
startin interfaceUnitOfWork<T extends Message<?>>
-
commit
public void commit()Description copied from interface:UnitOfWorkCommits 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 theCurrentUnitOfWork. 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:
commitin interfaceUnitOfWork<T extends Message<?>>
-
rollback
Description copied from interface:UnitOfWorkInitiates 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 theCurrentUnitOfWork.- Specified by:
rollbackin interfaceUnitOfWork<T extends Message<?>>- Parameters:
cause- The cause of the rollback. May benull.
-
parent
Description copied from interface:UnitOfWorkReturns 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:
parentin interfaceUnitOfWork<T extends Message<?>>- Returns:
- an optional parent Unit of Work
-
resources
Description copied from interface:UnitOfWorkReturns a mutable map of resources registered with the Unit of Work.- Specified by:
resourcesin interfaceUnitOfWork<T extends Message<?>>- Returns:
- mapping of resources registered with this Unit of Work
-
isRolledBack
public boolean isRolledBack()Description copied from interface:UnitOfWorkCheck if the Unit of Work has been rolled back.- Specified by:
isRolledBackin interfaceUnitOfWork<T extends Message<?>>- Returns:
trueif the unit of work was rolled back,falseotherwise.
-
registerCorrelationDataProvider
Description copied from interface:UnitOfWorkRegister givencorrelationDataProviderwith this Unit of Work. Correlation data providers are used to provide meta data based on this Unit of Work'sMessagewhenUnitOfWork.getCorrelationData()is invoked.- Specified by:
registerCorrelationDataProviderin interfaceUnitOfWork<T extends Message<?>>- Parameters:
correlationDataProvider- the Correlation Data Provider to register
-
getCorrelationData
Description copied from interface:UnitOfWorkGet the correlation data contained in themessagebeing processed by the Unit of Work. By default this correlation data will be copied to othermessagescreated in the context of this Unit of Work, so long as these messages extend fromGenericMessage.- Specified by:
getCorrelationDatain interfaceUnitOfWork<T extends Message<?>>- Returns:
- The correlation data contained in the message processed by this Unit of Work
-
onPrepareCommit
Description copied from interface:UnitOfWorkRegister givenhandlerwith the Unit of Work. The handler will be notified when the phase of the Unit of Work changes toUnitOfWork.Phase.PREPARE_COMMIT.- Specified by:
onPrepareCommitin interfaceUnitOfWork<T extends Message<?>>- Parameters:
handler- the handler to register with the Unit of Work
-
onCommit
Description copied from interface:UnitOfWorkRegister givenhandlerwith the Unit of Work. The handler will be notified when the phase of the Unit of Work changes toUnitOfWork.Phase.COMMIT.- Specified by:
onCommitin interfaceUnitOfWork<T extends Message<?>>- Parameters:
handler- the handler to register with the Unit of Work
-
afterCommit
Description copied from interface:UnitOfWorkRegister givenhandlerwith the Unit of Work. The handler will be notified when the phase of the Unit of Work changes toUnitOfWork.Phase.AFTER_COMMIT.- Specified by:
afterCommitin interfaceUnitOfWork<T extends Message<?>>- Parameters:
handler- the handler to register with the Unit of Work
-
onRollback
Description copied from interface:UnitOfWorkRegister givenhandlerwith the Unit of Work. The handler will be notified when the phase of the Unit of Work changes toUnitOfWork.Phase.ROLLBACK. On rollback, the cause for the rollback can obtained from the supplied- Specified by:
onRollbackin interfaceUnitOfWork<T extends Message<?>>- Parameters:
handler- the handler to register with the Unit of Work
-
onCleanup
Description copied from interface:UnitOfWorkRegister givenhandlerwith the Unit of Work. The handler will be notified when the phase of the Unit of Work changes toUnitOfWork.Phase.CLEANUP.- Specified by:
onCleanupin interfaceUnitOfWork<T extends Message<?>>- Parameters:
handler- the handler to register with the Unit of Work
-
phase
Description copied from interface:UnitOfWorkReturns the current phase of the Unit of Work.- Specified by:
phasein interfaceUnitOfWork<T extends Message<?>>- Returns:
- the Unit of Work phase
-
setPhase
Overwrite the current phase with the givenphase.- Parameters:
phase- the new phase of the Unit of Work
-
changePhase
Ask the unit of work to transition to the givenphasessequentially. 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
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
Notify the handlers attached to the givenphase.- Parameters:
phase- The phase for which to invoke registered handlers.
-
addHandler
Register the givenhandlerwith the Unit of Work. The handler will be invoked when the Unit of Work changes its phase to the givenphase.- Parameters:
phase- the Phase of the Unit of Work at which to invoke the handlerhandler- the handler to add
-
setExecutionResult
Set the execution result of processing the currentMessage.- Parameters:
executionResult- the ExecutionResult of the currently handled Message
-
setRollbackCause
Sets the cause for rolling back this Unit of Work.- Parameters:
cause- The cause for rolling back this Unit of Work
-