Spring Boot - Wait for web server to start

后端 未结 2 1136
予麋鹿
予麋鹿 2020-12-15 07:43

In my Spring Boot application I need to wait until the (default Tomcat) web server is fully initialized and ready to take traffic before I send messages to other application

相关标签:
2条回答
  • 2020-12-15 08:14

    Since Spring Boot 1.3.0 this can also be accomplished by implementing ApplicationListener<ApplicationReadyEvent>

    example:

    public class MyApplicationListener implements ApplicationListener<ApplicationReadyEvent>, Ordered {
    
        @Override
        public void onApplicationEvent(ApplicationReadyEvent event) {
            //do stuff now that application is ready
        }
    
        @Override
        public int getOrder() {
            return Ordered.LOWEST_PRECEDENCE;
        }
    }
    

    Also, as mentioned in the accepted answer, you can create a file named src/main/resources/META-INF/spring.factories listing your ApplicationListener. For example:

    org.springframework.context.ApplicationListener=com.example.MyApplicationListener
    

    however, in my case, I only needed this listener to run under a specific profile

    so I added the following property to application-<profile_name>.properties

    context.listener.classes=com.example.MyApplicationListener
    
    0 讨论(0)
  • 2020-12-15 08:17

    The simplest thing to do is to send the message once SpringApplication.run() has returned. This method won't return until Tomcat (or any other supported embedded container) is fully started and listening on the configured port(s). However, while this is simple, it's not a very neat approach as it mixes the concerns of your main configuration class and some of your application's runtime logic.

    Instead, you can use a SpringApplicationRunListener. finished() will not be called until Tomcat is fully started and listening on the configured port.

    Create a file named src/main/resources/META-INF/spring.factories listing your run listener. For example:

    org.springframework.boot.SpringApplicationRunListener=com.example.MyRunListener
    

    Create your run listener with the required constructor and implement SpringApplicationRunListener. For example:

    package com.example;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.SpringApplicationRunListener;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.core.env.ConfigurableEnvironment;
    
    public class MyRunListener implements SpringApplicationRunListener {
    
        public MyRunListener(SpringApplication application, String[] args) { }
    
        @Override
        public void starting() { }
    
        @Override
        public void environmentPrepared(ConfigurableEnvironment environment) { }
    
        @Override
        public void contextPrepared(ConfigurableApplicationContext context) { }
    
        @Override
        public void contextLoaded(ConfigurableApplicationContext context) { }
    
        @Override
        public void started(ConfigurableApplicationContext context) {
            // Send message; Tomcat is running and listening on the configured port(s)
        }
    
        @Override
        public void running(ConfigurableApplicationContext context) { }
    
        @Override
        public void failed(ConfigurableApplicationContext context, Throwable exception) { }
    
    0 讨论(0)
提交回复
热议问题