Help with MessageHandlerInterceptor registration on EventProcessingConfiguration

HI,

I have a requirement to implement an audit of events that occurred in our system to a separate system that aggregates auditing of several systems. The approach that I want to take is to implement a MessageHandlerInterceptor that does the necessary work after the commit of the event to the event store. For brevity I omitted the part where it does the call and used a simple log statement.

`

@Slf4j
public class AuditInterceptor<T extends Message<?>> implements MessageHandlerInterceptor {
@Override
public Object handle(final UnitOfWork<? extends T> unitOfWork, final InterceptorChain interceptorChain) throws Exception {
unitOfWork.afterCommit(unit -> {
final T message = unit.getMessage();
log.info("{}",message);
});
return interceptorChain.proceed();
}
}

`

I want to take this approach since I don’t want to have to register Event Handlers for every event in the system to do the auditing. The issue that I am facing currently is registering the above interceptor with the appropriate EventProcessor. In our project we are not registering an EventProcesor explicitly, but instead rely on the default configured event processor which is pulled in by Spring Boot Autoconfigure. However I am unsure on how to get access to this. The documentation for Axon 3.1 gives this example :

`


public class EventProcessorConfiguration {
    public EventProcessingConfiguration eventProcessingConfiguration() {
        return new EventProcessingConfiguration()
                .registerTrackingEventProcessor("my-tracking-processor")
                .registerHandlerInterceptor("my-tracking-processor", configuration -> new MyEventHandlerInterceptor());
    }
}

`

However I am unable to find the EventProcessorConfiguration in the classpath in order to register the Handler Interceptor. A likely candidate for this seems to be TrackingEventProcessorConfiguration but this seems to be an internal class of the Axon framework.

So my questions are :

  1. How do I register the above interceptor with the Event Processor such that it is invoked after an Event is committed ot the Event store?

  2. Am I on the right track with this approach or is there another way that I am not considering?

  3. If this can be configured in this way. my hope is that it will only execute the first time that an event is applied on an aggregate. Will this be the case, or will the interceptor be invoked when events are replayed?

The default is not tracking but subscribing, but why not make a listener for (any) event. You can get the UnitOfWork in an event handler the same you do now.

And then you can discard the handler on replaying with @AllowReplay(false)

That is, we have al base class for all our events , thus it is easy for us to make a single handler for all our events… Don’t know if you can use a axon DomainEventMessage als a parameter in an handler as an alternative.
If you still want to register a interceptor i think you have to use eventBus().registerDispatchInterceptor() , so you don’t rely on the event processor configuration.

We don’t have a base class for our events in our current application and I would like to avoid the extra requirement for events in order to apply the same pattern in other applications as well where such a refactor could have a bigger impact.

The problem with the Dispatch Interceptors is that they are invoked before the Unit Of Work has been created, which would mean that I won’t be able to have the Auditing to occur only after the event has been committed to the store.

I have since found the EventHandlingConfiguration class which has reference to the EventProcessors which have methods to register MessageHandlerInterceptors, but this EventHandlingConfiguration does not seem to be AutoConfigured with Spring Boot.

Hi Mare,

dispatch interceptors are always invoked in the unit of work of the message that triggered the intercepted message to be published. This means there could be no active unit of work.
However, if you configure your dispatch interceptor on your Event Store, there should always be an active Unit of Work for the command that causes these events to be dispatched. In that interceptor, you can register an “afterCommit” handler that logs (or does whatever it needs to do) the intercepted events.

When using Spring Autoconfiguration, you should be able to simple register an @Autowired method that has an “EventHandlingConfiguration” as parameter. Spring will invoke that method for you, allowing to tweak & tune it to your liking.

Hope this helps.
Cheers,

Allard