如何从Servlet中以编程方式检测Tomcat启动? [英] How to detect Tomcat Startup Programmatically from within a Servlet?

查看:90
本文介绍了如何从Servlet中以编程方式检测Tomcat启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

如何以编程方式确定Tomcat何时完成启动?我的意思是说,是从Tomcat上运行的WAR文件中以编程方式实现的,很可能是通过Servlet,如果可能的话,该Servlet可以利用容器信息?

How can you programmatically determine when Tomcat has completed startup? I mean programmatically from within a WAR file running on Tomcat, most likely via a Servlet that can tap into container info if possible?

背景

在WAR完全部署后,您通常会看到的Catalina日志条目与我要寻找的条目相同.我有一个约束,使得不希望解析日志.但是去Catalina.out的数据正是我要寻找的.我正在尝试寻找一种方法,以使其在Servlet之上甚至更高一些,以获取有关启动时间的容器状态,但我对可能的替代方法持开放态度.

The Catalina log entry that you normally see after a WAR has fully deployed would be equal to what I'm looking for. I have a constraint that makes parsing the logs undesirable. But the data that goes to Catalina.out is precisely what I'm looking for. I'm trying to find a way to go a layer above the servlet, maybe even higher, to obtain container status with regards to Startup time but I'm open to alternate methods if they are possible.

我尝试过的事情

我尝试检测servlet初始化,但是那是特定于WAR中的组件的,而不是servlet容器本身的.我还尝试了Spring Events,但是它们提供的最高级别是应用程序上下文",它仅指示一组Spring Bean已完成加载,甚至不一定指示所有Spring Bean ...如果有一种解决方案更接近Servlet Spec或Tomcat特定的库,将不胜感激.

I tried detecting servlet initialization but thats specific to components inside the WAR and not the servlet container itself. I also tried Spring Events but the highest level they provide are "Application Contexts" which only indicate a group of Spring Beans have finished loading and not necessarily even all Spring beans...If theres a solution thats closer to the Servlet Spec or a Tomcat specific library that would be appreciated.

推荐答案

仅当Tomcat已完全完成启动并正在运行和处理请求时,Servlet才执行.所以,对不起,这个问题没有道理. (这就像您不能如实地用否"回答您醒了吗?"的问题.)

A Servlet is only executing if Tomcat has fully completed it's startup and is running and processing requests. So, sorry but, the question does not make sense. (It's like you can't answer the question "Are you awake?" with "no" truthfully.)

在Tomcat启动完成之前,Web应用程序运行的所有内容是您实现和注册的(在server.xml中的)任何ServletContextListener. Tomcat启动应用程序后,它将调用任何已注册的ServletContextListener的contextInitialized()方法.

All that runs of a Web Application before Tomcat startup is completed, is any ServletContextListener that you implemented and registered (in server.xml). When the application has been started by Tomcat, it calls the contextInitialized() method of any registered ServletContextListener.

如果要在Tomcat生命周期内执行某些代码,则可以使用Tomcat的org.apache.catalina.LifecycleListener接口.在自己的类中实现此方法,然后在服务,引擎或主机的server.xml中注册该类.这样的类将不会驻留在您的Web应用程序中,而是会部署在Tomcat的lib目录中(类似这样;检查您的Tomcat版本).

If you want some code to be executed around the Tomcat lifecycle, you could use Tomcat's org.apache.catalina.LifecycleListener interface. Implement this in an own class and register the class in server.xml at a Service, Engine or Host. Such a class will not reside in your webapp, but is deployed in the lib directory of Tomcat (something like that; check for your version of Tomcat).

从概念上讲,一个Web应用程序永远无法知道同一Servlet容器(即Tomcat)中的任何其他Web应用程序;规范严格禁止这样做.没有任何方法可以以任何方式连接两个应用程序.在Tomcat中,"特权"应用程序除外,例如 Tomcat Manager .如果您将应用程序定义为特权应用程序,则它确实具有处理其他Web应用程序的某些方法.

Conceptually, one Web Application can never know of any other Web Application in the same Servlet Container (i.e. Tomcat); this is strictly prohibited by the specification. There are no methods to connect two apps in any way. In Tomcat, there is the exception of a "privileged" application, such as the Tomcat Manager. If you define an application as privileged, it does have certain means to deal with other webapps.

这篇关于如何从Servlet中以编程方式检测Tomcat启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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