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
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * 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;
 
Implementation of the event sourcing repository that uses a cache to improve loading performance. The cache removes the need to read all events from disk, at the cost of memory usage. Since caching is not compatible with the optimistic locking strategy, only pessimistic locking is available for this type of repository.

Note that an entry of a cached aggregate is immediately invalidated when an error occurs while saving that aggregate. This is done to prevent the cache from returning aggregates that may not have fully persisted to disk.

Parameters:
<T> The type of aggregate this repository stores
Author(s):
Allard Buijze
Since:
0.3
 
 
     private static final NoCache DEFAULT_CACHE = new NoCache();
 
     private Cache cache = ;

    
Initializes a repository with a pessimistic locking strategy. Optimistic locking is not compatible with caching.

 
     @Deprecated
     protected CachingEventSourcingRepository() {
         super(.);
     }

    
Initializes a repository with a the given aggregateFactory and a pessimistic locking strategy. Optimistic locking is not compatible with caching.

Parameters:
aggregateFactory The factory for new aggregate instances
See also:
org.axonframework.repository.LockingRepository.org.axonframework.repository.LockingRepository.()
 
     public CachingEventSourcingRepository(AggregateFactory<T> aggregateFactory) {
         super(aggregateFactory.);
     }

    
Saves the aggregate and stores it in the cache (if configured) for fast retrieval. If an exception occurs while saving the aggregate, the related cache entry is invalidated immediately.

Note that an entry of a cached aggregate is immediately invalidated when an error occurs while saving that aggregate. This is done to prevent the cache from returning aggregates that may not have fully persisted.

Parameters:
aggregate the aggregate to save
 
     @Override
     public void doSaveWithLock(final T aggregate) {
         .put(aggregate.getIdentifier(), aggregate);
         try {
             super.doSaveWithLock(aggregate);
         } catch (RuntimeException ex) {
             // when an exception occurs, the lock is release and cached state is compromised.
             .remove(aggregate.getIdentifier());
             throw ex;
         }
     }
 
     @Override
     protected void doDeleteWithLock(T aggregate) {
         .remove(aggregate.getIdentifier());
         super.doDeleteWithLock(aggregate);
     }

    
Perform the actual loading of an aggregate. The necessary locks have been obtained. If the aggregate is available in the cache, it is returned from there. Otherwise the underlying persistence logic is called to retrieve the aggregate.

Parameters:
aggregateIdentifier the identifier of the aggregate to load
expectedVersion The expected version of the aggregate
Returns:
the fully initialized aggregate
    @SuppressWarnings({"unchecked"})
    @Override
    public T doLoad(AggregateIdentifier aggregateIdentifierLong expectedVersion) {
        T aggregate = (T) .get(aggregateIdentifier);
        if (aggregate == null) {
            aggregate = super.doLoad(aggregateIdentifierexpectedVersion);
        } else if (aggregate.isDeleted()) {
            throw new AggregateDeletedException(aggregateIdentifier);
        }
        CurrentUnitOfWork.get().registerListener(new CacheClearingUnitOfWorkListener(aggregateIdentifier));
        return aggregate;
    }

    
Set the cache to use for this repository. If a cache is not set, caching is disabled for this implementation.

Parameters:
cache the cache to use
    public void setCache(Cache cache) {
        this. = cache;
    }
        private AggregateIdentifier identifier;
        public CacheClearingUnitOfWorkListener(AggregateIdentifier identifier) {
            this. = identifier;
        }
        @Override
        public void onRollback(Throwable failureCause) {
            .remove();
        }
    }
New to GrepCode? Check out our FAQ X