Implementing Snapshot in AXON 3.0 : Aggregate Type is unknown in this snapShotter

I'm still new to the axon frame work.

I'm trying to implement snapshotting using mongodb in my application and I keep on getting an error saying

"AbstractSnapshotter : An attempt to create and store a snapshot resulted in an exception. Exception summary: Aggregate Type is unknown in this snapshotter: com.myworklife.contacts.domain.contact.Contact"

This is a part of my java config file.

@Bean
public AggregateSnapshotter snapShotter(EventStore eventStore, AggregateFactory<Contact> contactAggregateFactory) {
    return new AggregateSnapshotter(eventStore);
}


@Bean
public SnapshotTriggerDefinition snapshotTriggerDefinition(Snapshotter snapShotter) throws Exception {
    return new EventCountSnapshotTriggerDefinition(snapShotter, 1);
}

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

@Bean
public Repository<Contact> contactAggregateRepository(EventStore eventStore, SnapshotTriggerDefinition snapshotTriggerDefinition) {
    return new ContactRepository(eventStore, snapshotTriggerDefinition);
}

And my repository.

    @Repository("ContactRepository")
public class ContactRepository extends EventSourcingRepository<Contact> {

    @Autowired
    public ContactRepository(EventStore eventStore, SnapshotTriggerDefinition snapshotTriggerDefinition) {
        super(Contact.class, eventStore, snapshotTriggerDefinition);
    }

    public Contact findContact(ContactId contactId) {
        return load(contactId.toString()).getWrappedAggregate().getAggregateRoot();
    }
}

My aggregate.

@Aggregate(repository="contactAggregateRepository")
public class Contact {

    @AggregateIdentifier
    private ContactId id;
    private String name;
    private String mobileNumber;

    public Contact() {
        // do nothing, Axon requires default constructor
    }

    @CommandHandler
    public Contact(CreateContactCommand createContactCommand) {
        apply(new ContactHasBeenCreatedEvent(createContactCommand.getContactId(), createContactCommand.getName(),
                createContactCommand.getMobileNumber()));
    }
}

Is there something I'm doing wrong?

since I'm getting an error saying "An attempt to create and store a snapshot resulted in an exception. Exception summary: Aggregate Type is unknown in this snapshotter: com.myworklife.contacts.domain.contact.Contact"

Any help will be highly appreciated.

Thanks, Pat

You need to add the contactAggregateFactory to the constructor of the AggregateSnapshotter in the snapShotter bean:

@Bean
public AggregateSnapshotter snapShotter(EventStore eventStore,    AggregateFactory<Contact> contactAggregateFactory) {
    return new AggregateSnapshotter(eventStore, contactAggregateFactory);
}

Implementing Snapshot in AXON 3.0 : Aggregate , Exception summary: Aggregate Type is unknown in this snapshotter: com.​myworklife.contacts.domain.contact.Contact". This is my Java config  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.

1.depencity jar

 <dependency>
    <groupId>org.axonframework</groupId>
    <artifactId>axon-spring-boot-autoconfigure</artifactId>
    <version>${axon.version}</version>
</dependency>

2.At first,you should config your application.ymp or bootstrap.yml,like this:spring: data: mongodb: host: localhost port: 27017 database: axonframework events: domainevents snapshots: snapshotevents

3.config your mongoDB:

    @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(mongoHost)));
    return mongoFactory.createMongo();
}

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

4.config repository for your aggregate,for example,i config a Element Aggreaget's repository

@Configuration
public class ElementConfiguration {

    @Autowired
    private EventStore eventStore;
    @Bean
    @Scope("prototype")
    public Element elementAggregate() {
        return new Element();
    }

    @Bean
    public AggregateFactory<Element> elementAggregateAggregateFactory() {

        SpringPrototypeAggregateFactory<Element> aggregateFactory = new SpringPrototypeAggregateFactory<>();
        aggregateFactory.setPrototypeBeanName("elementAggregate");
        return aggregateFactory;
    }
    @Bean
    public SpringAggregateSnapshotterFactoryBean springAggregateSnapshotterFactoryBean(){

        SpringAggregateSnapshotterFactoryBean factory = new SpringAggregateSnapshotterFactoryBean();
        return factory;
    }
     @Bean
    public Repository<Element> elementAggregateRepository(Snapshotter snapshotter) {

        EventCountSnapshotTriggerDefinition eventCountSnapshotTriggerDefinition = new EventCountSnapshotTriggerDefinition(snapshotter, 3);


        EventSourcingRepository<Element> repository = new EventSourcingRepository<Element>(
            elementAggregateAggregateFactory(),
            eventStore,
            eventCountSnapshotTriggerDefinition
        );
        return repository;
    }

5.enjoy

Repository and Event Store, Implementing Snapshot in AXON 3.0 : Aggregate Type is unknown in this snapShotter - spring. Snapshot creation can be triggered by a number of factors, for example the number of events created since the last snapshot, the time to initialize an aggregate exceeds a certain threshold, time-based, etc. Currently, Axon provides a mechanism that allows you to trigger snapshots based on an event count threshold.

If you're using Axon 3.3 and SpringBoot 2, this is how we did it in this project:

@Configuration
public class SnapshotConfiguration {

    @Bean
    public SpringAggregateSnapshotter snapshotter(ParameterResolverFactory parameterResolverFactory,
                                                  EventStore eventStore,
                                                  TransactionManager transactionManager) {
        // https://docs.axoniq.io/reference-guide/v/3.3/part-iii-infrastructure-components/repository-and-event-store#snapshotting
        // (...) By default, snapshots are created in the thread that calls the scheduleSnapshot() method, which is generally not recommended for production (...)
        Executor executor = Executors.newSingleThreadExecutor();
        return new SpringAggregateSnapshotter(eventStore, parameterResolverFactory, executor, transactionManager);
    }

    @Bean
    public EventCountSnapshotTriggerDefinition snapshotTrigger(SpringAggregateSnapshotter snapshotter) {
        int snapshotThreshold = 42;
        return new EventCountSnapshotTriggerDefinition(snapshotter, snapshotThreshold);
    }
}

And if you need the EventStore configuration:

@Configuration
public class AxonMongoEventStoreConfiguration {

    @Bean
    public MongoClient axonMongoClient(MongoClientURI axonMongoClientUri) {
        return new MongoClient(axonMongoClientUri);
    }

    @Bean
    public MongoClientURI axonMongoClientUri() {
        return new MongoClientURI("mongodb://host:port/database");
    }

    @Bean
    @Primary
    public EventStorageEngine eventStore(MongoClient axonMongoClient, MongoClientURI axonMongoClientUri) {
        DefaultMongoTemplate mongoTemplate = new DefaultMongoTemplate(axonMongoClient, axonMongoClientUri.getDatabase());
        return new MongoEventStorageEngine(mongoTemplate);
    }
}

Optimizing Aggregate Loading, Implementing Snapshot in AXON 3.0 : Aggregate Type is unknown in this snapShotter. I'm still new to the axon frame work. I'm trying to implement snapshotting  Snapshot creation can be triggered by a number of factors, for example the number of events created since the last snapshot, the time to initialize an aggregate exceeds a certain threshold, time-based, etc. Currently, Axon provides a mechanism that allows you to trigger snapshots based on an event count threshold.

[#754 Part 10] Use builder pattern for building blocks instead of , Download Axon. 3.0. About this guide. Part I - Getting started. Introduction In the Axon Framework, all repositories must implement the Repository interface. be done in full automation because the structure of the new event is unknown to to use a single Snapshotter to create snapshots for a variety of aggregate types​. In Axon 2, you cannot upcast any information that's not in the payload of the event itself. Instead, you will have to change the aggregate type directly in the database. A little backgroun: this column is used by Axon 2 to retrieve the events for a specific Aggregate (together with the aggregate identifier).

org.axonframework.eventsourcing.AggregateSnapshotter Maven , A Snapshotter is responsible for the actual creation of a snapshot. This is a special type of snapshot, since it contains the actual This event store must implement the SnapshotEventStore interface. Tip: if you use a snapshotter, make sure to choose snapshot trigger and batch size such that a single batch will generally retrieve all events required to rebuild an aggregate's state. template - MongoTemplate instance to obtain the database and the collections.

AxonFramework, Implementing Snapshot in AXON 3.0 : Aggregate Type is unknown in this @​Bean public AggregateSnapshotter snapShotter(EventStore  Implementation of the Aggregate interface that allows for an aggregate root to be a POJO with annotations on its Command and Event Handler methods. AnnotatedAggregate(T, AggregateModel<T>, EventBus) - Constructor for class org.axonframework.commandhandling.model.inspection.

Comments
  • can you suggest any tutorial for Implementing Snapshot in AXON 3.0