我有两个服务器:
如何在MySQL服务器未准备就绪时启动tomcat时保持健壮性? 实际上,我在电源故障期间遇到了麻烦,并且两个服务同时启动,最终tomcat失败了。
管理这种不同主机上的服务之间的依存关系的好方法是什么?在Unix中有一种本机的方法吗?
答案 0 :(得分:1)
您的问题已经存在答案。 链接:
基本上,您需要检查所需端口上的mysql是否已回答。 因此,您可以使用以下结构修改tomcat systemd单位文件:
@ManagedBean(name = "myBean")
@ViewScoped
public class MyBean implements Serializable {
private static final String REPORT_IDENTIFIER = "report_id";
@PostConstruct
public void init(){
//Initialize menu model
//...
//Add two menu items for downloading different reports
final DefaultMenuItem downloadItem1 = new DefaultMenuItem("Download Report 1");
downloadItem1.addCommand("#{myBean.callDownloadReport}");
downloadItem1.setAjax(false);
downloadItem1.setParam(REPORT_IDENTIFIER, "1");
model.addElement(downloadItem1);
final DefaultMenuItem downloadItem2 = new DefaultMenuItem("Download Report 2");
downloadItem2.addCommand("#{myBean.callDownloadReport}");
downloadItem2.setAjax(false);
downloadItem2.setParam(REPORT_IDENTIFIER, "2");
model.addElement(downloadItem2);
}
public void callDownloadReport(MenuActionEvent menuActionEvent){
//Get the report identifier
final String reportId = menuActionEvent.getMenuItem()
.getParams()
.get(REPORT_IDENTIFIER)
.get(0);
//Create new action event
final ActionEvent actionEvent = new ActionEvent(menuActionEvent.getComponent());
//Create the value expression with the report ID for the download listener
//-> is executed when calling "processAction"!
final FacesContext context = FacesContext.getCurrentInstance();
final String exprStr = "#{myBean.downloadReport('" + reportId + "')}";
final ValueExpression valueExpr = context.getApplication()
.getExpressionFactory()
.createValueExpression(context.getELContext(), exprStr, StreamedContent.class);
//Instantiate the download listener and indirectly call "downloadReport(String)"
new FileDownloadActionListener(valueExpr, null, null)
.processAction(actionEvent);
}
/**
* Indirectly called by {@link FileDownloadActionListener#processAction(ActionEvent)}.
*
* @param reportId the identifier of the report
*/
public StreamedContent downloadReport(final String reportId){
//Create report with specified ID
//...
}
}
这将在具有systemd的主机上工作。
通常,您将需要创建简单的脚本,尝试连接到远程数据库,如果成功,则返回退出代码0