Intercepting event handlers

We have a use case where we would like to intercept all event handlers.
There are audit data associated with all commands which are applied to
the events using the AuditDataProvider.
Some events will trigger new commands which should propagate the same
audit data.

Now when an event triggers a command we want the audit data associated
with the event to be applied to the command.
My initial thoughts are to intercept the event handler method and make
the audit data available later for the AuditDataProvider to apply to
the command.

I couldn't find a generic way, provided by the framework, to intercept
the event handlers. Any thoughs on this? Where should I do this?

Sounds more like a job for fex Spring AOP than for Axon, no?

Hi Ted,

I agree with Sebastian. If you want something to happen on all event handlers, AOP is most likely the way to go.
Recently, a colleague of mine solved a similar problem using relatively simple aspects. Since there is always an annotation on the event handler methods, it’s pretty easy to define the pointcut expression.

Cheers,

Allard

Alright, I got a nice pointcut expression to catch all event handlers
(except sagas) but the saga pointcut was trickier..
In my opinion this should work
"execution(@org.axonframework.saga.annotation.SagaEventHandler * *(*, ..)) ||
            (execution(void org.axonframework.saga.Saga.handle(*))
                    &&
!within(org.axonframework.saga.annotation.AbstractAnnotatedSaga))"
But it doesn't.
An alternative would be a pointcut to catch all "handle(*)" for both
EventHandlers and Sagas.

Hi Ted,

do you use Spring AOP? If so, that’s the reason why it doesn’t work: Saga instances are not Spring beans. There is a special trick to use Spring AOP to wire unmanaged object. I wrote a blog about that quite a while ago: http://www.gridshore.nl/2009/01/27/injecting-domain-objects-with-spring/

Maybe that helps.

Cheers,

Allard