I have a need where before event handlers are invoked, I’d like to set a thread local and then when the event handler is done, to reset the thread local. For obvious reasons, I’d rather not have this happen in each event handler. Looking at axon framework code, here’s an approach I have the following two approaches:
hmm - I’m currently using AsynchronousCluster. Looking at the code, it schedules the actual publish to the executor service using a EventProcessor (Runnable). So looks like I would need to subclass EventProcessor, then subclas AsynchronousCluster and implement newProcessingScheduler to return my custom EventProcessor.
Does that sound about right? The AsynchronousCluster and EventProcessor code both are quite long and complicated and I have just a basic understanding of the flow so this seems a little daunting atm.
you can create a subclass of AsyncronousCluster, and override the newProcessingScheduler method to return a subclass of EventProcessor. In that subclass, you override the doHandle method to do whatever you require it to do.
Something like this:
try {
// set threadlocal
return super.doHandle(…);
} finally {
// remove threadlocal
}
I went with Approach 1 and it seems to be working as intended. Haven’t tested it extensively, but hopefully this is useful for anyone who chances upon this thread.
`
public class CustomCommandHandlerAnnotationBeanPostProcessor extends AnnotationCommandHandlerBeanPostProcessor{
I just posted as well below - went with subclassing the AnnotationBeanPostProcessor - do give it a once over if you can. PS - just realized that I posted the code for the command handler - but essentially I have the same code for the event listener bean post processor as well.
it looks allright. If I come across an easier way, I’ll let you know. Meanwhile, I’ll take this usecase in consideration while designing the Axon 3 API’s.