With Spring's ApplicationEventMulticaster
you can't subscribe to a response. You probably noticed that the onApplicationEvent
method returns void
! The reason for this is because literally all it does is either call the Subscriber (i.e. ApplicationListener
) synchronously, or runs the listener method asynchronously on an executor without returning any type of Future
.
Spring's Project Reactor evolved a while ago to match the Reactive Manifesto and similar frameworks (like RxJava) more closely. Now with Spring 5 (with which Reactor comes with by default) you can use Reactor and RxJava interchangeably.
That being the case, regarding your questions:
How do I achieve the same behaviour with spring?
You use the new version of Reactor Core and the functional programming features of Flux
, Mono
, etc.
With Spring eventListeners I can publish another event from an
EventListener method, but I am missing the feature to directly
subscribe to a return value.
If you look at the API for Flux
, you'll see that it has a fluent and functional API (in some ways similar to Java 8 streams).
Flux.just(1, 2, 3, 4)
.map(value -> value + 1)
.subscribe(subscriber::function);
This way, you can operate on your "events" (i.e. 1,2,3,4 in this example), perform an operation on what would be a "return value" for those events, and then pipe those to some subscriber operation to consume those events.
How do I programmaticcaly register/unregister listeners and how do I
make the topics dynamic?
You should take a look at this SO answer.
To register/unregister, that's something you can do with what you might call "completors" in the Reactor framework. See the take
functions in the Reactor API. They will signal upstream that they basically want to be unsubscribed, and that the upstream producer should stop emitting.