question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@EventListener annotated bean cannot be removed from the ApplicationEventMulticaster

See original GitHub issue

Affects: 2.4.3

According to the JavaDoc of the ApplicationEventMulticaster I was expecting to be able to remove and/or add ApplicationListeners at runtime.

This currently does not work for @EventListener annotated methods, which apparently lead to a ApplicationListenerMethodAdapter instance at runtime.

Would be great, if we could remove this listener as well, either by

  1. making the Adapter instance injectable and then remove it with multicaster.removeApplicationListener(), or
  2. giving the Adapter instance a well defined bean name and remove it with multicaster.removeApplicationListenerBean()

I’ve included a small demo app with a failing test to illustrate what I’m trying to achieve:

event-sample.zip

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jhoellercommented, Mar 8, 2021

I’ve introduced an ApplicationEventMulticaster.removeApplicationListeners(Predicate<ApplicationListener<?>>) method in combination with ApplicationListener.getListenerId(), derived from the optional @EventListener.id attribute and defaulting to the signature of the annotated method. The latter two have been pulled up from TransactionalApplicationListener and @TransactionalEventListener where they had similar semantics already (recently introduced in 5.3, so not widely known).

I hope this works for your use case, e.g.: removeApplicationListeners(l -> l.getListenerId().startsWith("myrootpackage.mymodule"))

0reactions
jhoellercommented, Mar 9, 2021

I’ve revised this a bit to only expose the getListenerId() method on SmartApplicationListener (and also on GenericApplicationListener which derives from it now), avoiding potential conflicts with plain ApplicationListener implementations that happen to implement other interfaces as well (keeping the base interface as minimal as possible).

This means that a downcast will be necessary, I hope that’s still not too bad:

removeApplicationListeners(l -> l instanceof SmartApplicationListener &&
        ((SmartApplicationListener) l).getListenerId().startsWith("myrootpackage.mymodule"))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamically remove a @EventListener in Spring
How can I get hold of the name of this bean, so that I can remove the bean with the ApplicationEventMulticaster ? eventMulticaster....
Read more >
Event Handling With Spring Framework
Spring 4.1 introduced @EventListener , annotation driven event Listener which removed all the complexities of defining your EventListner.
Read more >
EventListener (Spring Framework 6.0.2 API)
Annotation that marks a method as a listener for application events. If an annotated method supports a single event type, the method may...
Read more >
2 Reasons of org.springframework.beans.factory ...
At the startup, when the Spring framework initializes the system by creating objects and their dependencies depending upon @Autowired annotation or spring ...
Read more >
org.springframework.context.event ...
ApplicationEventMulticaster maven / gradle build tool code. ... @param listenerBeanName the name of the listener bean to remove * @see ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found