Event Store Snapshotting in Axon 3

Hello,

Could you please show and example how to set up Event Source Snapshotting in Axon 3.

Thank you

Given an aggregate called MyAggregate (assuming you use Spring), define a repository bean called “myAggregateRepository” (you can also use another name, but then specify that name on your @Aggregate annotation) like this:

@Bean
public Repository myAggregateRepository(EventStore eventStore, Snapshotter snapshotter) {
return new EventSourcingRepository<>(MyAggregate.class, eventStore, new EventCountAggregateSnapshotter(snapshotter, 100));
}

@Bean
public SpringAggregateSnapshotterFactoryBean snapshotter() {
return new SpringAggregateSnapshotterFactoryBean();
}

I didn’t copy-paste this from an IDE, so it’s an ‘offline guess’ of how it should be.
Cheers,

Allard

Hello Allard,

Where is snapshotting supposed to be configured? Is there any full example that implements snapshotting using axon 3? Most of the exampled I’ve seen use older versions of axon, like 5 years ago.

Best Regards

Hello Harvey,

I got the following configuration (using JPA), both with Caching and with or without ParameterResolverFactory:

@Bean
public SpringAggregateSnapshotter snapshotter(ParameterResolverFactory parameterResolverFactory, EventStore eventStore, TransactionManager transactionManager) {
    Executor executor = Executors.newSingleThreadExecutor(); //Or any other executor of course
    return new SpringAggregateSnapshotter(eventStore, parameterResolverFactory, executor, transactionManager);
}

@Bean
public SnapshotTriggerDefinition snapshotTriggerDefinition(Snapshotter snapshotter) throws Exception {
    return new EventCountSnapshotTriggerDefinition(snapshotter, 50);
}
@Bean
public Repository<OrderBook> orderBookRepository(EventStore eventStore, SnapshotTriggerDefinition snapshotTriggerDefinition, Cache cache) {
    return new CachingEventSourcingRepository<>(new GenericAggregateFactory<>(OrderBook.class), eventStore, cache, snapshotTriggerDefinition);
}

@Bean
public Repository<Individual> individualRepository(EventStore eventStore, SnapshotTriggerDefinition snapshotTriggerDefinition, Cache cache, ParameterResolverFactory parameterResolverFactory) {
    return new CachingEventSourcingRepository<>(new GenericAggregateFactory<>(Individual.class), eventStore, new PessimisticLockFactory(), cache, parameterResolverFactory, snapshotTriggerDefinition);
}

Thanks for replying Frank. I added the configurations you provided. I had the following error:

Hello Harvey,

You can add a Cache bean like this:

`

@Bean
public Cache cache(){
return new WeakReferenceCache();
}

`

Cheers!