Implementing axon snapshot with springboot 2.3.3 and axon 4.4.2

╄→尐↘猪︶ㄣ 提交于 2021-01-29 07:25:18

问题


can anyone suggest any tutorial/sample project for Implementing Snapshot in AXON 4.4.2 with springBoot 2.3.3.

i went through the documentation(https://docs.axoniq.io/reference-guide/axon-framework/tuning/event-snapshots#snapshotting) and did below: The AxonConfig.class

@Configuration
public class AxonConfig {

    @Bean
    public SnapshotTriggerDefinition app1SnapshotTrigger(Snapshotter snapshotter) {
        return new EventCountSnapshotTriggerDefinition(snapshotter, 10);
    }
}

The Aggregate

@Aggregate(snapshotTriggerDefinition = "app1SnapshotTrigger")
public class MyAggregate {

    @AggregateIdentifier
    private String id;
    private String name;
    @AggregateMember
    private List<Address> addresses = new ArrayList<>();

    private  MyAggregate  () {

    }

    @CommandHandler
    private  MyAggregate  (CreateNameCommand createNameCommand) {
        -----
    }

    @EventSourcingHandler
    private void on(NameCreatedEvent nameCreatedEvent) {
       ----
    } 

Am i missing something. Will it create a snapshot at the threshold value 10. Thanks.


回答1:


unfortunately we have no sample demo ready to show in this case.

From your code snippet looks that all is in place. Maybe there is some other configuration that is taking over your annotation.

To give a try, I applied your configuration to our https://github.com/AxonIQ/giftcard-demo/

First note that can guide is the following

  • if you declared a Repository as we did in https://github.com/AxonIQ/giftcard-demo/blob/master/src/main/java/io/axoniq/demo/giftcard/command/GcCommandConfiguration.java#L17 this configuration will take over Annotation placed into your aggregate. If you prefer annotation, you can remove this Bean definition.

Here the piece of code, instead, to have this configured as a Bean

@Bean
public Repository<GiftCard> giftCardRepository(EventStore eventStore, SnapshotTriggerDefinition giftCardSnapshotTrigger) {
    return EventSourcingRepository.builder(GiftCard.class)
            .snapshotTriggerDefinition(giftCardSnapshotTrigger)
            .eventStore(eventStore)
            .build();
}

@Bean
public SpringAggregateSnapshotterFactoryBean snapshotter() {
    var springAggregateSnapshotterFactoryBean = new SpringAggregateSnapshotterFactoryBean();
    //Setting async executors
    springAggregateSnapshotterFactoryBean.setExecutor(Executors.newSingleThreadExecutor());
    return springAggregateSnapshotterFactoryBean;
}

@Bean("giftCardSnapshotTrigger")
public SnapshotTriggerDefinition giftCardSnapshotTriggerDefinition(Snapshotter snapshotter) {
    return new EventCountSnapshotTriggerDefinition(snapshotter, 10);
}

You can check that your snapshot is working fine looking at client log : after 10 events on the same agggregateId, you should find this info log entry o.a.a.c.event.axon.AxonServerEventStore : Snapshot created

To check you can use the REST api to retrieve the events from an aggregate curl -X GET "http://localhost:8024/v1/events?aggregateId=A01" This will produce a stream containing events starting from the latest Snapshot: you will have nine events listed until the tenth event will be processed. After that, the endpoint will list events from the snapshot.

You can also check /actuator/health endpoint: it will shows the last snapshot token if the showDetails is enabled (enabled by default in EE, not enabled by default in SE).

Corrado.



来源:https://stackoverflow.com/questions/63767707/implementing-axon-snapshot-with-springboot-2-3-3-and-axon-4-4-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!