Using Interceptors to Handle Commands and Events

Hello,

I was wondering if we can write our own interceptors which can allow me to “DO SOMETHING” before / after the event is published on the event bus or command.

Hi Ajinkya,

You’ve definitely have the option to introduce your own implementations of MessageDispatchInterceptors and MessageHandlerInterceptors to “DO SOMETHING”.
The dispatch interceptors are to add functionality before a message is dispatched (on the command or event bus) and the handler interceptors give you the option to add functionality around the message handling at the receiving end.
The Javadoc of both interfaces might give you some insight how to approach this.

Lastly, both the command and event bus have functions to register interceptors (although the EventBus only allows dispatch interceptors, handler interceptors go on the event processors).

Hope this helps.

Cheers,

Steven

Hi,

It is certainly possible to use interceptors in Axon Framework, both for commands and events.

Each command bus implementation supports dispatch interceptors (which are invoked when the command message is dispatched onto the bus) and handler interceptors (which are invoked before the command message is handled by its handler method).

Event busses support dispatch interceptors (which are invoked when the event message is dispatched onto the event bus) and the Event Processors support handler interceptors (which are invoked before the event message is processed by its handler method).

Dispatch interceptors and handler interceptors support modifying or blocking the event message, handler interceptors also support modifying the result.

For more information, also see https://docs.axonframework.org/part3/command-dispatching.html#command-handler-interceptors

Hope this helps, Oscar