I have a Saga that creates a command which creates the Item aggregate.
I need to execute some other logic after the aggregates gets created so I've got a handler to deal with the ItemCreatedEvent.
It seems the ItemEventsHandler logic is not executed under the Saga instance locking. So I get another event associated to the Saga executing concurrently with the handler.
Is there a way to enforce the handler to be part of the same saga instance handling/execution? Am I missing something?
Using axon 4.3.3 with spring boot.
@Saga
public class OrderSaga {
@Inject
private transient CommandGateway commandGateway;
@StartSaga
@SagaEventHandler(associationProperty = "executionId")
public void handle(OrderCreatedEvent event) {
event.items.forEach(item -> {
// Associate to Saga
SagaLifecycle.associateWith("itemId", item.id);
commandGateway.sendAndWait(new CreateItemCommand(event.groupId, event.schemaId, item));
});
}
}
@Aggregate
public class ItemAggregate {
@AggregateIdentifier
private String id;
private String groupId;
...
@CommandHandler
public ItemAggregate on(CreateItemCommand command) {
AggregateLifecycle.apply(new ItemCreatedEvent(
command.id,
command.groupId,
...));
}
@EventSourcingHandler
protected void on(ItemCreatedEvent event) {
this.id = event.id;
this.groupId = event.groupId;
...
}
}
@Service
public class ItemEventsHandler {
@EventHandler
protected void on(ItemCreatedEvent event) {
// This needs to be executed under the Saga locking
}
}
ItemCreatedEvent
needs to be handled within the same lock as theOrderSaga
. Can you per chance elaborate what the use case for this is @matpiera? – StevenItemEventsHandler
needs to handle events in the same order as the Saga. It talks to a legacy system. What I'm doing now it's havingItem
asAggregateMember
of Order. Does it make sense @Steven? – matpieraOrder
aggregate containsItem
entities does sound logical to me @matpiera, although it always "depends" on the domain whether something makes sense or not. Further more, although you state the events need to be handled in the same order, that still does not explain why that is a necessity in your system. At any point, event handling should be regarded as (conceptually) asynchronous. Thus imposing an order on when which component handles what is not really something I would suggest, unless there is a very clear and specific domain requirement on the matter. – Steven