Search code examples
eventhandleraxon

Axon event handler should - indirectly - emit new event


I have an application which consists of storing custom queries against some objects. These objects are managed in another application so I only receive events whenever something happens on these objects.

Now every time a new instance of such an object appears - which I know by an event - I want to check if it matches any of my custom queries and if so, emit an event that there is a new match.

The query does not know in advance which objects might match, and the objects in turn do not know of the existence of any of these queries. So from my understanding, a saga will not help me solve this problem.

But I think an event handler should not directly emit new events. So I wonder if I should create a command to send to my query objects so it can execute and check for a match, or if there is some other pattern I can apply.

I don't think my custom query aggregate is the right place to check for new matches, since it actually does not care; it just manages the query. But having to introduce a new aggregate and send it a command, just to let it check for new matches, feels like it might not be the right place either.

Any advice on this issue?


Solution

  • It looks like a Set Based Validation to me! If that is the case, AxonIQ has a pretty good blog about this problem/pattern and ways of solving it using AxonFramework!

    https://axoniq.io/blog-overview/set-based-validation

    Basically, you can have a projection close to your Aggregate where you can check for those values, if they exist or not. Mind you that those projections should never be exposed to the outside world and only used by the Aggregate itself configured with a Subscribing Event Processor, meaning they will be updated right away.