Maybe I’m missing something in the documentation, but it is not clear to me, when to use Command Handler Enhancers and when to use Command Handler Interceptors.
Handler Enhancers documentation start with this paragraph
Handler Enhancers allow you to wrap handlers and add custom logic to the execution, or eligibility of handlers for a certain message. This differs from
HandlerInterceptors in that you have access to the aggregate member at the time of resolving, and it allows for more fine-grained control. You can use handler enhancers to intercept and perform checks on groups of
When exactly to use which, would very much depend on what your need is.
Interceptor can only deal with a message before and/or after it is dispatched/handled. It can’t modify what the handler does with it. So if intercepting the message itself can get the job done (filter messages out, add/update/remove data to a message that handler already knows how to deal with, …) then go with an interceptor. To use interceptors, one does not need to dive deep into the message handling processes of the framework. It’s sufficient to know when interceptors are executed.
Enhancer allows to change how the actual message handling works. It allows to dynamically and conditionally change the handling logic. One can wrap a handler and provide custom implementation, configure it to handle messages that it didn’t handle before or stop handling those it did handle. It is powerful tool but requires good understanding of how the message handling process in Axon Framework works.
Another way to look at it is that interceptors are more “business data” layer while enhancer are more “infrastructure” layer. Or if you are familiar with servlets and web containers, then you can think of the interceptors as “servlet filters” and enhancers being something like Tomcat’s valves.
I hope that helps.