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
Since Spring Boot 1.3.0 this can also be accomplished by implementing ApplicationListener
example:
public class MyApplicationListener implements ApplicationListener, 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-
context.listener.classes=com.example.MyApplicationListener