Implementing Snapshot in AXON 3.0

While implementing Snapshotting snapshot entry is not created in mongodb store. following is my configuration file.

I am getting WARN as AbstractSnapshotter : An attempt to create and store a snapshot resulted in an exception. Exception summary: null

@Configuration
//@ConditionalOnClass(CommandBus)
@EnableMongoRepositories(basePackages = {“com.example”})
public class MongoConfiguration {

@Value("${spring.data.mongodb.uri}")
private String mongoUrl;

@Value("${spring.data.mongodb.database}")
private String mongoDbName;

@Value("${spring.data.mongodb.events.collection.name}")
private String eventsCollectionName;

@Value("${spring.data.mongodb.snapshot.collection.name}")
private String snapshotCollectionName;

@Bean
public Serializer axonJsonSerializer() {
return new JacksonSerializer();
}

@Bean
//s @ConditionalOnMissingBean
CommandBus commandBus() {
return new SimpleCommandBus();

}

@Bean
public EventStorageEngine eventStorageEngine(){
return new MongoEventStorageEngine(
axonJsonSerializer(),null, axonMongoTemplate(), new DocumentPerEventStorageStrategy());
}

@Bean(name = “axonMongoTemplate”)
public MongoTemplate axonMongoTemplate() {
MongoTemplate template = new DefaultMongoTemplate(mongoClient(), mongoDbName, eventsCollectionName, snapshotCollectionName);
return template;
}

@Bean
public MongoClient mongoClient(){
//MongoFactory mongoFactory = new MongoFactory();
// mongoFactory.setMongoAddresses(Arrays.asList(new ServerAddress(mongoUrl)));
MongoClientURI mongoClientURI = new MongoClientURI(mongoUrl);
MongoClient mongoClient = new MongoClient(mongoClientURI);
return mongoClient;
}

@Bean
public EventStore eventStore() {
return new EmbeddedEventStore(eventStorageEngine());
}

@Bean
public CommandGateway commandGateway(CommandBus commandBus) {
return new DefaultCommandGateway(commandBus);
}

@Bean
SimpleEventBus eventBus() {
return new SimpleEventBus();
}

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

@Bean
public SnapshotTriggerDefinition snapshotTriggerDefinition(Snapshotter snapshotter) throws Exception {
//snapshotter.setTransactionManager(transactionManager);
return new EventCountSnapshotTriggerDefinition(snapshotter, 1);
}

@Bean
public AggregateFactory saleAggregateFactory() {
SpringPrototypeAggregateFactory aggregateFactory = new SpringPrototypeAggregateFactory<>();
aggregateFactory.setPrototypeBeanName(“sale”);

return aggregateFactory;
}

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

@Bean
public Repository saleAggregateRepository(EventStore eventStore, SnapshotTriggerDefinition snapshotTriggerDefinition, ParameterResolverFactory parameterResolverFactory) {

//new GenericAggregateFactory<>(Sale.class)
// EventSourcingRepository repository = new EventSourcingRepository(Sale, eventStore);
EventSourcingRepository repository = new EventSourcingRepository<>(new GenericAggregateFactory<>(Sale.class), eventStore, parameterResolverFactory, snapshotTriggerDefinition);

return repository;

//return new CachingEventSourcingRepository<>(new GenericAggregateFactory<>(Sale.class), eventStore, cache, snapshotTriggerDefinition);
}

}

Hi Brahma,

Maybe set a breakpoint in AbstractSnapshotter (Silentask->run()) to see what the exception is. It will probably tell you more about the error.

Could be a null transactionmanager, in which case you can set it to NoTransactionManager.INSTANCE in your snapshotter bean as you are using MongoDB.

Also, you should probably get rid of the EventBus bean, as you already declared a EventStore. Don’t create them both at the same time, because the EventStore extends the EventBus already…

Thanks Frank, for quick reply,

while normal debugging I find that trancationManager is set with NoTransactionManager.INSTANCE.

But I didn’t get “Silentask->run()” , can you please elaborate this.

Thanks,
Brahma

AbstractSnapshotter class line 145 in Axon 3.0.5 :slight_smile:

Hi Frank,

Thanks for guiding me.

AbstractSnapshotter class line 145 is giving NoSuchElementException.

Thanks,
Brahma

Hi Frank,

I think the following line of AggregateSnapshotter is giving NoSuchElementException.

aggregateModels.computeIfAbsent(aggregateType, k -> ModelInspector.inspectAggregate(k, parameterResolverFactory));

Thanks,
Brahma

Hi Brahma,

Maybe you can include a full stack trace at that point.

My guess is you have some older events containing fields that are no longer present in your Aggregate. The serializer will then fail on unknown elements.
Maybe you can start with a clean database to see if it works, or if you need to keep the events, tell the serializer to ignore the unknown field by specifying something like: serializer.getXStream().ignoreUnknownElements(“oldfields”);

Hi Frank,

I am not able to store snapshot in the table but event are stored in the corresponding table.
Problem is every event that stored having same sequence no. (zero for each event) its not gets incremented.
So the following if condition gets failed and snapshot storing is not executed.

AbstractSnapshotter