开发者

Is Spring's Lifecycle.stop() supposed to synchronous or asynchronous?

开发者 https://www.devze.com 2023-01-11 14:48 出处:网络
It\'s not clear from the Spring 开发者_运维百科documentation reference or JavaDoc if Lifecycle.stop() is supposed to execute synchronously or asynchronously. Does anyone know?The event distribution me

It's not clear from the Spring 开发者_运维百科documentation reference or JavaDoc if Lifecycle.stop() is supposed to execute synchronously or asynchronously. Does anyone know?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消