propagate events from within EventSourcingHandler leads to multiple events thrown on snapshot creation

Hi,

we are currently evaluating Axon 3.0.5 for our needs on a new project and I have a conceptual question regarding publishing events as reaction on events applied to the aggregate.

The scenario is rather simple: We have two aggregates, equipments and customers, and equipments can be assigned to one customer - the customer aggregate does not store which equipments are assigned.

Now I want to react on a EquipmentAssignedToCustomerCommand, which applies to the equipment updating its assigned customer (EquipmentAssignedEvent), and this resulting in another published event which unassigns the equipment from the former customer.

@EventSourcingHandler public void on(EquipmentAssignedEvent event) { logActivity(event); if (this.assignedCustomerId != null) { eventService .publish(GenericEventMessage.asEventMessage( new EquipmentUnassignedFromCustomerEvent(this.id, this.assignedCustomerId)) ); } this.assignedCustomerId = event.getCustomerId(); }

The example itself does work the way intended, but then I realized that on event replay for creating the snapshotEvents, the EquipmentUnassignedEvent gets published a second time (and how many times there are updates between snapshots.

Do I make a conceptional mistake here?

Hi Lars,

Your assumption is definitely correct in the case you call an eventService (which I guess is the EventBus/Store?) to publish a new event.
If you use the AggregateLifecycle.apply() function however, a check will be performed if you’re in a replaying/event-sourcing state or not.
If it is, it will not apply another event during that stage.

Hence my suggestion is to use the AggregateLifecycle.apply() rather than the EventBus directly when publishing events in event sourcing handlers.

Hope this helps!

Cheers,
Steven

Hi Steven,

thanks, that helped indeed. You were right, the eventService.publish was calling directly on the EventStore.

Cheers,
Lars