public abstract class AbstractUnitOfWork<T extends Message<?>> extends Object implements UnitOfWork<T>
UnitOfWork.Phase
Constructor and Description |
---|
AbstractUnitOfWork() |
Modifier and Type | Method and Description |
---|---|
protected abstract void |
addHandler(UnitOfWork.Phase phase,
Consumer<UnitOfWork<T>> handler)
Register the given
handler with the Unit of Work. |
void |
afterCommit(Consumer<UnitOfWork<T>> handler)
Register given
handler with the Unit of Work. |
protected void |
changePhase(UnitOfWork.Phase... phases)
Ask the unit of work to transition to the given
phases sequentially. |
void |
commit()
Commits the Unit of Work.
|
protected Collection<CorrelationDataProvider> |
correlationDataProviders()
Provides the collection of registered Correlation Data Providers of this Unit of Work.
|
MetaData |
getCorrelationData()
Get the correlation data contained in the
message being processed by the Unit of Work. |
boolean |
isRolledBack()
Check if the Unit of Work has been rolled back.
|
protected abstract void |
notifyHandlers(UnitOfWork.Phase phase)
Notify the handlers attached to the given
phase . |
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.
|
void |
rollback(Throwable cause)
Initiates the rollback of this Unit of Work, invoking all registered rollback (
and
clean-up handlers {@link #onCleanup(Consumer)} respectively. |
protected abstract void |
setExecutionResult(ExecutionResult executionResult)
Set the execution result of processing the current
Message . |
protected void |
setPhase(UnitOfWork.Phase phase)
Overwrite the current phase with the given
phase . |
protected abstract void |
setRollbackCause(Throwable cause)
Sets the cause for rolling back this Unit of Work.
|
void |
start()
Starts the current unit of work.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
attachTransaction, execute, execute, executeWithResult, executeWithResult, getExecutionResult, getMessage, getOrComputeResource, getOrDefaultResource, getResource, isActive, isCurrent, isRoot, rollback, root, transformMessage
public void start()
UnitOfWork
start
in interface UnitOfWork<T extends Message<?>>
public void commit()
UnitOfWork
UnitOfWork.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.commit
in interface UnitOfWork<T extends Message<?>>
public void rollback(Throwable cause)
UnitOfWork
and
clean-up handlers {@link #onCleanup(Consumer)}
respectively. Finally, the Unit of Work is deregistered from the
CurrentUnitOfWork
.rollback
in interface UnitOfWork<T extends Message<?>>
cause
- The cause of the rollback. May be null
.public Optional<UnitOfWork<?>> parent()
UnitOfWork
parent
in interface UnitOfWork<T extends Message<?>>
public Map<String,Object> resources()
UnitOfWork
resources
in interface UnitOfWork<T extends Message<?>>
public boolean isRolledBack()
UnitOfWork
isRolledBack
in interface UnitOfWork<T extends Message<?>>
true
if the unit of work was rolled back, false
otherwise.public void registerCorrelationDataProvider(CorrelationDataProvider correlationDataProvider)
UnitOfWork
correlationDataProvider
with this Unit of Work. Correlation data providers are used
to provide meta data based on this Unit of Work's Message
when UnitOfWork.getCorrelationData()
is invoked.registerCorrelationDataProvider
in interface UnitOfWork<T extends Message<?>>
correlationDataProvider
- the Correlation Data Provider to registerpublic MetaData getCorrelationData()
UnitOfWork
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
.getCorrelationData
in interface UnitOfWork<T extends Message<?>>
public void onPrepareCommit(Consumer<UnitOfWork<T>> handler)
UnitOfWork
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
.onPrepareCommit
in interface UnitOfWork<T extends Message<?>>
handler
- the handler to register with the Unit of Workpublic void onCommit(Consumer<UnitOfWork<T>> handler)
UnitOfWork
handler
with the Unit of Work. The handler will be notified when the phase of the
Unit of Work changes to UnitOfWork.Phase.COMMIT
.onCommit
in interface UnitOfWork<T extends Message<?>>
handler
- the handler to register with the Unit of Workpublic void afterCommit(Consumer<UnitOfWork<T>> handler)
UnitOfWork
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
.afterCommit
in interface UnitOfWork<T extends Message<?>>
handler
- the handler to register with the Unit of Workpublic void onRollback(Consumer<UnitOfWork<T>> handler)
UnitOfWork
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
suppliedonRollback
in interface UnitOfWork<T extends Message<?>>
handler
- the handler to register with the Unit of Workpublic void onCleanup(Consumer<UnitOfWork<T>> handler)
UnitOfWork
handler
with the Unit of Work. The handler will be notified when the phase of the
Unit of Work changes to UnitOfWork.Phase.CLEANUP
.onCleanup
in interface UnitOfWork<T extends Message<?>>
handler
- the handler to register with the Unit of Workpublic UnitOfWork.Phase phase()
UnitOfWork
phase
in interface UnitOfWork<T extends Message<?>>
protected void setPhase(UnitOfWork.Phase phase)
phase
.phase
- the new phase of the Unit of Workprotected void changePhase(UnitOfWork.Phase... phases)
phases
sequentially. 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.phases
- The phases to transition to in sequential orderprotected Collection<CorrelationDataProvider> correlationDataProviders()
protected abstract void notifyHandlers(UnitOfWork.Phase phase)
phase
.phase
- The phase for which to invoke registered handlers.protected abstract void addHandler(UnitOfWork.Phase phase, Consumer<UnitOfWork<T>> handler)
handler
with the Unit of Work. The handler will be invoked when the
Unit of Work changes its phase to the given phase
.phase
- the Phase of the Unit of Work at which to invoke the handlerhandler
- the handler to addprotected abstract void setExecutionResult(ExecutionResult executionResult)
Message
.executionResult
- the ExecutionResult of the currently handled Messageprotected abstract void setRollbackCause(Throwable cause)
cause
- The cause for rolling back this Unit of WorkCopyright © 2010–2024. All rights reserved.