Francisco Spaeth
Francisco Spaeth

Reputation: 23893

Tomcat application deployment listener

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?

Upvotes: 3

Views: 2107

Answers (1)

daggett
daggett

Reputation: 28564

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>

Upvotes: 1

Related Questions