中止Tomcat初创公司 [英] Abort Tomcat start-up

查看:91
本文介绍了中止Tomcat初创公司的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体来说,当我的网络应用程序无法初始化时(由于配置错误),我希望Tomcat中止启动。

Specifically, I want Tomcat to abort start-up when my web app can't initialize (due to a configuration error).


  1. 我试图使用Tomcat关闭端口。当我的网络应用程序启动时,这还没有准备好。

  1. I'm tried to use the Tomcat shutdown port. That's not ready when my web app is starting up.

-Dorg.apache.catalina.startup.EXIT_ON_INIT_FAILURE = true,不起作用。 Tomcat启动就好了。

-Dorg.apache.catalina.startup.EXIT_ON_INIT_FAILURE=true, doesn't work. Tomcat starts up just fine.

抛出未被捕获的异常。我试过抛出异常。 Tomcat仍然正确初始化,但我的Web应用程序处于非功能状态。

Throw an uncaught Exception. I've tried throwing an exception. Tomcat still initializes correctly but my web app is in a non-functional state.

使用Tomcat JMX API。没有关闭方法会导致Tomcat关闭。

Use the Tomcat JMX API. None of the shutdown methods will cause Tomcat to shut down.

到目前为止,我唯一的选择是是 System.exit(-1)。这确实有效,但我担心对Tomcat这样做会导致问题。

So far, the only other option I've got is System.exit(-1). That does work, but I'm concerned that doing that to Tomcat will cause problems.

推荐答案

Tomcat旨在抵御编写错误的应用程序无法启动。

Tomcat is designed to be resilient against badly written applications that don't start up.

org.apache.catalina.startup.EXIT_ON_INIT_FAILURE用于停止Tomcat,如果其中一个Tomcat的组件,例如其中一个连接器,无法启动(例如因为端口正在使用中)。

org.apache.catalina.startup.EXIT_ON_INIT_FAILURE is for stopping Tomcat if one of Tomcat's components, such as one of the connectors, doesn't start (e.g. because the port is in use).

System.exit()很难看,但是它会起作用,就Tomcat而言它是安全的有关(至于那件事就是杀人-9)。应用程序可能不喜欢这种相当残酷的关闭方法。

System.exit() is ugly but it will work and it is safe as far as Tomcat is concerned (as for that matter is kill -9). It is applications that may not like this rather brutal approach to shutdown.

有一种特定于Tomcat的方法可以更干净地完成这项工作。将自定义LifecycleListener添加到服务器组件并让它响应AFTER_START_EVENT。让侦听器将Container层次结构从服务器导航到上下文[服务器 - >服务 - >引擎 - 主机 - >上下文],并检查每个上下文是否处于STARTED状态。如果没有,请在服务器上调用stop()然后destroy()。

There is a Tomcat specific way to do this more cleanly. Add a custom LifecycleListener to the Server component and have it respond to AFTER_START_EVENT. Have the listener navigate the Container hierarchy from the Server to the Contexts [Server->Service(s)->Engine-Host(s)->Context(s)] and check that each context is in the state STARTED. If not, call stop() and then destroy() on the Server.

这篇关于中止Tomcat初创公司的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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