2012-11-15 42 views
0

在部署我的war文件時,我將配置從數據庫加載到單例中。 如果數據庫中缺少任何所需的配置變量,我希望應用程序暫停。如何在配置加載失敗時停止部署

在Java中,我會做System.exit(1),但如果我理解正確,這將殺死JVM,這是glassfish。

如何殺死應用程序?

回答

1

覆蓋你的servlet的Servlet.init方法:

@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 

的Application Server不會加載您的Web應用程序,並停止在應用程序服務器日誌中編寫錯誤的部署。

+0

不幸的是,它不是一個servlet,而是一個web服務。不確定是否有類似的東西? – likenoother

+1

我認爲他意味着你必須在啓動servlet中完成它。如果您沒有啓動servlet,請創建一個servlet並將其設置爲web.xml中的啓動servlet –

+0

您可以爲web環境發佈web.xml文件嗎? –

1

我設法通過拋出一個ExceptionInInitializerError來獲得所需的結果。 由於在@Startup單例的@PostConstruct方法內發生異常,它會停止部署。

我會查看web.xml文件。 感謝您的幫助。