Disconnect declarative message bus listener


In my old code I was able to disconnect from message bus connections by running MessageBuseConnection#disconnect(). I don't have such a connection when using declaratively defined listeners. Is that not possible anymore?

In my case sometimes the listeners found out that they don't need to be called anymore and would unsubscribe. As the listeners are supposed to be stateless I also can't save that information in a field and just ignore the call.


Could you please give a real world example? You could still use programmatic registration of listeners if explicit unsubscribe is absolutely necessary in your plugin.


I wrote a plugin that provides extra functionality for some of our projects. It adds windows, menu entries etc. which I only want to appear when these projects are open. To recognize these projects (initially) I listen for repository change events because then I can analyze the remote's URL to determine if they contain certain strings. It's a bit hacky but doesn't require user interaction. After a project has been analyzed I save the result in a @State class. As soon as this is done I do not need to be informed about any repository change events and I disconnect from the message bus.

With a declarative listener I would be informed about every repository change anyway, would check the config and then return. I guess the overhead is minimal but for me it makes more sense that I can subscribe to events (and unsubscribe from them) depending on a state than register a listener which will always be called and then needs to check. That seems to run against the performance improvements you're trying to achieve.

Perhaps it would be a good idea if you'd always have to declare the listener but could disable (and reenable) it? That way the listener is always known at compile time and can be easily found by code analysis but still can be controlled at runtime.


I don't see how this could be generalized in a declarative way, other than introducing some separate listenerAvailabilityCondition class to specify and delegating all the work to the MessageBus. Not sure that would be a (performance) improvement.


Please sign in to leave a comment.