It's not clear from the Spring documentation reference or JavaDoc if Lifecycle.stop()
is supposed to execute synchronously or asynchronously. Does anyone know?
Looking at the code the stop()
method seems to be implemented in lots of places, but I'd say that it is typically synchronous. One clue is that the implementation of stop()
in AbstractApplicationContext is:
public void stop() {
getLifecycleProcessor().stop();
publishEvent(new ContextStoppedEvent(this));
}
They wouldn't publish a "the context has stopped" event if the context was still in the process of stopping.
You could side-step this question by registering an event listener and wait for the ContextStoppedEvent
. But even that won't protect you against some broken class that neglects to publish the event, or that publishes it before the instance has fully stopped.
Another option is to ask this question on the Spring forums ... or even raise an issue against the relevant documentation / javadocs.
The event distribution mechanism is pluggable. By default it's synchronous, but you can instruct the context to distribute the lifecycle events in an asynchronous or even multi-threaded fashion, if you so desire.
So it's not documented because it's not defined, it's left up to you.