public interface UnitOfWork<T extends Message<?>>
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.Modifier and Type | Interface and Description |
---|---|
static class |
UnitOfWork.Phase
Enum indicating possible phases of the Unit of Work.
|
Modifier and Type | Method and Description |
---|---|
void |
afterCommit(Consumer<UnitOfWork<T>> handler)
Register given
handler with the Unit of Work. |
default void |
attachTransaction(TransactionManager transactionManager)
Attach a transaction to this Unit of Work, using the given
transactionManager . |
void |
commit()
Commits the Unit of Work.
|
default void |
execute(Runnable task)
Execute the given
task in the context of this Unit of Work. |
default void |
execute(Runnable task,
RollbackConfiguration rollbackConfiguration)
Execute the given
task in the context of this Unit of Work. |
default <R> ResultMessage<R> |
executeWithResult(Callable<R> task)
Execute the given
task in the context of this Unit of Work. |
<R> ResultMessage<R> |
executeWithResult(Callable<R> task,
RollbackConfiguration rollbackConfiguration)
Execute the given
task in the context of this Unit of Work. |
MetaData |
getCorrelationData()
Get the correlation data contained in the
message being processed by the Unit of Work. |
ExecutionResult |
getExecutionResult()
Get the result of the task that was executed by this Unit of Work.
|
T |
getMessage()
Get the message that is being processed by the Unit of Work.
|
default <R> R |
getOrComputeResource(String key,
Function<? super String,R> mappingFunction)
Returns the resource attached under given
name . |
default <R> R |
getOrDefaultResource(String key,
R defaultValue)
Returns the resource attached under given
name . |
default <R> R |
getResource(String name)
Returns the resource attached under given
name , or null if no such resource is
available. |
default boolean |
isActive()
Indicates whether this UnitOfWork is started.
|
default boolean |
isCurrent()
Check if the Unit of Work is the 'currently' active Unit of Work returned by
CurrentUnitOfWork.get() . |
boolean |
isRolledBack()
Check if the Unit of Work has been rolled back.
|
default boolean |
isRoot()
Check that returns
true if this Unit of Work has not got a parent. |
void |
onCleanup(Consumer<UnitOfWork<T>> handler)
Register given
handler with the Unit of Work. |
void |
onCommit(Consumer<UnitOfWork<T>> handler)
Register given
handler with the Unit of Work. |
void |
onPrepareCommit(Consumer<UnitOfWork<T>> handler)
Register given
handler with the Unit of Work. |
void |
onRollback(Consumer<UnitOfWork<T>> handler)
Register given
handler with the Unit of Work. |
Optional<UnitOfWork<?>> |
parent()
Returns an optional for the parent of this Unit of Work.
|
UnitOfWork.Phase |
phase()
Returns the current phase of the Unit of Work.
|
void |
registerCorrelationDataProvider(CorrelationDataProvider correlationDataProvider)
Register given
correlationDataProvider with this Unit of Work. |
Map<String,Object> |
resources()
Returns a mutable map of resources registered with the Unit of Work.
|
default void |
rollback()
Initiates the rollback of this Unit of Work, invoking all registered rollback (
and
clean-up handlers {@link #onCleanup(Consumer)} respectively. |
void |
rollback(Throwable cause)
Initiates the rollback of this Unit of Work, invoking all registered rollback (
and
clean-up handlers {@link #onCleanup(Consumer)} respectively. |
default UnitOfWork<?> |
root()
Returns the root of this Unit of Work.
|
void |
start()
Starts the current unit of work.
|
UnitOfWork<T> |
transformMessage(Function<T,? extends Message<?>> transformOperator)
Transform the Message being processed using the given operator and stores the result.
|
void start()
void commit()
onCleanup(Consumer)
}) will be
invoked and the Unit of Work is deregistered from the CurrentUnitOfWork
.
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.IllegalStateException
- if the UnitOfWork wasn't started or if the Unit of Work is not the 'current' Unit
of Work returned by CurrentUnitOfWork.get()
.default void rollback()
and
clean-up handlers {@link #onCleanup(Consumer)}
respectively. Finally, the Unit of Work is deregistered from the
CurrentUnitOfWork
.
If the rollback is a result of an exception, consider using rollback(Throwable)
instead.IllegalStateException
- if the Unit of Work is not in a compatible phase.void rollback(Throwable cause)
and
clean-up handlers {@link #onCleanup(Consumer)}
respectively. Finally, the Unit of Work is deregistered from the
CurrentUnitOfWork
.cause
- The cause of the rollback. May be null
.IllegalStateException
- if the Unit of Work is not in a compatible phase.default boolean isActive()
start()
method has been called, and
if the UnitOfWork has not been committed or rolled back.true
if this UnitOfWork is started, false
otherwise.UnitOfWork.Phase phase()
void onPrepareCommit(Consumer<UnitOfWork<T>> handler)
handler
with the Unit of Work. The handler will be notified when the phase of the
Unit of Work changes to UnitOfWork.Phase.PREPARE_COMMIT
.handler
- the handler to register with the Unit of Workvoid onCommit(Consumer<UnitOfWork<T>> handler)
handler
with the Unit of Work. The handler will be notified when the phase of the
Unit of Work changes to UnitOfWork.Phase.COMMIT
.handler
- the handler to register with the Unit of Workvoid afterCommit(Consumer<UnitOfWork<T>> handler)
handler
with the Unit of Work. The handler will be notified when the phase of the
Unit of Work changes to UnitOfWork.Phase.AFTER_COMMIT
.handler
- the handler to register with the Unit of Workvoid onRollback(Consumer<UnitOfWork<T>> handler)
handler
with the Unit of Work. The handler will be notified when the phase of the
Unit of Work changes to UnitOfWork.Phase.ROLLBACK
. On rollback, the cause for the rollback can obtained from the
suppliedhandler
- the handler to register with the Unit of Workvoid onCleanup(Consumer<UnitOfWork<T>> handler)
handler
with the Unit of Work. The handler will be notified when the phase of the
Unit of Work changes to UnitOfWork.Phase.CLEANUP
.handler
- the handler to register with the Unit of WorkOptional<UnitOfWork<?>> parent()
default boolean isRoot()
true
if this Unit of Work has not got a parent.true
if this Unit of Work has no parentdefault UnitOfWork<?> root()
parent()
) it returns
itself, otherwise it returns the root of its parent.T getMessage()
UnitOfWork<T> transformMessage(Function<T,? extends Message<?>> transformOperator)
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.
transformOperator
- The transform operator to apply to the stored messageMetaData getCorrelationData()
message
being processed by the Unit of Work.
By default this correlation data will be copied to other messages
created in the context of this
Unit of Work, so long as these messages extend from GenericMessage
.void registerCorrelationDataProvider(CorrelationDataProvider correlationDataProvider)
correlationDataProvider
with this Unit of Work. Correlation data providers are used
to provide meta data based on this Unit of Work's Message
when getCorrelationData()
is invoked.correlationDataProvider
- the Correlation Data Provider to registerMap<String,Object> resources()
default <R> R getResource(String name)
name
, or null
if no such resource is
available.R
- The type of resourcename
- The name under which the resource was attachedname
, or null
if no resource was found.default <R> R getOrComputeResource(String key, Function<? super String,R> mappingFunction)
name
. If there is no resource mapped to the given key yet
the mappingFunction
is invoked to provide the mapping.R
- The type of resourcekey
- The name under which the resource was attachedmappingFunction
- The function that provides the mapping if there is no mapped resource yetkey
, or the resource returned by the
mappingFunction
if no resource was found.default <R> R getOrDefaultResource(String key, R defaultValue)
name
. If there is no resource mapped to the given key,
the defaultValue
is returned.R
- The type of resourcekey
- The name under which the resource was attacheddefaultValue
- The value to return if no mapping is availablekey
, or the resource returned by the
mappingFunction
if no resource was found.default void attachTransaction(TransactionManager transactionManager)
transactionManager
. 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.transactionManager
- The Transaction Manager to create, commit and/or rollback the transactiondefault void execute(Runnable task)
task
in 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.task
- the task to executedefault void execute(Runnable task, RollbackConfiguration rollbackConfiguration)
task
in 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, the rollbackConfiguration
determines if the Unit of Work should be rolled back or committed,
and the exception is thrown.task
- the task to executerollbackConfiguration
- configuration that determines whether or not to rollback the unit of work when task
execution failsdefault <R> ResultMessage<R> executeWithResult(Callable<R> task)
task
in 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.R
- the type of result that is returned after successful executiontask
- the task to execute<R> ResultMessage<R> executeWithResult(Callable<R> task, @Nonnull RollbackConfiguration rollbackConfiguration)
task
in 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, the rollbackConfiguration
determines if the Unit of Work should be rolled back or
committed.R
- the type of result that is returned after successful executiontask
- the task to executerollbackConfiguration
- configuration that determines whether or not to rollback the unit of work when task
execution failsExecutionResult getExecutionResult()
null
.
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 check isRolledBack()
.
null
if the Unit of Work has not
been given a task to execute.boolean isRolledBack()
true
if the unit of work was rolled back, false
otherwise.default boolean isCurrent()
CurrentUnitOfWork.get()
.true
if the Unit of Work is the currently active Unit of WorkCopyright © 2010–2024. All rights reserved.