Tomcat服务器启动上的回调已完成 [英] Callback on Tomcat server startup complete

查看:224
本文介绍了Tomcat服务器启动上的回调已完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Tomcat服务器启动完成后,Spring或Tomcat中是否有任何机制,生命周期事件或回调来通知? (我配置了8个Web应用程序和队列.我希望在所有应用程序启动后将通知返回给每个应用程序.)我知道Spring具有应用程序侦听器,一旦Web应用程序初始化,便可以使用该侦听器.但是我不能使用它,因为我希望在所有Web应用程序初始化后收到通知.

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*******

我已经实现了一个Tomcat侦听器来记录消息,但是我完全不知道在哪里挂接该侦听器.

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

我尝试使用Spring创建此bean,并且还向web.xml中添加了侦听器,但这两个都不起作用.

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

这是我的代码:

public class KPTomcatListener implements LifecycleListener {

    private static final Logger LOG = LoggerFactory.getLogger(KPTomcatListener.class);
    /**
     * All the events of tomcat
     * AFTER_START_EVENT, 
     * AFTER_STOP_EVENT, 
     * BEFORE_START_EVENT, 
     * BEFORE_STOP_EVENT, 
     * DESTROY_EVENT, 
     * INIT_EVENT, 
     * PERIODIC_EVENT, 
     * START_EVENT, 
     * STOP_EVENT
     */
    private static int counter;

    @Override
    public void lifecycleEvent(LifecycleEvent arg0) {
        String event = arg0.getType();
        LOG.debug("Tomcat Envents: " + (++counter) + " :: " + event);
        if(event.equals("AFTER_START_EVENT")) {
            LOG.debug("Hey I've started");
        }
    }

}

推荐答案

所有主要的Tomcat组件均实现org.apache.catalina.Lifecycle,其中包括添加org.apache.catalina.LifecycleListener的功能.听起来像您想要HostAFTER_START_EVENT.

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.

您可以像这样在server.xml中配置侦听器:

You configure the listener in server.xml like this:

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

该类必须打包在一个JAR中,并且该JAR必须放置在Tomcat的lib目录中.

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

这篇关于Tomcat服务器启动上的回调已完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆