5
votes

org.springframework.boot.loader.Launcher by default will always spawn a background thread in the launch() method (https://github.com/spring-projects/spring-boot/blob/master/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/Launcher.java#L105). Is there any easy way to change this to execute in the same thread, so my service launcher can know when the application has fully loaded, and not simply exit with success immediately every time.

1
looking at it you could use your own implementation, extend jarlauncher/warlauncher class and override the launch method.MarianP
Yes I understand I can override that method easily. The problem is spring-boot does not easily allow a way to use a different mainClass for the jar launch method.Todd Lindner
The context class loader cannot be set without spawning a new Thread so you might get into trouble if you try to change this method. What is your "service launcher" actually doing?Phil Webb
It is a launcher for Microsoft Windows services. By default it waits for the main method to complete to signal the service is "UP" - and also any exceptions in the main method (for example a spring context startup failure) would cause an exit code 1 from java.exe which would signal the service didn't start properly. I miss both of those things because of the thread fork.Todd Lindner

1 Answers

0
votes

I believe you can register listener like this:

public static void main(String[] args) {
   SpringApplication app = new SpringApplication(WsBootClientApplication.class);

   app.addListeners(new ApplicationListener<ContextStartedEvent>() {
      @Override
      public void onApplicationEvent(ContextStartedEvent event) {
         //do your stuff
      }
   });
   app.run(args);
}

If ContextStartedEvent is not the right event for you, you can replace ContextStartedEvent with ApplicationEvent. That is more generic and will handle all context lifecycle events your application can listen to.