Snapshots on Event

Hi all,

I’m curious about whether or not it’s possible to configure a snapshot trigger definition that creates snapshots when a specific event is published.

I’m just learning Axon and am making a small shopping cart application. My hope is to create snapshots whenever there’s a CartClearedEvent or a CartCheckoutEvent. I’ve looked around and it seems like the only possible option for snapshotting is after a set number of events. Is this correct? Or am I missing something?

If this isn’t a feature, maybe it could be one that could be looked into?

Any help or info is appreciated!

Thanks,
Chris

Hi Chris,

To get a bit more understanding on snapshotting please take a look at our documentation page about snapshots. The framework currently supports EventCountSnapshotTriggerDefinition only. If this is not sufficient, you can create your own trigger definition and register it within configuration components. However, if you think this should be part of the framework itself, you are welcomed to create an issue on our Github page or even a PR :).

Cheers,
Milan.

I have read the docs. However, when it comes to snapshots they only really explain theory and not actual practice or implementation.
If you know of a place with examples or more than the docs have in implementing custom triggers or actually using snapshot events, that would be helpful.

Thanks,
Chris.

Hi Chris,

As an example, we have an AxonTrader sample application. If you are using spring, you can refer to CompanyConfig class, and see how to create EventCountSnapshotTriggerDefinition as a bean. You can also wire the trigger definition manually to aggregateConfigurer:

aggregateConfigurer.configureSnapshotTrigger(c -> buildMySnapshotTrigger(c));

Unfortunately, we’re missing detailed instructions on how to build a custom trigger definition and snapshotter in our reference guide. However, you can take a look at EventCountSnapshotTriggerDefinition and get the idea how to implement your own. When it comes to wiring up your implementation, you can use one of two examples aforementioned.

Hope this helps!

Cheers,
Milan.

Hi Milan,

Thanks for your hint on looking at the EventCountTrigger implementation!

I managed to create an EventTypeSnapshotTriggerDefinition that appears to be working exactly as intended. Rather than accepting a count threshold and checking that, I accept Class…EventTypes, and then check those class names against the message payloadType name.

Quick question though: I noticed in the CompanyConfig class that there’s no AggregateFactory defined. How is this so? I attempted copying what’s in the CompanyConfig(substituting my aggregate names), however I was forced to define my own AggregateFactory bean for my ShoppingCart aggregate. Is the factory defined somewhere else in the project that I’m missing?

Thanks,
Chris

Hi Chris,

I forgot to mention another approach for defining a custom trigger definition. If your aggregate configuration is different only by custom trigger definition (and you are using framework version >= 3.1), you can state it in the Aggregate annotation

@Aggregate(snapshotTriggerDefinition = "myCustomTriggerDefinition")

Do note that this configuration requires bean with qualifier myCustomTriggerDefinition.

Regarding manual registration of AggregateFactory for your bean. Again, if you are using spring, you don’t have to provide a custom implementation of AggregateFactory. The framework should register SpringPrototypeAggregateFactory bean for you unless you have a bean with name yourAggregateNameAggregateFactory already defined. However, if you are experiencing some issues with this, please provide us with some more details and we’ll be happy to take a look at it.

Cheers,
Milan.

Hi Milan,

Thanks, I didn’t know about annotation property. Just creating the triggerDefinition and setting the property worked, although I had to define a SpringAggregateSnappshotterFactoryBean.

Because of the annotation property I no longer need to manually define the aggregateFactory. However, before, I did need to define it. If I didn’t, I would get a nullPointer when creating the AggregateRepository bean.

Thanks for your help,

Chris