在部署我的 war 文件时,我将配置从数据库加载到单例中。 如果数据库中缺少任何必需的配置变量,我希望应用程序停止。
在 Java 中,我会执行 System.exit(1),但如果我理解正确的话,这会杀死 JVM,即 glassfish。
如何终止该应用程序?
最佳答案
覆盖Servlet.init您的 Servlet 的方法:
@Override
public void init(ServletConfig config) throws ServletException {
super.init(ServletConfig);
//load config from database, and eventually launch an exception of type ServletException
// for example throw new RequiredConfigMissingException(...);
}
并启动 ServletException 类型的异常(最好扩展它),以防缺少所需的配置:
public class RequiredConfigMissingException extends ServletException
应用程序服务器不会加载您的 Web 应用程序并停止部署,并在应用程序服务器日志中写入错误。
关于java - 如果配置加载失败如何停止部署,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13392319/