org.axonframework.unitofwork
Class NestableUnitOfWork

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

public abstract class NestableUnitOfWork
extends Object
implements UnitOfWork

Abstract implementation of the UnitOfWork interface. Provides the necessary implementations to support most actions required by any Unit of Work, such as managing registration with the CurrentUnitOfWork and support for nesting a Unit of Work.

Since:
0.7
Author:
Allard Buijze
See Also:
CurrentUnitOfWork

Constructor Summary
NestableUnitOfWork()
           
 
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.
protected  void commitInnerUnitOfWork()
          Commit all registered inner units of work.
protected abstract  void doCommit()
          Executes the logic required to commit this unit of work.
protected abstract  void doRollback(Throwable cause)
          Executes the logic required to commit this unit of work.
protected abstract  void doStart()
          Performs logic required when starting this UnitOfWork instance.
<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.
protected abstract  void notifyListenersCleanup()
          Send a UnitOfWorkListener.onCleanup(UnitOfWork) notification to all registered listeners.
protected abstract  void notifyListenersPrepareCommit()
          Send a UnitOfWorkListener.onPrepareCommit(UnitOfWork, java.util.Set, java.util.List) notification to all registered listeners.
protected abstract  void notifyListenersRollback(Throwable cause)
          Send a UnitOfWorkListener.onRollback(UnitOfWork, Throwable) notification to all registered listeners.
 void publishEvent(EventMessage<?> event, EventBus eventBus)
          Request to publish the given event on the given eventBus.
protected abstract  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.
protected abstract  void registerScheduledEvents(UnitOfWork unitOfWork)
          Invokes when a child Unit of Work should register its scheduled events with the given unitOfWork.
 void rollback()
          Clear the UnitOfWork of any buffered changes.
 void rollback(Throwable cause)
          Clear the UnitOfWork of any buffered changes.
protected abstract  void saveAggregates()
          Saves all registered aggregates by calling their respective callbacks.
 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
 
Methods inherited from interface org.axonframework.unitofwork.UnitOfWork
isTransactional, registerAggregate, registerListener
 

Constructor Detail

NestableUnitOfWork

public NestableUnitOfWork()
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

registerScheduledEvents

protected abstract void registerScheduledEvents(UnitOfWork unitOfWork)
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.

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

notifyListenersCleanup

protected abstract void notifyListenersCleanup()
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.


notifyListenersRollback

protected abstract void notifyListenersRollback(Throwable cause)
Send a UnitOfWorkListener.onRollback(UnitOfWork, Throwable) notification to all registered listeners.

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

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

registerForPublication

protected abstract 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. 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.

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

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.

doStart

protected abstract void doStart()
Performs logic required when starting this UnitOfWork instance.


doCommit

protected abstract void doCommit()
Executes the logic required to commit this unit of work.


doRollback

protected abstract void doRollback(Throwable cause)
Executes the logic required to commit this unit of work.

Parameters:
cause - the cause of the rollback

commitInnerUnitOfWork

protected void commitInnerUnitOfWork()
Commit all registered inner units of work. This should be invoked after events have been dispatched and before any listeners are notified of the commit.


saveAggregates

protected abstract void saveAggregates()
Saves all registered aggregates by calling their respective callbacks.


notifyListenersPrepareCommit

protected abstract void notifyListenersPrepareCommit()
Send a UnitOfWorkListener.onPrepareCommit(UnitOfWork, java.util.Set, java.util.List) notification to all registered listeners.


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.


Copyright © 2010-2016. All Rights Reserved.