Snapshot and Initializing an Aggregate based on a Snapshot Event

Hello guys, how are you?

So, I’m trying to understand snapshotters better.

So far I’m just using the following annotation:


@Aggregate(snapshotTriggerDefinition = "eventCountSnapshot")
@Revision("1.0")
final class Account constructor() {

with this simple configuration:

@Bean
fun snapshotterFactory(): SpringAggregateSnapshotterFactoryBean {
    return SpringAggregateSnapshotterFactoryBean()
}

@Bean
fun eventCountSnapshot(snapshotter: Snapshotter) =
        EventCountSnapshotTriggerDefinition(snapshotter, 2)

So far so good. I have this row on my snapshot table.

6946167f-c375-4e45-ac48-d745c8b15128 31 Account 483355b1-68eb-453b-877f-103f536aa7e8 {“traceId”:“891d401a-8262-4102-98bf-825bdacb3771”,“correlationId”:“891d401a-8262-4102-98bf-825bdacb3771”} {“id”:“6946167f-c375-4e45-ac48-d745c8b15128”,“name”:“Account 1”,“balance”:359} 1.0 br.com.zup.axon.saga.aggregate.Account 2018-06-01T20:46:51.868Z

also, I have this handler on my Account Aggregate

@EventHandler
fun on(account: Account) {
    this.id = id
    this.name = name
    this.balance = balance
}

My question is, how I can customize my payload_type to a specific event class model? Let’s say AccountSnapshotEvent.

Is this more or less what the documentation in this part said I could do?

https://docs.axonframework.org/part-iii-infrastructure-components/repository-and-event-store#initializing-an-aggregate-based-on-a-snapshot-event

@EventHandler
protected void applySnapshot(MySnapshotEvent event) {
// the snapshot event should contain all relevant state
this.someState = event.someState;
this.otherState = event.otherState;
}

Thanks for any help

Hi Mike,

What I’m guessing your’e doing, is trying to add an ‘event sourcing handler’ to your Account Aggregate which handlers your snapshot-events.

Is that a correct assumption?

If so, the answer to your question is simple.

You only need to handle snapshot events, if you’re not using an AggregateSnapshotter.

As you’re using the SpringAggregateSnapshotterFactoryBean, you will end up with a SpringAggregetSnapshotter, which is an implementation of an AggregateSnapshotter.

You’ll thus end up with AggregateSnapshot instances, which you do not have to handle yourself.
An AggregateSnapshot is technically a serialized version of your Aggregate.

Hope this helps you Mike!

Cheers,
Steven

Hello, Steven. How are you doing?
Thanks for the answer btw.

"You’ll thus end up with AggregateSnapshot instances, which you do not have to handle yourself.
An AggregateSnapshot is technically a serialized version of your Aggregate."

I understand this and that’s how I’m using right now.

But let I try to explain this better. I saw this piece of code

@EventHandler
protected void applySnapshot(MySnapshotEvent event) {
// the snapshot event should contain all relevant state
this.someState = event.someState;
this.otherState = event.otherState;
}

at the documentation and I really like the idea of having my own snapshot event bean: MySnapshotEvent, in this case.
I think makes things more clear special with this method name “applySnapshot” even tho I have to write more code.

So I dug deep in the source code and I didn’t figure out how to take a snapshot with a custom event like that instead of Aggregate itself.

Instead of having this column at SNAPSHOT table:

payload_type: br.com.zup.axon.saga.aggregate.Account

I would like something like:

payload_type: br.com.zup.axon.saga.aggregate.AccountSnapshotEvent

So, my question is: it’s possible to do that? Is that what the documentation is saying?

I guess by your answer it’s possible but I have to write my own AggregateSnapshotter.

Thank you again!

Hi Mike,

Ah, sorry for that! I thought the internals were unclear, but I apparently made too quick an assumption.

To answer your questions though, I think you should be able to create a Snapshotter which sets a different payload type if you’d want to.

I’d probably start off with the AbstractSnapshotter myself, and introduce an implementation which sets the desired payload type i.o. using the Aggregate type.

That said though, it would require some additional engineering, as the framework currently doesn’t easily support you changing the payload type.

I hope this helps Mike, feel free to further this discussion if necessary!

Cheers,

Steven

I see. Again, thank you again.
I really appreciate.