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;
 
 
 import java.util.List;
Snapshotter trigger mechanism that counts the number of events to decide when to create a snapshot. This implementation acts as a proxy towards the actual event store, and keeps track of the number of "unsnapshotted" events for each aggregate. This means repositories should be configured to use an instance of this class instead of the actual event store.

Author(s):
Allard Buijze
Since:
0.6
 
 public class EventCountSnapshotterTrigger implements SnapshotterTrigger {
 
     private static final int DEFAULT_TRIGGER_VALUE = 50;
 
     private Snapshotter snapshotter;
     private volatile boolean clearCountersAfterAppend = true;
     private int trigger = ;
 
     @Override
     public DomainEventStream decorateForRead(String aggregateTypeAggregateIdentifier aggregateIdentifier,
                                              DomainEventStream eventStream) {
         AtomicInteger counter = new AtomicInteger(0);
         .put(aggregateIdentifiercounter);
         return new CountingEventStream(eventStreamcounter);
     }
 
     @Override
     public DomainEventStream decorateForAppend(String aggregateTypeEventSourcedAggregateRoot aggregate,
                                                DomainEventStream eventStream) {
         AggregateIdentifier aggregateIdentifier = aggregate.getIdentifier();
         .putIfAbsent(aggregateIdentifiernew AtomicInteger(0));
         AtomicInteger counter = .get(aggregateIdentifier);
         return new TriggeringEventStream(aggregateTypeaggregateIdentifiereventStreamcounter);
     }
 
     private void triggerSnapshotIfRequired(String typeAggregateIdentifier aggregateIdentifier,
                                            final AtomicInteger eventCount) {
         if (eventCount.get() > ) {
             .scheduleSnapshot(typeaggregateIdentifier);
             eventCount.set(1);
         }
     }

    
Sets the snapshotter to notify when a snapshot needs to be taken.

Parameters:
snapshotter the snapshotter to notify
 
     public void setSnapshotter(Snapshotter snapshotter) {
         this. = snapshotter;
     }

    
Sets the number of events that will trigger the creation of a snapshot events. Defaults to 50.

This means that a snapshot is created as soon as loading an aggregate would require reading in more than 50 events.

Parameters:
trigger The default trigger value.
 
     public void setTrigger(int trigger) {
         this. = trigger;
     }

    
Indicates whether to maintain counters for aggregates after appending events to the event store for these aggregates. Defaults to true.

By setting this value to false, event counters are kept in memory. This is particularly useful when repositories use caches, preventing events from being loaded. Consider registering the Caches use using setAggregateCache(net.sf.jsr107cache.Cache) or setAggregateCaches(java.util.List)

Parameters:
clearCountersAfterAppend indicator whether to clear counters after appending events
    public void setClearCountersAfterAppend(boolean clearCountersAfterAppend) {
        this. = clearCountersAfterAppend;
    }

    
Sets the Cache instance used be Caching repositories. By registering them to the snapshotter trigger, it can optimize memory usage by clearing counters held for aggregates that are contained in caches. When an aggregate is evicted or deleted from the cache, its event counter is removed from the trigger.

Use the setAggregateCaches(java.util.List) method if you have configured different caches for different repositories.

Using this method will automatically set setClearCountersAfterAppend(boolean) to false.

Parameters:
cache The cache used by caching repositories
See also:
setAggregateCaches(java.util.List)
    public void setAggregateCache(Cache cache) {
        this. = false;
        cache.addListener(new CacheListener());
    }

    
Sets the Cache instances used be Caching repositories. By registering them to the snapshotter trigger, it can optimize memory usage by clearing counters held for aggregates that are contained in caches. When an aggregate is evicted or deleted from the cache, its event counter is removed from the trigger.

Parameters:
caches The caches used by caching repositories
    public void setAggregateCaches(List<Cachecaches) {
        for (Cache cache : caches) {
            setAggregateCache(cache);
        }
    }
    private class CountingEventStream implements DomainEventStream {
        private final DomainEventStream delegate;
        private final AtomicInteger counter;
        public CountingEventStream(DomainEventStream delegateAtomicInteger counter) {
            this. = delegate;
            this. = counter;
        }
        @Override
        public boolean hasNext() {
            return .hasNext();
        }
        @Override
        public DomainEvent next() {
            DomainEvent next = .next();
            .incrementAndGet();
            return next;
        }
        @Override
        public DomainEvent peek() {
            return .peek();
        }

        
Returns the counter containing the number of bytes read.

Returns:
the counter containing the number of bytes read
        protected AtomicInteger getCounter() {
            return ;
        }
    }
    private final class TriggeringEventStream extends CountingEventStream {
        private final String aggregateType;
        private AggregateIdentifier aggregateIdentifier;
        private TriggeringEventStream(String aggregateTypeAggregateIdentifier aggregateIdentifier,
                                      DomainEventStream delegateAtomicInteger counter) {
            super(delegatecounter);
            this. = aggregateType;
            this. = aggregateIdentifier;
        }
        @Override
        public boolean hasNext() {
            boolean hasNext = super.hasNext();
            if (!hasNext) {
                CurrentUnitOfWork.get().registerListener(new SnapshotTriggeringListener(,
                                                                                        ,
                                                                                        getCounter()));
                if () {
                    .remove(getCounter());
                }
            }
            return hasNext;
        }
    }
    private final class CacheListener implements net.sf.jsr107cache.CacheListener {
        @Override
        public void onLoad(Object key) {
        }
        @Override
        public void onPut(Object key) {
        }
        @SuppressWarnings({"SuspiciousMethodCalls"})
        @Override
        public void onEvict(Object key) {
            .remove(key);
        }
        @SuppressWarnings({"SuspiciousMethodCalls"})
        @Override
        public void onRemove(Object key) {
            .remove(key);
        }
        @Override
        public void onClear() {
            .clear();
        }
    }
    private class SnapshotTriggeringListener extends UnitOfWorkListenerAdapter {
        private final String aggregateType;
        private final AggregateIdentifier aggregateIdentifier;
        private final AtomicInteger counter;
        public SnapshotTriggeringListener(String aggregateType,
                                          AggregateIdentifier aggregateIdentifierAtomicInteger counter) {
            this. = aggregateType;
            this. = aggregateIdentifier;
            this. = counter;
        }
        @Override
        public void onCleanup() {
        }
    }
New to GrepCode? Check out our FAQ X