Class BatchingUnitOfWork<T extends Message<?>>
java.lang.Object
org.axonframework.messaging.unitofwork.AbstractUnitOfWork<T>
org.axonframework.messaging.unitofwork.BatchingUnitOfWork<T>
- Type Parameters:
T- The type of message handled by this Unit of Work
- All Implemented Interfaces:
UnitOfWork<T>
Unit of Work implementation that is able to process a batch of Messages instead of just a single Message.
- Since:
- 3.0
- Author:
- Rene de Waele, Allard Buijze
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.axonframework.messaging.unitofwork.UnitOfWork
UnitOfWork.Phase -
Constructor Summary
ConstructorsConstructorDescriptionBatchingUnitOfWork(List<T> messages) Initializes a BatchingUnitOfWork for processing the given list ofmessages.BatchingUnitOfWork(T... messages) Initializes a BatchingUnitOfWork for processing the given batch ofmessages. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddHandler(UnitOfWork.Phase phase, Consumer<UnitOfWork<T>> handler) Register the givenhandlerwith the Unit of Work.<R> ResultMessage<R> executeWithResult(Callable<R> task, RollbackConfiguration rollbackConfiguration) Execute the giventaskin the context of this Unit of Work.Get the result of the task that was executed by this Unit of Work.Returns a Map ofExecutionResultper Message.Get the message that is being processed by the Unit of Work.Get the batch of messages that is being processed (or has been processed) by this unit of work.booleanChecks if the message being processed now is the first of the batch being processed in this unit of work.booleanisFirstMessage(Message<?> message) Checks if the givenmessageis the first of the batch being processed in this unit of work.booleanChecks if the message being processed now is the last of the batch being processed in this unit of work.booleanisLastMessage(Message<?> message) Checks if the givenmessageis the last of the batch being processed in this unit of work.protected voidnotifyHandlers(UnitOfWork.Phase phase) Notify the handlers attached to the givenphase.protected voidsetExecutionResult(ExecutionResult executionResult) Set the execution result of processing the currentMessage.protected voidsetRollbackCause(Throwable cause) Sets the cause for rolling back this Unit of Work.transformMessage(Function<T, ? extends Message<?>> transformOperator) Transform the Message being processed using the given operator and stores the result.Methods inherited from class org.axonframework.messaging.unitofwork.AbstractUnitOfWork
afterCommit, changePhase, commit, correlationDataProviders, getCorrelationData, isRolledBack, onCleanup, onCommit, onPrepareCommit, onRollback, parent, phase, registerCorrelationDataProvider, resources, rollback, setPhase, startMethods 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, getOrComputeResource, getOrDefaultResource, getResource, isActive, isCurrent, isRoot, rollback, root
-
Constructor Details
-
BatchingUnitOfWork
Initializes a BatchingUnitOfWork for processing the given batch ofmessages.- Parameters:
messages- batch of messages to process
-
BatchingUnitOfWork
Initializes a BatchingUnitOfWork for processing the given list ofmessages.- Parameters:
messages- batch of messages to process
-
-
Method Details
-
executeWithResult
public <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.
This implementation executes the giventaskfor each of its messages. The return value is the result of the last executed task.- 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
-
getExecutionResults
Returns a Map ofExecutionResultper Message. If the Unit of Work has not been given a task to execute, the ExecutionResult isnullfor each Message.- Returns:
- a Map of ExecutionResult per Message processed by this Unit of Work
-
getMessage
Description copied from interface:UnitOfWorkGet 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
Description copied from interface:UnitOfWorkTransform 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
-
getExecutionResult
Description copied from interface:UnitOfWorkGet 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 checkUnitOfWork.isRolledBack().- 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.
-
setExecutionResult
Description copied from class:AbstractUnitOfWorkSet the execution result of processing the currentMessage.- Specified by:
setExecutionResultin classAbstractUnitOfWork<T extends Message<?>>- Parameters:
executionResult- the ExecutionResult of the currently handled Message
-
notifyHandlers
Description copied from class:AbstractUnitOfWorkNotify the handlers attached to the givenphase.- Specified by:
notifyHandlersin classAbstractUnitOfWork<T extends Message<?>>- Parameters:
phase- The phase for which to invoke registered handlers.
-
setRollbackCause
Description copied from class:AbstractUnitOfWorkSets the cause for rolling back this Unit of Work.- Specified by:
setRollbackCausein classAbstractUnitOfWork<T extends Message<?>>- Parameters:
cause- The cause for rolling back this Unit of Work
-
addHandler
Description copied from class:AbstractUnitOfWorkRegister the givenhandlerwith the Unit of Work. The handler will be invoked when the Unit of Work changes its phase to the givenphase.- Specified by:
addHandlerin classAbstractUnitOfWork<T extends Message<?>>- Parameters:
phase- the Phase of the Unit of Work at which to invoke the handlerhandler- the handler to add
-
getMessages
Get the batch of messages that is being processed (or has been processed) by this unit of work.- Returns:
- the message batch
-
isLastMessage
Checks if the givenmessageis the last of the batch being processed in this unit of work.- Parameters:
message- the message to check for- Returns:
trueif the message is the last of this batch,falseotherwise
-
isLastMessage
public boolean isLastMessage()Checks if the message being processed now is the last of the batch being processed in this unit of work.- Returns:
trueif the message is the last of this batch,falseotherwise
-
isFirstMessage
Checks if the givenmessageis the first of the batch being processed in this unit of work.- Parameters:
message- the message to check- Returns:
trueif the message is the first of this batch,falseotherwise
-
isFirstMessage
public boolean isFirstMessage()Checks if the message being processed now is the first of the batch being processed in this unit of work.- Returns:
trueif the message is the first of this batch,falseotherwise
-