I need that my Springboot application knows when its web server (Tomcat) is up
I tried something using @WebListener, ServletContextListener and @ServletComponentScan
@WebListener
public class MyServletContextListener implements ServletContextListener {
private static final Logger LOG = getLogger(MyServletContextListener.class);
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
LOG.info("Starting up!");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
LOG.info("Shutting down!");
}
}
It seems to work: my listener is correctly notified.
However, I realized that if I try to join my Web server from this listener, the call fails
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
URI uri = HttpHelper.newAnyUriBuilder("localhost", 8181, false).path("actuator/health").build();
new RestAPI().GET(uri); /// Throws
LOG.info("Starting up!");
}
meaning that the web server is not completely UP AND READY
If I postpone the call, it succeeds:
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
new Thread(new Runnable() {
@Override
public void run() {
try { Thread.sleep(1000); } catch (InterruptedException e) {} /// dirty but this is just a sample
URI uri = HttpHelper.newAnyUriBuilder("localhost", 8181, false).path("actuator/health").build();
new RestAPI().GET(uri); /// does not throw anymore
LOG.info("Starting up!");
}
}).start();
}
Any idea how can I be notified correctly?
Thanks