Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat application deployment listener

Tags:

java

tomcat

I'm wondering how can I listen for Tomcat web application deployments. I would like to have my listener invoked every time an application is undeployed or deployed from/to the container.

I already investigate a bit and found out that some listeners, i.e. LifecycleListener can be registered over JMX. But unfortunatelly this listener ins't enough for me since it triggers events just when Engine/Host/Context is in shutdown or startup process.

The same with ContainerListener that basically informs container shutdown and startup events.

So, my question basically is: which interface shall I implement and how can I register it to tomcat in order to be notified every time a new application is deployed?

like image 790
Francisco Spaeth Avatar asked Dec 20 '25 15:12

Francisco Spaeth


1 Answers

servlet context init/destroy

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;

import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;

public class AppContextListener implements ServletContextListener {

    private static final Log logger = LogFactory.getLog(AppContextListener.class);

    @Override
    public void contextDestroyed(ServletContextEvent e) {
        logger.warn("AppContext Delete: " + e.getServletContext().getContextPath());
    }

    @Override
    public void contextInitialized(ServletContextEvent e) {
        logger.warn("AppContext Create: " + e.getServletContext().getContextPath());
    }

}

and put into tomcat/conf/web.xml

   <listener>
     <listener-class>AppContextListener</listener-class>
   </listener>
like image 61
daggett Avatar answered Dec 23 '25 06:12

daggett



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!