You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Call runners after ApplicationReadyEvent has been published #7656
Spring Boot provides the very useful ApplicationRunner and CommandLineRunner for executing code immediately before the SpringApplication.run method finishes.
There are many articles and StackOverflow questions (example 1, example 2) that advocate using these for the bulk of the logic within a Command Line application.
However, if you use one of these, then the "Started ApplicationName in X seconds" log message is output after the runners have finished. Additionally, the event ApplicationReadyEvent is called afterwards as well.
This implies that runners are only intended for initialisation work. You should not, in fact, be using them for work that should be considered part of running the application and doing so is a misuse of their intended purpose.
Can we please have some clarity on the proper usage added to the documentation if this is indeed a misuse?
If it is not a misuse then can the runner execution get moved to after the ApplicationReadyEvent, and after the "Started" log message, to more clearly indicate that the application is already, in fact, ready, and now the runner execution is part of the application itself running?
I think that they should probably happen after the started event, although that's not something we'll be able to do until Boot 2.0. I'll tag this for team discussion to see what others think.
Reacted by Rupert Madden-Abbott and Knut Schleßelmann
@wilkinsona this one has a side effect on a web application. If you use to run something in an ApplicationRunner to initialize something, then you may end up in a situation where the HTTP connector is started before the initializer completes.
This is easy to run in problem with devtools and LiveReload as the browser might refresh "too fast" and your app is in an inconsistent state then.
I think we need to make sure to update the release notes to:
Clearly describe that side effect
Provide alternatives (i.e. if you want to run something when everything has completed, but before the connection actually starts
IMO, an application runner shouldn't be used for initialisation. If you need to initialise something it should be done as part of application context refresh. If you need that initialisation to happen after something, then the standard dependency mechanisms offered by the Framework should be used. I'm struggling to think of a use case where a component's dependency would effectively be everything.
+1 on documenting that CommandLineRunner and ApplicationRunner should only be used for initialization work and nothing more.
Found this out while testing spring-boot-admin. Clients just weren't registering with the admin server and it took quite a while to figure out that ApplicationReadyEvent just wasn't ever fired because of what we were doing inside a CommandLineRunner bean..
@frederikvanhoyweghen this issue is closed so please do not comment here to suggest a change. Your use case is also quite specific to Spring Boot Admin so I'd rather ask there but if you have some ideas to generically improve the documentation, please create a separate issue.
Activity
philwebb commentedon Dec 15, 2016
Thanks for raising this.
I think that they should probably happen after the
startedevent, although that's not something we'll be able to do until Boot 2.0. I'll tag this for team discussion to see what others think.[-]Proper use of Runners is ambiguous[/-][+]Call runners after ApplicationReadyEvent has been published[/+]snicoll commentedon Nov 4, 2017
@wilkinsona this one has a side effect on a web application. If you use to run something in an
ApplicationRunnerto initialize something, then you may end up in a situation where the HTTP connector is started before the initializer completes.This is easy to run in problem with devtools and LiveReload as the browser might refresh "too fast" and your app is in an inconsistent state then.
I think we need to make sure to update the release notes to:
wilkinsona commentedon Nov 4, 2017
IMO, an application runner shouldn't be used for initialisation. If you need to initialise something it should be done as part of application context refresh. If you need that initialisation to happen after something, then the standard dependency mechanisms offered by the Framework should be used. I'm struggling to think of a use case where a component's dependency would effectively be everything.
frederikvanhoyweghen commentedon Jan 2, 2019
+1 on documenting that CommandLineRunner and ApplicationRunner should only be used for initialization work and nothing more.
Found this out while testing spring-boot-admin. Clients just weren't registering with the admin server and it took quite a while to figure out that ApplicationReadyEvent just wasn't ever fired because of what we were doing inside a CommandLineRunner bean..
snicoll commentedon Jan 2, 2019
@frederikvanhoyweghen this issue is closed so please do not comment here to suggest a change. Your use case is also quite specific to Spring Boot Admin so I'd rather ask there but if you have some ideas to generically improve the documentation, please create a separate issue.
frederikvanhoyweghen commentedon Jan 2, 2019
Sorry, I didn't notice the issue was closed. I took it up with the spring boot admin maintainers, I'll create a separate issue if need be. Thanks!