Event handler pre/post hooks?

Hi,

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:

Approach 1:

Hi,

you can also create a custom Cluster instance and assign the Listeners that you want the processing on to that instance.

Cheers,

Allard

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.

-Raghu

Hi Raghu,

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
}

Hope this helps.
Cheers,

Allard

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{

@Override
protected AnnotationCommandHandlerAdapter initializeAdapterFor(Object bean,
ParameterResolverFactory parameterResolverFactory) {
return new WrappedAnnotationCommandHandlerAdapter(bean, parameterResolverFactory);
}
}
`

and

`
public class WrappedAnnotationCommandHandlerAdapter extends
AnnotationCommandHandlerAdapter {

public WrappedAnnotationCommandHandlerAdapter(
Object annotatedCommandHandler,
ParameterResolverFactory parameterResolverFactory) {
super(annotatedCommandHandler, parameterResolverFactory);
}

@Override
public Object handle(CommandMessage command, UnitOfWork unitOfWork)
throws Throwable {
try {
if(command.getMetaData().containsKey(“tenantId”)) {
String tenantId = (String) command.getMetaData().get(“tenantId”);
CurrentTenantHolder.set(tenantId);
}
return super.handle(command, unitOfWork);
} finally {
CurrentTenantHolder.reset();
}
}

}

`

Thanks Allard! Great to have multiple approaches.

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 :slight_smile: - but essentially I have the same code for the event listener bean post processor as well.

Hi,

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.

Cheers,

Allard