Restore Aggregate from Snapshot

175 views
Skip to first unread message

Vladimir Kovalchuk

unread,
Jan 7, 2018, 5:41:02 AM1/7/18
to Axon Framework Users
Hi All

I've configured Snapshots by providing this configuration:

@Bean
public SpringAggregateSnapshotter snapshotter(ParameterResolverFactory parameterResolverFactory,
EventStore eventStore, TransactionManager transactionManager) {
Executor executor = Executors.newSingleThreadExecutor();
return new SpringAggregateSnapshotter(eventStore, parameterResolverFactory, executor, transactionManager);
}

@Bean
public Repository<Poll> pollRepository(Snapshotter snapshotter) {

EventSourcingRepository<Poll> repository = new EventSourcingRepository<Poll>(
pollAggregateFactory(),
eventStore,
new EventCountSnapshotTriggerDefinition(
snapshotter,
3)
);
return repository;
}

Now I constantly get this error 

Command 'wepoll.command.AttachTopicsCommand' resulted in org.axonframework.eventsourcing.IncompatibleAggregateException(Aggregate identifier must be non-null after applying an event. Make sure the aggregate identifier is initialized at the latest when handling the creation event.)

Does it mean that I need to provide some special functional to restore aggregate state from snapshot? Why I get this error? 
I'm new in Axon Framework... 



Allard Buijze

unread,
Jan 8, 2018, 2:17:18 PM1/8/18
to axonfr...@googlegroups.com
Hi Vladimir,

which Serializer do you use?

My hunch is that the serializer doesn't deserialize the identifier field properly, leaving it null. In such case, Axon will complain, because that field must be set after applying the first event (which is the snapshot event in this case).

Cheers,

Allard

Op zo 7 jan. 2018 om 11:41 schreef Vladimir Kovalchuk <vladimir.k...@gmail.com>:
--
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.


--

Vladimir Kovalchuk

unread,
Jan 10, 2018, 1:15:10 AM1/10/18
to Axon Framework Users
Hi Allard.
@Bean
public Serializer axonJsonSerializer() {
return new JacksonSerializer();
}

I'm using JacksonSerializer

Allard Buijze

unread,
Jan 10, 2018, 5:20:14 AM1/10/18
to axonfr...@googlegroups.com
That actually explains it:

you probably have a getter to expose the identifier, but no setter to set it. Jackson requires getters and setters (or a constructor that allows values to be set). While that's all very nice for commands and events, snapshots are a slightly different beast. Snapshots are in fact just serialized versions of the aggregate itself.
Since Axon 3.1, you can specify two different serializers: one for events, and one for the rest. Our recommendation would be to use JacksonSerializer for the Events and an XStreamSerializer for the rest.
In Spring Boot, you can configure your JacksonSerializer like this, to have Axon only use it for events, leaving the (default) XStreamSerializer for the other objects:

@Bean
@Qualifier("eventSerializer")
public Serializer eventSerializer() {
 // create it and return it
}

Cheers,

Allard

Op wo 10 jan. 2018 om 07:15 schreef Vladimir Kovalchuk <vladimir.k...@gmail.com>:

Vladimir Kovalchuk

unread,
Mar 8, 2018, 4:02:48 PM3/8/18
to Axon Framework Users
With XStreamSerializer works pretty well so I'm leaving XSTream as is... It's ok I think
Reply all
Reply to author
Forward
0 new messages