Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (c) 2010-2011. Axon Framework
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 package org.axonframework.eventsourcing;
Abstract convenience class to be extended by all aggregate roots. The AbstractEventSourcedAggregateRoot tracks all uncommitted events. It also provides convenience methods to initialize the state of the aggregate root based on a org.axonframework.domain.DomainEventStream, which can be used for event sourcing.

Allard Buijze
 public abstract class AbstractEventSourcedAggregateRoot extends AbstractAggregateRoot
         implements EventSourcedAggregateRoot {
     private static final long serialVersionUID = 5868786029296883724L;

Initializes the aggregate root using a random aggregate identifier.
     protected AbstractEventSourcedAggregateRoot() {

Initializes the aggregate root using the provided aggregate identifier.

identifier the identifier of this aggregate
     protected AbstractEventSourcedAggregateRoot(AggregateIdentifier identifier) {


This implementation is aware of two special types of DomainEvents: The AggregateDeletedEvent, which indicates that the aggregate is deleted and the AggregateSnapshot, which is a snapshot event, containing the actual aggregate inside.

When an AggregateDeletedEvent is encountered, a AggregateDeletedException is thrown, unless there are events following the AggregateDeletedEvent. This could be the case when an event is added to the stream as a correction to an earlier event.

AggregateSnapshot events are used to initialize the aggregate with the correct version (getVersion()).

java.lang.IllegalStateException if this aggregate was already initialized.
AggregateDeletedException if the event stream contains an event of type org.axonframework.domain.AggregateDeletedEvent (or subtype).
     public void initializeState(DomainEventStream domainEventStream) {
         Assert.state(getUncommittedEventCount() == 0, "Aggregate is already initialized");
         long lastSequenceNumber = -1;
         while (domainEventStream.hasNext()) {
             DomainEvent event =;
             if (event instanceof AggregateDeletedEvent) {
                 throw new AggregateDeletedException(event.getAggregateIdentifier());
             lastSequenceNumber = event.getSequenceNumber();
             if (!(event instanceof AggregateSnapshot)) {

Apply the provided event. Applying events means they are added to the uncommitted event queue and forwarded to the handle(org.axonframework.domain.DomainEvent) event handler method} for processing.

The event is applied on all entities part of this aggregate.

event The event to apply
    protected void apply(DomainEvent event) {
        if (event instanceof AggregateDeletedEvent) {
    private void handleRecursively(DomainEvent event) {
        Collection<AbstractEventSourcedEntitychildEntities = getChildEntities();
        if (childEntities != null) {
            for (AbstractEventSourcedEntity entity : childEntities) {

Returns a list of event sourced entities directly referenced by the aggregate root.

The default implementation uses reflection to find references to AbstractEventSourcedEntity implementations.

It will look for entities:

This method may be overridden by subclasses. A null may be returned if this entity does not have any child entities.

a list of event sourced entities contained in this aggregate
        return ReflectionUtils.findFieldValuesOfType(thisAbstractEventSourcedEntity.class);

Apply state changes based on the given event.

Note: Implementations of this method should *not* perform validation.

event The event to handle
    protected abstract void handle(DomainEvent event);
    public Long getVersion() {
        return getLastCommittedEventSequenceNumber();
New to GrepCode? Check out our FAQ X