Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback on Tomcat server startup complete

Is there any mechanism, lifecycle event or callbacks, in Spring or Tomcat to notify once Tomcat server startup is completed? (I have 8 web applications and queues configured. I would prefer to get notification back to each application once all the applications are started.) I know Spring has the application listener, which can be used once the web application is initialized. But I cannot use it in my case because I would prefer to get a notification once all the web apps are initialized.

EDITED*

I've implemented a Tomcat listener to log the message, but I have absolutely no idea where to hook this listener.

I tried to create this bean using Spring and also adding the listener to web.xml both did not work.

Here is my code:

public class KPTomcatListener implements LifecycleListener {

    private static final Logger LOG = LoggerFactory.getLogger(KPTomcatListener.class);
    /**
     * All the events of tomcat
     * See: https://tomcat.apache.org/tomcat-10.0-doc/api/constant-values.html
     * AFTER_DESTROY_EVENT     "after_destroy"
     * AFTER_INIT_EVENT     "after_init"
     * AFTER_START_EVENT     "after_start"
     * AFTER_STOP_EVENT     "after_stop"
     * BEFORE_DESTROY_EVENT     "before_destroy"
     * BEFORE_INIT_EVENT     "before_init"
     * BEFORE_START_EVENT     "before_start"
     * BEFORE_STOP_EVENT     "before_stop"
     * CONFIGURE_START_EVENT     "configure_start"
     * CONFIGURE_STOP_EVENT     "configure_stop"
     * PERIODIC_EVENT     "periodic"
     * START_EVENT     "start"
     * STOP_EVENT     "stop"
     */
    private static int counter;
    
    @Override
    public void lifecycleEvent(LifecycleEvent arg0) {
        String event = arg0.getType();
        LOG.debug("Tomcat Events: " + (++counter) + " :: " + event);
        if(event.equals(org.apache.catalina.Lifecycle.AFTER_START_EVENT)) { // or "after_start"
            LOG.debug("Hey I've started");
        }
    }

}
like image 940
Karthik Prasad Avatar asked Aug 30 '26 03:08

Karthik Prasad


1 Answers

All of the major Tomcat components implement org.apache.catalina.Lifecycle which includes the ability to add a org.apache.catalina.LifecycleListener. It sounds like you want the AFTER_START_EVENT of the Host.

You configure the listener in server.xml like this:

<Host ... >
  <Listener className="your.package.KPTomcatListener"/>
  <!-- Other nested elements go here -->
</Host>

The class must be packaged in a JAR and the JAR placed in Tomcat's lib directory.

like image 145
Mark Thomas Avatar answered Aug 31 '26 21:08

Mark Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!