org.axonframework.unitofwork
Class UnitOfWorkListenerCollection

java.lang.Object
  extended by org.axonframework.unitofwork.UnitOfWorkListenerCollection
All Implemented Interfaces:
UnitOfWorkListener

public class UnitOfWorkListenerCollection
extends Object
implements UnitOfWorkListener

This class is responsible for notifying registered listeners in a specific order of precedence. When onPrepareCommit(UnitOfWork, java.util.Set, java.util.List)} and onEventRegistered(UnitOfWork, org.axonframework.domain.EventMessage) are called the listeners will be handled in the order they have been registered (order of precedence). When afterCommit(UnitOfWork), onRollback(UnitOfWork, Throwable), and onCleanup(UnitOfWork) are called the listeners will be handled in the reversed order of precedence.

This behaviour is particularly useful because the AuditingUnitOfWorkListener should write an entry before any other listeners are allowed to anything (prepare commit) and write an entry only after all other listeners have successfully committed (after commit).

Since:
2.0
Author:
Frank Versnel

Constructor Summary
UnitOfWorkListenerCollection()
           
 
Method Summary
 void add(UnitOfWorkListener listener)
          Adds a listener to the collection.
 void afterCommit(UnitOfWork unitOfWork)
          Invoked when the UnitOfWork is committed.
 void onCleanup(UnitOfWork unitOfWork)
          Notifies listeners that the UnitOfWork is being cleaned up.
<T> EventMessage<T>
onEventRegistered(UnitOfWork unitOfWork, EventMessage<T> event)
          Invoked when an Event is registered for publication when the UnitOfWork is committed.
 void onPrepareCommit(UnitOfWork unitOfWork, Set<AggregateRoot> aggregateRoots, List<EventMessage> events)
          Invoked before aggregates are committed, and before any events are published.
 void onPrepareTransactionCommit(UnitOfWork unitOfWork, Object transaction)
          Invoked before the transaction bound to this Unit of Work is committed, but after all other commit activities (publication of events and saving of aggregates) are performed.
 void onRollback(UnitOfWork unitOfWork, Throwable failureCause)
          Invoked when the UnitOfWork is rolled back.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UnitOfWorkListenerCollection

public UnitOfWorkListenerCollection()
Method Detail

afterCommit

public void afterCommit(UnitOfWork unitOfWork)
Invoked when the UnitOfWork is committed. The aggregate has been saved and the events have been scheduled for dispatching. In some cases, the events could already have been dispatched. When processing of this method causes an exception, a UnitOfWork may choose to call UnitOfWorkListener.onRollback(UnitOfWork, Throwable) consecutively.

Listeners are called in the reversed order of precedence.

Specified by:
afterCommit in interface UnitOfWorkListener
Parameters:
unitOfWork - The Unit of Work being committed
See Also:
UnitOfWork.commit()

onRollback

public void onRollback(UnitOfWork unitOfWork,
                       Throwable failureCause)
Invoked when the UnitOfWork is rolled back. The UnitOfWork may choose to invoke this method when committing the UnitOfWork failed, too.

Listeners are called in the reversed order of precedence.

Specified by:
onRollback in interface UnitOfWorkListener
Parameters:
unitOfWork - The Unit of Work being rolled back
failureCause - The exception (or error) causing the roll back
See Also:
UnitOfWork.rollback(Throwable)

onEventRegistered

public <T> EventMessage<T> onEventRegistered(UnitOfWork unitOfWork,
                                             EventMessage<T> event)
Invoked when an Event is registered for publication when the UnitOfWork is committed. Listeners may alter Event information by returning a new instance for the event. Note that the Listener must ensure the functional meaning of the EventMessage does not change. Typically, this is done by only modifying the MetaData on an Event.

The simplest implementation simply returns the given event.

Listeners are called in the order of precedence.

Specified by:
onEventRegistered in interface UnitOfWorkListener
Type Parameters:
T - The type of payload of the EventMessage
Parameters:
unitOfWork - The Unit of Work on which an event is registered
event - The event about to be registered for publication
Returns:
the (modified) event to register for publication

onPrepareCommit

public void onPrepareCommit(UnitOfWork unitOfWork,
                            Set<AggregateRoot> aggregateRoots,
                            List<EventMessage> events)
Invoked before aggregates are committed, and before any events are published. This phase can be used to do validation or other activity that should be able to prevent event dispatching in certain circumstances.

Note that the given events may not contain the uncommitted domain events of each of the aggregateRoots. To retrieve all events, collect all uncommitted events from the aggregate roots and combine them with the list of events.

Listeners are called in the order of precedence.

Specified by:
onPrepareCommit in interface UnitOfWorkListener
Parameters:
unitOfWork - The Unit of Work being committed
aggregateRoots - the aggregate roots being committed
events - Events that have been registered for dispatching with the UnitOfWork

onPrepareTransactionCommit

public void onPrepareTransactionCommit(UnitOfWork unitOfWork,
                                       Object transaction)
Description copied from interface: UnitOfWorkListener
Invoked before the transaction bound to this Unit of Work is committed, but after all other commit activities (publication of events and saving of aggregates) are performed. This gives resource manager the opportunity to take actions that must be part of the same transaction.

Note that this method is only invoked if the Unit of Work is bound to a transaction.

Specified by:
onPrepareTransactionCommit in interface UnitOfWorkListener
Parameters:
unitOfWork - The Unit of Work of which the underlying transaction is being committed.
transaction - The object representing the (status of) the transaction, as returned by TransactionManager.startTransaction().
See Also:
TransactionManager

onCleanup

public void onCleanup(UnitOfWork unitOfWork)
Notifies listeners that the UnitOfWork is being cleaned up. This gives listeners the opportunity to clean up resources that might have been used during commit or rollback, such as remaining locks, open files, etc.

This method is always called after all listeners have been notified of a commit or rollback.

Listeners are called in the reversed order of precedence.

Specified by:
onCleanup in interface UnitOfWorkListener
Parameters:
unitOfWork - The Unit of Work being cleaned up

add

public void add(UnitOfWorkListener listener)
Adds a listener to the collection. Note that the order in which you register the listeners determines the order in which they will be handled during the various stages of a unit of work.

Parameters:
listener - the listener to be added


Copyright © 2010-2016. All Rights Reserved.