Should deadlines be scheduled in a @CommandHandler or @EventSourcingHandler?
For an aggregate:
- When scheduling a deadline the
deadlineManager.schedule(..)method returns a
- Given that we need to store that ID in the state of the aggregate (in case we need to cancel the deadline later) I assumed it would go in an
@EventSourcingHandler(since we are changing the state of the aggregate).
- However, placing it in an
@EventSourcingHandlerresults in tests failing with:
deadlineId has different value when sourcing events.
- I believe this is expected behaviour because the test fixture runs the ‘given, when, then’ which is defined on it and then runs each event sourcing handler again and makes sure that the states match.
- Since when the
@EventSourcingHandleris run for the second time the
scehdule(...)method is called again and a different
deadlineIdis stored as the state - this results in conflicting state between the first time the handlers are run and the second.
- When using a
@CommandHandlerthe test fails as well.
So my question is: what is the correct way to schedule a deadline and store the deadlineId for an aggregate?
For a saga:
- I’m assuming it is best practice to schedule a deadline on a
@SagaEventHandler. Everything works here without issue.