org.axonframework.commandhandling.disruptor
Class DisruptorUnitOfWork

java.lang.Object
  extended by org.axonframework.commandhandling.disruptor.DisruptorUnitOfWork
All Implemented Interfaces:
EventRegistrationCallback, UnitOfWork

public class DisruptorUnitOfWork
extends Object
implements UnitOfWork, EventRegistrationCallback

Specialized UnitOfWork instance for the DisruptorCommandBus. It expects the executing command to target a single aggregate instance.

Since:
2.0
Author:
Allard Buijze

Constructor Summary
DisruptorUnitOfWork(boolean transactional)
          Creates a new Unit of Work for use in the DisruptorCommandBus.
 
Method Summary
 void attachInheritedResources(UnitOfWork inheritingUnitOfWork)
          Attach all inherited resources to the given unitOfWork.
 void attachResource(String name, Object resource)
          Attaches the given resource to this Unit of Work under the given name.
 void attachResource(String name, Object resource, boolean inherited)
          Attaches the given resource to this Unit of Work under the given name.
 void commit()
          Commits the UnitOfWork.
 EventSourcedAggregateRoot getAggregate()
          Returns the identifier of the aggregate modified in this UnitOfWork.
 String getAggregateType()
          Returns the type identifier of the aggregate handled in this unit of work.
 List<EventMessage> getEventsToPublish()
          Returns the events that need to be published as part of this Unit of Work.
 DomainEventStream getEventsToStore()
          Returns the events that need to be stored as part of this Unit of Work.
<T> T
getResource(String name)
          Returns the resource previously attached under given name, or null if no such resource is available.
 boolean isStarted()
          Indicates whether this UnitOfWork is started.
 boolean isTransactional()
          Indicates whether this UnitOfWork is bound to a transaction.
 void onAfterCommit()
          Invokes this UnitOfWork's on-after-commit cycle.
 void onCleanup()
          Invokes this UnitOfWork's on-cleanup cycle.
 void onPrepareCommit()
          Invokes this UnitOfWork's on-prepare-commit cycle.
 void onPrepareTransactionCommit(Object transaction)
          Invokes this UnitOfWork's on-prepare-transaction-commit cycle.
<T> DomainEventMessage<T>
onRegisteredEvent(DomainEventMessage<T> event)
          Invoked when an Aggregate registers an Event for publication.
 void onRollback(Throwable cause)
          Invokes this UnitOfWork's on-rollback cycle.
 void publishEvent(EventMessage event, EventBus eventBus)
          Request to publish the given event on the given eventBus.
<T extends AggregateRoot>
T
registerAggregate(T aggregateRoot, EventBus eventBus, SaveAggregateCallback<T> saveAggregateCallback)
          Register an aggregate with this UnitOfWork.
 void registerListener(UnitOfWorkListener listener)
          Register a listener that listens to state changes in this UnitOfWork.
 void rollback()
          Clear the UnitOfWork of any buffered changes.
 void rollback(Throwable cause)
          Clear the UnitOfWork of any buffered changes.
 void setAggregateType(String aggregateType)
          Sets the type identifier of the aggregate handled in this unit of work
 void setEventStreamDecorator(EventStreamDecorator eventStreamDecorator)
          Registers the EventStreamDecorator for events as part of this unit of work
 void start()
          Starts the current unit of work, preparing it for aggregate registration.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DisruptorUnitOfWork

public DisruptorUnitOfWork(boolean transactional)
Creates a new Unit of Work for use in the DisruptorCommandBus.

Parameters:
transactional - Whether this Unit of Work is bound to a transaction
Method Detail

commit

public void commit()
Description copied from interface: UnitOfWork
Commits the UnitOfWork. All registered aggregates that have not been registered as stored are saved in their respective repositories, buffered events are sent to their respective event bus, and all registered UnitOfWorkListeners are notified.

After the commit (successful or not), the UnitOfWork is unregistered from the CurrentUnitOfWork and has cleaned up all resources it occupied. This effectively means that a rollback is done if Unit Of Work failed to commit.

Specified by:
commit in interface UnitOfWork

onPrepareCommit

public void onPrepareCommit()
Invokes this UnitOfWork's on-prepare-commit cycle. Typically, this is run after the actual aggregates have been committed, but before any of the changes are made public.


onPrepareTransactionCommit

public void onPrepareTransactionCommit(Object transaction)
Invokes this UnitOfWork's on-prepare-transaction-commit cycle.

Parameters:
transaction - The object representing the transaction to about to be committed

onAfterCommit

public void onAfterCommit()
Invokes this UnitOfWork's on-after-commit cycle. Typically, this is run after all the events have been stored and published.


onCleanup

public void onCleanup()
Invokes this UnitOfWork's on-cleanup cycle. Typically, this is run after all the events have been stored and published and the after-commit cycle has been executed.


onRollback

public void onRollback(Throwable cause)
Invokes this UnitOfWork's on-rollback cycle. Typically, this is run after all the events have been stored and published and the after-commit cycle has been executed.

Parameters:
cause - The cause of the rollback

rollback

public void rollback()
Description copied from interface: UnitOfWork
Clear the UnitOfWork of any buffered changes. All buffered events and registered aggregates are discarded and registered UnitOfWorkListeners are notified.

If the rollback is a result of an exception, consider using UnitOfWork.rollback(Throwable) instead.

Specified by:
rollback in interface UnitOfWork

rollback

public void rollback(Throwable cause)
Description copied from interface: UnitOfWork
Clear the UnitOfWork of any buffered changes. All buffered events and registered aggregates are discarded and registered UnitOfWorkListeners are notified.

Specified by:
rollback in interface UnitOfWork
Parameters:
cause - The cause of the rollback. May be null.

start

public void start()
Description copied from interface: UnitOfWork
Starts the current unit of work, preparing it for aggregate registration. The UnitOfWork instance is registered with the CurrentUnitOfWork.

Specified by:
start in interface UnitOfWork

isStarted

public boolean isStarted()
Description copied from interface: UnitOfWork
Indicates whether this UnitOfWork is started. It is started when the UnitOfWork.start() method has been called, and if the UnitOfWork has not been committed or rolled back.

Specified by:
isStarted in interface UnitOfWork
Returns:
true if this UnitOfWork is started, false otherwise.

isTransactional

public boolean isTransactional()
Description copied from interface: UnitOfWork
Indicates whether this UnitOfWork is bound to a transaction.

Specified by:
isTransactional in interface UnitOfWork
Returns:
true if this unit of work is bound to a transaction, otherwise false

registerListener

public void registerListener(UnitOfWorkListener listener)
Description copied from interface: UnitOfWork
Register a listener that listens to state changes in this UnitOfWork. This typically allows components to clean up resources, such as locks, when a UnitOfWork is committed or rolled back. If a UnitOfWork is partially committed, only the listeners bound to one of the committed aggregates is notified.

Specified by:
registerListener in interface UnitOfWork
Parameters:
listener - The listener to notify when the UnitOfWork's state changes.

registerAggregate

public <T extends AggregateRoot> T registerAggregate(T aggregateRoot,
                                                     EventBus eventBus,
                                                     SaveAggregateCallback<T> saveAggregateCallback)
Description copied from interface: UnitOfWork
Register an aggregate with this UnitOfWork. These aggregates will be saved (at the latest) when the UnitOfWork is committed. This method returns the instance of the aggregate root that should be used as part of the processing in this Unit Of Work.

If an aggregate of the same type and with the same identifier has already been registered, one of two things may happen, depending on the actual implementation:

.

Specified by:
registerAggregate in interface UnitOfWork
Type Parameters:
T - the type of aggregate to register
Parameters:
aggregateRoot - The aggregate root to register in the UnitOfWork
eventBus - The event bus on which Events generated by this aggregate must be published
saveAggregateCallback - The callback that is invoked when the UnitOfWork wants to store the registered aggregate
Returns:
The actual aggregate instance to use

attachResource

public void attachResource(String name,
                           Object resource)
Description copied from interface: UnitOfWork
Attaches the given resource to this Unit of Work under the given name. The attached resource is not inherited by any nested Unit of Work (see UnitOfWork.attachResource(String, Object, boolean).

If a resource was already attached under this name, it is overwritten.

By convention, resources should be attached using the fully Qualified name of their type (or main interface). For example, a JDBC Connection should be attached using the name "java.sql.Connection". When there is a requirement to distinguish between different resources of the same type, their name may differ from this convention.

Specified by:
attachResource in interface UnitOfWork
Parameters:
name - The name under which to attach the resource
resource - The resource to attach

attachResource

public void attachResource(String name,
                           Object resource,
                           boolean inherited)
Description copied from interface: UnitOfWork
Attaches the given resource to this Unit of Work under the given name. The attached resource is inherited as indicated. Inherited resources are automatically attached to a nested Unit of Work.

If a resource was already attached under this name, it is overwritten.

By convention, resources should be attached using the fully Qualified name of their type (or main interface). For example, a JDBC Connection should be attached using the name "java.sql.Connection". When there is a requirement to distinguish between different resources of the same type, their name may differ from this convention.

Specified by:
attachResource in interface UnitOfWork
Parameters:
name - The name under which to attach the resource
resource - The resource to attach
inherited - Whether or not the resource may be inherited by a nested Unit of Work

getResource

public <T> T getResource(String name)
Description copied from interface: UnitOfWork
Returns the resource previously attached under given name, or null if no such resource is available.

Specified by:
getResource in interface UnitOfWork
Type Parameters:
T - The type of resource
Parameters:
name - The name under which the resource was attached
Returns:
The resource attached under the given name, or null if no such resource is available.

attachInheritedResources

public void attachInheritedResources(UnitOfWork inheritingUnitOfWork)
Description copied from interface: UnitOfWork
Attach all inherited resources to the given unitOfWork.

Specified by:
attachInheritedResources in interface UnitOfWork
Parameters:
inheritingUnitOfWork - the Unit of Work inheriting the resources from this instance.

publishEvent

public void publishEvent(EventMessage event,
                         EventBus eventBus)
Description copied from interface: UnitOfWork
Request to publish the given event on the given eventBus. The UnitOfWork may either publish immediately, or buffer the events until the UnitOfWork is committed.

Specified by:
publishEvent in interface UnitOfWork
Parameters:
event - The event to be published on the event bus
eventBus - The event bus on which to publish the event

getEventsToStore

public DomainEventStream getEventsToStore()
Returns the events that need to be stored as part of this Unit of Work.

Returns:
the events that need to be stored as part of this Unit of Work

getEventsToPublish

public List<EventMessage> getEventsToPublish()
Returns the events that need to be published as part of this Unit of Work.

Returns:
the events that need to be published as part of this Unit of Work

getAggregate

public EventSourcedAggregateRoot getAggregate()
Returns the identifier of the aggregate modified in this UnitOfWork.

Returns:
the identifier of the aggregate modified in this UnitOfWork

onRegisteredEvent

public <T> DomainEventMessage<T> onRegisteredEvent(DomainEventMessage<T> event)
Description copied from interface: EventRegistrationCallback
Invoked when an Aggregate registers an Event for publication. The simplest implementation will simply return the given event.

Specified by:
onRegisteredEvent in interface EventRegistrationCallback
Type Parameters:
T - The type of payload
Parameters:
event - The event registered for publication
Returns:
the message to actually publish. May not be null.

getAggregateType

public String getAggregateType()
Returns the type identifier of the aggregate handled in this unit of work.

Returns:
the type identifier of the aggregate handled in this unit of work

setAggregateType

public void setAggregateType(String aggregateType)
Sets the type identifier of the aggregate handled in this unit of work

Parameters:
aggregateType - the type identifier of the aggregate handled in this unit of work

setEventStreamDecorator

public void setEventStreamDecorator(EventStreamDecorator eventStreamDecorator)
Registers the EventStreamDecorator for events as part of this unit of work

Parameters:
eventStreamDecorator - The EventStreamDecorator to use for the event streams part of this unit of work


Copyright © 2010-2016. All Rights Reserved.