Interface UnitOfWork<T extends Message<?>>
- All Known Implementing Classes:
AbstractUnitOfWork,BatchingUnitOfWork,CommandHandlingEntry,DefaultUnitOfWork,DisruptorUnitOfWork
public interface UnitOfWork<T extends Message<?>>
This class represents a Unit of Work that monitors the processing of a
Message.
Before processing begins a Unit of Work is bound to the active thread by registering it with the CurrentUnitOfWork. After processing, the Unit of Work is deregistered from the CurrentUnitOfWork.
Handlers can be notified about the state of the processing of the Message by registering with this Unit of Work.- Since:
- 0.6
- Author:
- Allard Buijze
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumEnum indicating possible phases of the Unit of Work. -
Method Summary
Modifier and TypeMethodDescriptionvoidafterCommit(Consumer<UnitOfWork<T>> handler) Register givenhandlerwith the Unit of Work.default voidattachTransaction(TransactionManager transactionManager) Attach a transaction to this Unit of Work, using the giventransactionManager.voidcommit()Commits the Unit of Work.default voidExecute the giventaskin the context of this Unit of Work.default voidexecute(Runnable task, RollbackConfiguration rollbackConfiguration) Execute the giventaskin the context of this Unit of Work.default <R> ResultMessage<R> executeWithResult(Callable<R> task) Execute the giventaskin the context of this Unit of Work.<R> ResultMessage<R> executeWithResult(Callable<R> task, RollbackConfiguration rollbackConfiguration) Execute the giventaskin the context of this Unit of Work.Get the correlation data contained in themessagebeing processed by the Unit of Work.Get the result of the task that was executed by this Unit of Work.Get the message that is being processed by the Unit of Work.default <R> RgetOrComputeResource(String key, Function<? super String, R> mappingFunction) Returns the resource attached under givenname.default <R> RgetOrDefaultResource(String key, R defaultValue) Returns the resource attached under givenname.default <R> RgetResource(String name) Returns the resource attached under givenname, ornullif no such resource is available.default booleanisActive()Indicates whether this UnitOfWork is started.default booleanCheck if the Unit of Work is the 'currently' active Unit of Work returned byCurrentUnitOfWork.get().booleanCheck if the Unit of Work has been rolled back.default booleanisRoot()Check that returnstrueif this Unit of Work has not got a parent.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.default voidrollback()Initiates the rollback of this Unit of Work, invoking all registered rollback (and clean-up handlers #onCleanup(Consumer)respectively.voidInitiates the rollback of this Unit of Work, invoking all registered rollback (and clean-up handlers #onCleanup(Consumer)respectively.default UnitOfWork<?> root()Returns the root of this Unit of Work.voidstart()Starts the current unit of work.transformMessage(Function<T, ? extends Message<?>> transformOperator) Transform the Message being processed using the given operator and stores the result.
-
Method Details
-
start
void start()Starts the current unit of work. The UnitOfWork instance is registered with the CurrentUnitOfWork. -
commit
void commit()Commits 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 (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.- Throws:
IllegalStateException- if the UnitOfWork wasn't started or if the Unit of Work is not the 'current' Unit of Work returned byCurrentUnitOfWork.get().
-
rollback
default void rollback()Initiates 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. If the rollback is a result of an exception, consider usingrollback(Throwable)instead.- Throws:
IllegalStateException- if the Unit of Work is not in a compatible phase.
-
rollback
Initiates 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.- Parameters:
cause- The cause of the rollback. May benull.- Throws:
IllegalStateException- if the Unit of Work is not in a compatible phase.
-
isActive
default boolean isActive()Indicates whether this UnitOfWork is started. It is started when thestart()method has been called, and if the UnitOfWork has not been committed or rolled back.- Returns:
trueif this UnitOfWork is started,falseotherwise.
-
phase
UnitOfWork.Phase phase()Returns the current phase of the Unit of Work.- Returns:
- the Unit of Work phase
-
onPrepareCommit
Register givenhandlerwith the Unit of Work. The handler will be notified when the phase of the Unit of Work changes toUnitOfWork.Phase.PREPARE_COMMIT.- Parameters:
handler- the handler to register with the Unit of Work
-
onCommit
Register givenhandlerwith the Unit of Work. The handler will be notified when the phase of the Unit of Work changes toUnitOfWork.Phase.COMMIT.- Parameters:
handler- the handler to register with the Unit of Work
-
afterCommit
Register givenhandlerwith the Unit of Work. The handler will be notified when the phase of the Unit of Work changes toUnitOfWork.Phase.AFTER_COMMIT.- Parameters:
handler- the handler to register with the Unit of Work
-
onRollback
Register 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- Parameters:
handler- the handler to register with the Unit of Work
-
onCleanup
Register givenhandlerwith the Unit of Work. The handler will be notified when the phase of the Unit of Work changes toUnitOfWork.Phase.CLEANUP.- Parameters:
handler- the handler to register with the Unit of Work
-
parent
Optional<UnitOfWork<?>> parent()Returns 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.- Returns:
- an optional parent Unit of Work
-
isRoot
default boolean isRoot()Check that returnstrueif this Unit of Work has not got a parent.- Returns:
trueif this Unit of Work has no parent
-
root
Returns the root of this Unit of Work. If this Unit of Work has no parent (seeparent()) it returns itself, otherwise it returns the root of its parent.- Returns:
- the root of this Unit of Work
-
getMessage
T getMessage()Get the message that is being processed by the Unit of Work. A Unit of Work processes a single Message over its life cycle.- Returns:
- the Message being processed by this Unit of Work
-
transformMessage
Transform the Message being processed using the given operator and stores the result.Implementations should take caution not to change the message type to a type incompatible with the current Unit of Work. For example, do not return a CommandMessage when transforming an EventMessage.
- Parameters:
transformOperator- The transform operator to apply to the stored message- Returns:
- this Unit of Work
-
getCorrelationData
MetaData getCorrelationData()Get 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.- Returns:
- The correlation data contained in the message processed by this Unit of Work
-
registerCorrelationDataProvider
Register givencorrelationDataProviderwith this Unit of Work. Correlation data providers are used to provide meta data based on this Unit of Work'sMessagewhengetCorrelationData()is invoked.- Parameters:
correlationDataProvider- the Correlation Data Provider to register
-
resources
Returns a mutable map of resources registered with the Unit of Work.- Returns:
- mapping of resources registered with this Unit of Work
-
getResource
Returns the resource attached under givenname, ornullif no such resource is available.- Type Parameters:
R- The type of resource- Parameters:
name- The name under which the resource was attached- Returns:
- The resource mapped to the given
name, ornullif no resource was found.
-
getOrComputeResource
Returns the resource attached under givenname. If there is no resource mapped to the given key yet themappingFunctionis invoked to provide the mapping.- Type Parameters:
R- The type of resource- Parameters:
key- The name under which the resource was attachedmappingFunction- The function that provides the mapping if there is no mapped resource yet- Returns:
- The resource mapped to the given
key, or the resource returned by themappingFunctionif no resource was found.
-
getOrDefaultResource
Returns the resource attached under givenname. If there is no resource mapped to the given key, thedefaultValueis returned.- Type Parameters:
R- The type of resource- Parameters:
key- The name under which the resource was attacheddefaultValue- The value to return if no mapping is available- Returns:
- The resource mapped to the given
key, or the resource returned by themappingFunctionif no resource was found.
-
attachTransaction
Attach a transaction to this Unit of Work, using the giventransactionManager. The transaction will be managed in the lifecycle of this Unit of Work. Failure to start a transaction will cause this Unit of Work to be rolled back.- Parameters:
transactionManager- The Transaction Manager to create, commit and/or rollback the transaction
-
execute
Execute the giventaskin the context of this Unit of Work. If the Unit of Work is not started yet it will be started. If the task executes successfully the Unit of Work is committed. If any exception is raised while executing the task, the Unit of Work is rolled back and the exception is thrown.- Parameters:
task- the task to execute
-
execute
Execute the giventaskin the context of this Unit of Work. If the Unit of Work is not started yet it will be started. If the task executes successfully the Unit of Work is committed. If an exception is raised while executing the task, therollbackConfigurationdetermines if the Unit of Work should be rolled back or committed, and the exception is thrown.- Parameters:
task- the task to executerollbackConfiguration- configuration that determines whether or not to rollback the unit of work when task execution fails
-
executeWithResult
Execute the giventaskin the context of this Unit of Work. If the Unit of Work is not started yet it will be started. If the task executes successfully the Unit of Work is committed and the result of the task is returned. If any exception is raised while executing the task, the Unit of Work is rolled back and the exception is thrown.- Type Parameters:
R- the type of result that is returned after successful execution- Parameters:
task- the task to execute- Returns:
- The result of the task wrapped in Result Message
-
executeWithResult
<R> ResultMessage<R> executeWithResult(Callable<R> task, @Nonnull RollbackConfiguration rollbackConfiguration) Execute the giventaskin the context of this Unit of Work. If the Unit of Work is not started yet it will be started. If the task executes successfully the Unit of Work is committed and the result of the task is returned. If execution fails, therollbackConfigurationdetermines if the Unit of Work should be rolled back or committed.- Type Parameters:
R- the type of result that is returned after successful execution- Parameters:
task- the task to executerollbackConfiguration- configuration that determines whether or not to rollback the unit of work when task execution fails- Returns:
- The result of the task wrapped in Result Message
-
getExecutionResult
ExecutionResult getExecutionResult()Get the result of the task that was executed by this Unit of Work. If the Unit of Work has not been given a task to execute this method returnsnull.Note that the value of the returned ExecutionResult's
ExecutionResult.isExceptionResult()does not determine whether or not the UnitOfWork has been rolled back. To check whether or not the UnitOfWork was rolled back checkisRolledBack().- Returns:
- The result of the task executed by this Unit of Work, or
nullif the Unit of Work has not been given a task to execute.
-
isRolledBack
boolean isRolledBack()Check if the Unit of Work has been rolled back.- Returns:
trueif the unit of work was rolled back,falseotherwise.
-
isCurrent
default boolean isCurrent()Check if the Unit of Work is the 'currently' active Unit of Work returned byCurrentUnitOfWork.get().- Returns:
trueif the Unit of Work is the currently active Unit of Work
-