How to detect Tomcat Startup Programmatically from within a Servlet?

喜你入骨 提交于 2021-01-29 05:40:21

问题


Question

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?

Background

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.

What I've Tried

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.


回答1:


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.)

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.

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).

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.



来源:https://stackoverflow.com/questions/53656052/how-to-detect-tomcat-startup-programmatically-from-within-a-servlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!