Implementing Snapshot in AXON 3.0

187 views
Skip to first unread message

Brahma Choudhary

unread,
Jun 26, 2017, 5:05:43 AM6/26/17
to Axon Framework Users
 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<Sale> saleAggregateFactory() {
        SpringPrototypeAggregateFactory<Sale> aggregateFactory = new SpringPrototypeAggregateFactory<>();
        aggregateFactory.setPrototypeBeanName("sale");
      

        return aggregateFactory;
    }

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

    @Bean
    public Repository<Sale> saleAggregateRepository(EventStore eventStore, SnapshotTriggerDefinition snapshotTriggerDefinition, ParameterResolverFactory parameterResolverFactory) {
       
        //new GenericAggregateFactory<>(Sale.class)
        // EventSourcingRepository<ToDoItem> repository = new EventSourcingRepository(Sale, eventStore);
         EventSourcingRepository<Sale> repository =  new EventSourcingRepository<>(new GenericAggregateFactory<>(Sale.class), eventStore, parameterResolverFactory, snapshotTriggerDefinition);
         
        return repository;
       
        //return new CachingEventSourcingRepository<>(new GenericAggregateFactory<>(Sale.class), eventStore, cache, snapshotTriggerDefinition);
    }
   
 
  
  
}

Frank Groot

unread,
Jun 26, 2017, 5:41:10 AM6/26/17
to Axon Framework Users
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..



--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Frank Groot - Lead Architect - frank...@trifork.nl - http://www.trifork.nl/ 
Mobile: +31 6 5073 1421 Office: +31 20 486 20 36 Fax: +31 20 475 08 28
Address: 
Rijnsburgstraat 9-11 1059 AT - Amsterdam - The Netherlands

Brahma Choudhary

unread,
Jun 26, 2017, 6:20:30 AM6/26/17
to Axon Framework Users
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

Frank Groot

unread,
Jun 26, 2017, 6:59:28 AM6/26/17
to Axon Framework Users
AbstractSnapshotter class line 145 in Axon 3.0.5 :)

Brahma Choudhary

unread,
Jun 26, 2017, 12:09:49 PM6/26/17
to Axon Framework Users
Hi Frank,

Thanks for guiding me.

AbstractSnapshotter class line 145 is giving NoSuchElementException.


Thanks,
Brahma

Brahma Choudhary

unread,
Jun 26, 2017, 2:18:39 PM6/26/17
to Axon Framework Users
Hi Frank,

I think the following line of AggregateSnapshotter is giving NoSuchElementException.

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


Thanks,
Brahma

Frank Groot

unread,
Jun 27, 2017, 3:15:28 AM6/27/17
to Axon Framework Users
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");

Brahma Choudhary

unread,
Jul 8, 2017, 5:12:16 AM7/8/17
to Axon Framework Users
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
-----------------------------

 if (snapshotEvent != null && snapshotEvent.getSequenceNumber() > firstEventSequenceNumber) {
                eventStore.storeSnapshot(snapshotEvent);
            }

Thnaks,
Brahma
Reply all
Reply to author
Forward
0 new messages