Tomcat应用程序部署侦听器 [英] Tomcat application deployment listener
问题描述
我想知道如何监听Tomcat Web应用程序部署。我希望每次取消部署应用程序或从容器部署应用程序时都会调用我的监听器。
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.
我已经调查了一下,发现有些听众,即 LifecycleListener
可以通过JMX注册。但不幸的是,这个监听器对我来说还不够,因为它只是在引擎/主机/上下文处于关机或启动过程时触发事件。
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.
与 ContainerListener
相同,它基本上会通知容器关闭和启动事件。
The same with ContainerListener
that basically informs container shutdown and startup events.
所以,我的问题基本上是:我应该实现哪个接口以及如何将其注册到tomcat以便每次部署新应用程序时得到通知?
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?
推荐答案
servlet context init / destroy
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());
}
}
并放入tomcat / conf / web.xml
and put into tomcat/conf/web.xml
<listener>
<listener-class>AppContextListener</listener-class>
</listener>
这篇关于Tomcat应用程序部署侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!