org.axonframework.unitofwork
Class DefaultUnitOfWork

java.lang.Object
  extended by org.axonframework.unitofwork.NestableUnitOfWork
      extended by org.axonframework.unitofwork.DefaultUnitOfWork
All Implemented Interfaces:
UnitOfWork

public class DefaultUnitOfWork
extends NestableUnitOfWork

Implementation of the UnitOfWork that buffers all published events until it is committed. Aggregates that have not been explicitly save in their aggregates will be saved when the UnitOfWork commits.

This implementation requires a mechanism that explicitly commits or rolls back.

Since:
0.6
Author:
Allard Buijze

Constructor Summary
DefaultUnitOfWork()
          Initializes a Unit of Work (without starting it) that is not bound to any transaction.
DefaultUnitOfWork(TransactionManager<?> transactionManager)
          Initializes a Unit of Work (without starting it) that is binds to the transaction created by the given transactionManager when the Unit of Work starts.
 
Method Summary
protected  void doCommit()
          Executes the logic required to commit this unit of work.
protected  void doRollback(Throwable cause)
          Executes the logic required to commit this unit of work.
protected  void doStart()
          Performs logic required when starting this UnitOfWork instance.
 boolean isTransactional()
          Indicates whether this UnitOfWork is bound to a transaction.
protected  void notifyListenersAfterCommit()
          Send a UnitOfWorkListener.afterCommit(UnitOfWork) notification to all registered listeners.
protected  void notifyListenersCleanup()
          Send a UnitOfWorkListener.onCleanup(UnitOfWork) notification to all registered listeners.
protected  void notifyListenersPrepareCommit()
          Send a UnitOfWorkListener.onPrepareCommit(UnitOfWork, java.util.Set, java.util.List) notification to all registered listeners.
protected  void notifyListenersPrepareTransactionCommit(Object transaction)
          Send a UnitOfWorkListener.afterCommit(UnitOfWork) notification to all registered listeners.
protected  void notifyListenersRollback(Throwable cause)
          Send a UnitOfWorkListener.onRollback(UnitOfWork, Throwable) notification to all registered listeners.
protected  void publishEvents()
          Publishes all registered events to their respective event bus.
<T extends AggregateRoot>
T
registerAggregate(T aggregate, EventBus eventBus, SaveAggregateCallback<T> saveAggregateCallback)
          Register an aggregate with this UnitOfWork.
 void registerForPublication(EventMessage<?> event, EventBus eventBus, boolean notifyRegistrationHandlers)
          Register the given event for publication on the given eventBus when the unit of work is committed.
 void registerListener(UnitOfWorkListener listener)
          Register a listener that listens to state changes in this UnitOfWork.
protected  void registerScheduledEvents(UnitOfWork unitOfWork)
          Invokes when a child Unit of Work should register its scheduled events with the given unitOfWork.
protected  void saveAggregates()
          Saves all registered aggregates by calling their respective callbacks.
static UnitOfWork startAndGet()
          Starts a new DefaultUnitOfWork instance, registering it a CurrentUnitOfWork.
static UnitOfWork startAndGet(TransactionManager<?> transactionManager)
          Starts a new DefaultUnitOfWork instance using the given transactionManager to provide a backing transaction, registering it a CurrentUnitOfWork.
 
Methods inherited from class org.axonframework.unitofwork.NestableUnitOfWork
attachInheritedResources, attachResource, attachResource, commit, commitInnerUnitOfWork, getResource, isStarted, publishEvent, rollback, rollback, start
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultUnitOfWork

public DefaultUnitOfWork()
Initializes a Unit of Work (without starting it) that is not bound to any transaction.


DefaultUnitOfWork

public DefaultUnitOfWork(TransactionManager<?> transactionManager)
Initializes a Unit of Work (without starting it) that is binds to the transaction created by the given transactionManager when the Unit of Work starts.

Parameters:
transactionManager - The transaction manager to manage the transaction around this Unit of Work
Method Detail

startAndGet

public static UnitOfWork startAndGet()
Starts a new DefaultUnitOfWork instance, registering it a CurrentUnitOfWork. This methods returns the started UnitOfWork instance.

Note that this Unit Of Work type is not meant to be shared among different Threads. A single DefaultUnitOfWork instance should be used exclusively by the Thread that created it.

Returns:
the started UnitOfWork instance

startAndGet

public static UnitOfWork startAndGet(TransactionManager<?> transactionManager)
Starts a new DefaultUnitOfWork instance using the given transactionManager to provide a backing transaction, registering it a CurrentUnitOfWork. This methods returns the started UnitOfWork instance.

Note that this Unit Of Work type is not meant to be shared among different Threads. A single DefaultUnitOfWork instance should be used exclusively by the Thread that created it.

Parameters:
transactionManager - The transaction manager to provide the backing transaction. May be null when not using transactions.
Returns:
the started UnitOfWork instance

doStart

protected void doStart()
Description copied from class: NestableUnitOfWork
Performs logic required when starting this UnitOfWork instance.

Specified by:
doStart in class NestableUnitOfWork

isTransactional

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

Returns:
true if this unit of work is bound to a transaction, otherwise false

doRollback

protected void doRollback(Throwable cause)
Description copied from class: NestableUnitOfWork
Executes the logic required to commit this unit of work.

Specified by:
doRollback in class NestableUnitOfWork
Parameters:
cause - the cause of the rollback

doCommit

protected void doCommit()
Description copied from class: NestableUnitOfWork
Executes the logic required to commit this unit of work.

Specified by:
doCommit in class NestableUnitOfWork

registerScheduledEvents

protected void registerScheduledEvents(UnitOfWork unitOfWork)
Description copied from class: NestableUnitOfWork
Invokes when a child Unit of Work should register its scheduled events with the given unitOfWork. Typically, the given unitOfWork is the parent of the current.

Specified by:
registerScheduledEvents in class NestableUnitOfWork
Parameters:
unitOfWork - The Unit of Work to register scheduled events with
See Also:
UnitOfWork.publishEvent(org.axonframework.domain.EventMessage, org.axonframework.eventhandling.EventBus)

registerAggregate

public <T extends AggregateRoot> T registerAggregate(T aggregate,
                                                     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:

.

Type Parameters:
T - the type of aggregate to register
Parameters:
aggregate - 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

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.

Parameters:
listener - The listener to notify when the UnitOfWork's state changes.

registerForPublication

public void registerForPublication(EventMessage<?> event,
                                   EventBus eventBus,
                                   boolean notifyRegistrationHandlers)
Description copied from class: NestableUnitOfWork
Register the given event for publication on the given eventBus when the unit of work is committed. This method will only be invoked on the outer unit of work, as that one is responsible for maintaining the order of publication of events.

The notifyRegistrationHandlers parameter indicates whether the registration handlers should be notified of the registration of this event.

Specified by:
registerForPublication in class NestableUnitOfWork
Parameters:
event - The Event to publish
eventBus - The Event Bus to publish the Event on
notifyRegistrationHandlers - Indicates whether event registration handlers should be notified of this event

notifyListenersRollback

protected void notifyListenersRollback(Throwable cause)
Description copied from class: NestableUnitOfWork
Send a UnitOfWorkListener.onRollback(UnitOfWork, Throwable) notification to all registered listeners.

Specified by:
notifyListenersRollback in class NestableUnitOfWork
Parameters:
cause - The cause of the rollback

notifyListenersPrepareTransactionCommit

protected void notifyListenersPrepareTransactionCommit(Object transaction)
Send a UnitOfWorkListener.afterCommit(UnitOfWork) notification to all registered listeners.

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

notifyListenersAfterCommit

protected void notifyListenersAfterCommit()
Send a UnitOfWorkListener.afterCommit(UnitOfWork) notification to all registered listeners.


publishEvents

protected void publishEvents()
Publishes all registered events to their respective event bus.


saveAggregates

protected void saveAggregates()
Description copied from class: NestableUnitOfWork
Saves all registered aggregates by calling their respective callbacks.

Specified by:
saveAggregates in class NestableUnitOfWork

notifyListenersPrepareCommit

protected void notifyListenersPrepareCommit()
Description copied from class: NestableUnitOfWork
Send a UnitOfWorkListener.onPrepareCommit(UnitOfWork, java.util.Set, java.util.List) notification to all registered listeners.

Specified by:
notifyListenersPrepareCommit in class NestableUnitOfWork

notifyListenersCleanup

protected void notifyListenersCleanup()
Description copied from class: NestableUnitOfWork
Send a UnitOfWorkListener.onCleanup(UnitOfWork) notification to all registered listeners. The implementation must ensure that all listeners are notified, even if one throws an exception.

Specified by:
notifyListenersCleanup in class NestableUnitOfWork


Copyright © 2010-2016. All Rights Reserved.