Register event handlers with dynamic event type

We have a use case where we would like to register event handlers at runtime with a specific event type (only known at runtime).

We’ve done this before by creating an event listener like below parameterised with ApplicationEvent and registering it with the event publisher at runtime:

@EventListener
public void receiveEvent(ApplicationEvent event) {
    ...
}

The problem with this is that it’s not scalable as every event in Bitbucket is handled and for asynchronous events the thread pool queue may eventually become full.

It seems that the event API does not provide a way of registering a set of events an event handler should listen for at runtime.

Is there any way around this without resulting to reflection or some other means to register the supported event types?

2 Likes