Snapshot examples

I can’t find any example about snapshots.

I tried this:

AggregateSnapshotter aggregateSnapshotter = new AggregateSnapshotter();
AggregateFactory factory = myRepository.getAggregateFactory();
aggregateSnapshotter.setAggregateFactories(Arrays.asList(factory));

aggregateSnapshotter.setEventStore(eventStore);
EventCountSnapshotterTrigger trigger = new EventCountSnapshotterTrigger();
trigger.setSnapshotter(aggregateSnapshotter);
trigger.setTrigger(100);
repositoryLavagna.setSnapshotterTrigger(trigger);

It works in a test project I used to explore and develop this code but not in my main application.
I have no errors, simply the snapshot table is empty (of course I tried with more than 100 events…).

The eventstore is a JpaEventSore (Oracle in the application, HSQLDB in the test project).

Any idea?

Thanks
Andrea Mariottini

Do you use exactly the same config in your main application?

If it worked in your test project, I don’t see why it shouldn’t work in anonther one. The must be some difference in configuration…

Cheers,

Allard

The test project is very different from main application but Axon is configured the same way.
The only difference is in the test project I have one EntityManager, in the application the EntityManager is provided as discussed in the other thread about multitenancy.
But except for the snapshot my domain works and the events are correctly persisted in the eventstore, handlers are called etc…

First of all I would like to know if the code I wrote about snapshotting is correct.
Then probably I made some mistake somewhere but I can’t find it… What could make the code work without errors but doing nothing?

Thanks

Andrea

Hi Andrea,

the code looks allright. Are you sure that the entityManager provided when the snapshot is appended is the same one as the one injected when the “regular” events are appended? The only thing I can imagine happening right now is that the snapshot is stored using another entity manager, which is ignored later on when the transaction is committed.

Cheers,

Allard

The EntityManager is stored in a thread local variable so is the same, moreover in the provided code there isn’t any explicit reference to the EntityManager, only to eventstore.

I had no time these days to do more tests, I’ll let you know when I’ll find something new.

Thanks

Andrea

I attach the log of what happens.
I wrapped the AggregateSnapshotter ad put System.out.println("-----------------> CREATE SNAPSHOT") in the createSnapshot() method before a call to super. createSnapshot().
As I understand createSnapshot is called after the Unit Of Work is stopped.
What does it means?
Maybe the snapshot is created after the transaction is committed?

[2013-11-27 16:40:09,786 - DEBUG] (SimpleCommandBus.java:120) -> [Dispatching command [xxx.MyCommand]]

[2013-11-27 16:40:09,787 - DEBUG] (NestableUnitOfWork.java:126) -> [Starting Unit Of Work.]

[2013-11-27 16:40:09,787 - DEBUG] (NestableUnitOfWork.java:141) -> [Registering Unit Of Work as CurrentUnitOfWork]

[2013-11-27 16:40:09,804 - DEBUG] (UnitOfWorkListenerCollection.java:161) -> [Registering listener: org.axonframework.eventsourcing.EventSourcingRepository$ConflictResolvingListener]

[2013-11-27 16:40:09,805 - DEBUG] (UnitOfWorkListenerCollection.java:161) -> [Registering listener: org.axonframework.repository.LockingRepository$LockCleaningListener]

[2013-11-27 16:40:09,805 - DEBUG] (NestableUnitOfWork.java:46) -> [Committing Unit Of Work]

[2013-11-27 16:40:09,806 - DEBUG] (UnitOfWorkListenerCollection.java:108) -> [Notifying listeners of commit request]

[2013-11-27 16:40:09,806 - DEBUG] (UnitOfWorkListenerCollection.java:111) -> [Notifying listener [org.axonframework.eventsourcing.EventSourcingRepository$ConflictResolvingListener] of upcoming commit]

[2013-11-27 16:40:09,807 - DEBUG] (UnitOfWorkListenerCollection.java:111) -> [Notifying listener [org.axonframework.repository.LockingRepository$LockCleaningListener] of upcoming commit]

[2013-11-27 16:40:09,807 - DEBUG] (UnitOfWorkListenerCollection.java:115) -> [Listeners successfully notified]

[2013-11-27 16:40:09,807 - DEBUG] (DefaultUnitOfWork.java:266) -> [Persisting changes to aggregates]

[2013-11-27 16:40:09,808 - DEBUG] (DefaultUnitOfWork.java:269) -> [Persisting changes to [xxxxxx.MyAggregate], identifier: [a9b5effd-2cb3-41fc-9e32-798bb418433c]]

[2013-11-27 16:40:09,809 - DEBUG] (UnitOfWorkListenerCollection.java:161) -> [Registering listener: org.axonframework.eventsourcing.EventCountSnapshotterTrigger$SnapshotTriggeringListener]

[2013-11-27 16:40:09,811 - DEBUG] (DefaultUnitOfWork.java:275) -> [Aggregates successfully persisted]

[2013-11-27 16:40:09,811 - DEBUG] (NestableUnitOfWork.java:52) -> [This Unit Of Work is not nested. Finalizing commit…]

[2013-11-27 16:40:09,812 - DEBUG] (DefaultUnitOfWork.java:233) -> [Publishing events to the event bus]

[2013-11-27 16:40:09,812 - DEBUG] (DefaultUnitOfWork.java:249) -> [Publishing event [xxx.MyEvent] to event bus [org.axonframework.eventhandling.SimpleEventBus@1666eba]]

[2013-11-27 16:40:09,812 - DEBUG] (SimpleEventBus.java:98) -> [Dispatching Event [EventoTestoLavagnaModificato] to EventListener [Class]]

[2013-11-27 16:40:09,815 - DEBUG] (SimpleEventBus.java:98) -> [Dispatching Event [EventoTestoLavagnaModificato] to EventListener [Class]]

[2013-11-27 16:40:09,817 - DEBUG] (SimpleEventBus.java:98) -> [Dispatching Event [EventoTestoLavagnaModificato] to EventListener [Class]]

[2013-11-27 16:40:09,818 - DEBUG] (DefaultUnitOfWork.java:260) -> [All events successfully published.]

[2013-11-27 16:40:09,818 - DEBUG] (UnitOfWorkListenerCollection.java:120) -> [Notifying listeners of transaction commit request]

[2013-11-27 16:40:09,818 - DEBUG] (UnitOfWorkListenerCollection.java:123) -> [Notifying listener [org.axonframework.eventsourcing.EventSourcingRepository$ConflictResolvingListener] of upcoming transaction commit]

[2013-11-27 16:40:09,818 - DEBUG] (UnitOfWorkListenerCollection.java:123) -> [Notifying listener [org.axonframework.repository.LockingRepository$LockCleaningListener] of upcoming transaction commit]

[2013-11-27 16:40:09,819 - DEBUG] (UnitOfWorkListenerCollection.java:123) -> [Notifying listener [org.axonframework.eventsourcing.EventCountSnapshotterTrigger$SnapshotTriggeringListener] of upcoming transaction commit]

[2013-11-27 16:40:09,819 - DEBUG] (UnitOfWorkListenerCollection.java:127) -> [Listeners successfully notified]

[2013-11-27 16:40:09,824 - DEBUG] (UnitOfWorkListenerCollection.java:58) -> [Notifying listeners after commit]

[2013-11-27 16:40:09,824 - DEBUG] (UnitOfWorkListenerCollection.java:63) -> [Notifying listener [org.axonframework.eventsourcing.EventCountSnapshotterTrigger$SnapshotTriggeringListener] after commit]

[2013-11-27 16:40:09,824 - DEBUG] (UnitOfWorkListenerCollection.java:63) -> [Notifying listener [org.axonframework.repository.LockingRepository$LockCleaningListener] after commit]

[2013-11-27 16:40:09,825 - DEBUG] (UnitOfWorkListenerCollection.java:63) -> [Notifying listener [org.axonframework.eventsourcing.EventSourcingRepository$ConflictResolvingListener] after commit]

[2013-11-27 16:40:09,825 - DEBUG] (NestableUnitOfWork.java:171) -> [Stopping Unit Of Work]

[2013-11-27 16:40:09,825 - DEBUG] (UnitOfWorkListenerCollection.java:137) -> [Notifying listeners of cleanup]

[2013-11-27 16:40:09,825 - DEBUG] (UnitOfWorkListenerCollection.java:143) -> [Notifying listener [org.axonframework.eventsourcing.EventCountSnapshotterTrigger$SnapshotTriggeringListener] of cleanup]

-----------------> CREATE SNAPSHOT

[2013-11-27 16:40:09,846 - DEBUG] (UnitOfWorkListenerCollection.java:143) -> [Notifying listener [org.axonframework.repository.LockingRepository$LockCleaningListener] of cleanup]

[2013-11-27 16:40:09,847 - DEBUG] (UnitOfWorkListenerCollection.java:143) -> [Notifying listener [org.axonframework.eventsourcing.EventSourcingRepository$ConflictResolvingListener] of cleanup]

[2013-11-27 16:40:09,847 - DEBUG] (UnitOfWorkListenerCollection.java:150) -> [Listeners successfully notified]

[2013-11-27 16:40:09,847 - DEBUG] (NestableUnitOfWork.java:68) -> [Clearing resources of this Unit Of Work.]

Hi Andrea,

did you configure a transaction manager on your snapshotter? (not the trigger, but the snapshotter itself).

Cheers,

Allard

I think no, I’m not at office these days so I can’t verify, is there a setter/constructor for this?

Andrea

Hi Andrea,

I wanted to reply with “yes, there is a setter for it”, but I double checked. There is only one for the SpringAggregateSnapshotter. If I recall, you don’t use Spring, do you?

I have filed an issue (AXON-197) and fixed is as well. 2.0.7, which contains the fix, will be released very soon.

Cheers,

Allard

Yes, I don’t use Spring…
Thanks, I’ll wait for version 2.0.7

Andrea

Thanks Allard!
I just tried versione 2.0.7 and now the snapshot is persisted.