Publish a DomainEventMessage outside of an aggregate
See original GitHub issueHello,
it’s more a question or feature request than an issue.
I’m working in the Smart Home/IoT-domain and our system consumes events (messages) from gateways/devices via AMQP. The events represent domain events and we want to store them in the event store (keyword: event sourcing). Gateways/devices are our aggregate roots and we want to able to reconstruct their state based on events (keyword: digital twins).
I need a way to apply a GenericDomainEventMessage
outside of an aggregate. The simplest solution would be to to use the EventBus
to publish the GenericDomainEventMessage
. The Event Message could have a field with an @TargetAggregateIdentifier
annotation. The EventBus
then knows how to set the aggregateIdentifier in the event store and how to increase the sequenceNumber.
What do you think?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:7 (2 by maintainers)
Top GitHub Comments
Hi @RobWin,
Interesting use case you’ve got there. Typically I’d not be overly enthusiastic to let outside sources influence your aggregates, but this seems a little different from the other use cases I’ve come across. I’ve discussed it with @abuijze and we’ve not reached the point that it should be a feature in the framework yet, but created this yourself should be fairly easy.
So, what I’d suggest to do is the following:
EventStore#lastSequenceNumberFor(String aggregateIdentifier)
.GenericDomainEventMessage
you’re going to append you can just call the constructor of theGenericDomainEventMessage
, providing the aggregatetype
, theaggregateIdentifier
(which you’ve previously used to pull the sequence number), thesequenceNumber
you’ve retrieved in the previous operation and the payload, being the event you’ve received. You can additionally provide you own meta data as aMap
, which could contain the correlation id of the AMQP message, a message identifier and a timestamp. I’d suggest to leave the latter two to theGenericDomainEventMessage
constructor, but you could provide your own if necessary.GenericDomainEventMessage
, you can call theEventBus#publish(List<? extends EventMessage<?>> events)
function to publish your event. If you’re using theEmbeddedEventStore
(which I guess you will), the event will be stored, and after that handled by any Event Listeners you have.The only caveat to this is that the
EventStore#lastSequenceNumberFor(String aggregateIdentifier)
is not in a release yet. It’s already part of 3.2 though, which should be released somewhere end of month.Would this solution work out for you @RobWin?
Hi guys
I think that I have similar problem.
Concretely I use sagas to handle payment transaction. My scenario is following:
When PaymentExpiredEvent occurs it is stored into db where event identifier is same as aggregate identifier column . That means that such event can not be used during event sourcing of my aggregate.
Q: Am I right? Is this correct behaviour?
If so, what I can do as RobWin mentioned is to trigger another PaymentExpiredCommand from saga and create similar ExpiredEvent which will be joind with aggregate. But I want to avoid this if possible.
Q: What can I do?
thank you very much