-
Notifications
You must be signed in to change notification settings - Fork 38k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Asynchronous initialization of beans during startup [SPR-14920] #19487
Comments
Antonio Anzivino commented Thanks for making progress on this. I will add additional comment An This introduced all kinds of hell for bean deadlocking, but is a very simple approach and can be configured by means of parallelism and timeout to address the deadlocking issues. After all, Spring already detects circular dependencies, and timeouts are widely used in detecting a deadlock. |
This issue also happens with Coroutines when they are used with |
See #13410 (comment) for a variant of this to be introduced in 6.2, at the granularity of the entire bean creation phase for a particular bean. The new background initialization flag can be set for individual beans. Injection points are not proxies by default but can be declared as Alternatively, there is still the pattern of bean-internal asynchronous bootstrapping, along the lines of what |
Antonio Anzivino opened SPR-14920 and commented
Some projects take a few minutes to start up because of I/O operations required for their initialization. For example, one of my projects queries a European Central Bank web service as part of its init sequence, while other beans query the database to retrieve the initial data set.
In general, Spring initializes beans one by one on the same thread and it has been working safe so far.
It is quite a few years I was thinking to this: make bean initialization asynchronous.
I imagine the process like this: if a bean's initialization method (AsyncInitializingBean?) returns a Future<Void>, then Spring uses the default task executor to defer bean initialization and go to the next bean. Only when all the Futures return, the context is said to have initialized.
If a bean has a dependency on an asynchronous bean, then obviously its initialization cannot start before the dependent asynchronous bean has initialized.
I am opening this ticket to get feedback from the community.
And now let's talk about the workaround, because there is one. A developer can speed up the context init process by manually deferring I/O operations after init, but then the developer has to make sure that beans calling it in an uninitialized state (before the deferred initialization completes) get a consistent result, e.g. implementing locks on all methods. This works but requires a lot more code.
Example:
Thanks for your feedback
Issue Links:
2 votes, 4 watchers
The text was updated successfully, but these errors were encountered: