如何检查MySQL和Tomcat是否正在运行? [英] How can i check if MySQL and Tomcat are running?

查看:150
本文介绍了如何检查MySQL和Tomcat是否正在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个分为不同子组件的Java应用程序,每个子组件都在一个单独的Tomcat实例上运行。此外,一些组件通过Hibernate使用MySQL数据库。

I've created a Java application that is split in different subcomponents, each of those runs on a separate Tomcat instance. Also, some components use a MySQL db through Hibernate.

我现在正在创建一个管理控制台,它报告了我所有Tomcat实例和MySQL的状态。我不需要详细信息,但知道它们是否正在运行就足够了。

I'm now creating an administration console where it's reported the status of all my Tomcat instances and of MySQL. I don't need detailed information, but knowing if they are running or not it's enough.

这可能是最好的解决方案吗?

What could be the best solution to do that?

谢谢

推荐答案

最简单的方法就是连接服务器并查看是否成功。

Most straightforward way would be to just connect the server and see if it succeeds.

MySQL:

Connection connection = null;
try {
    connection = DriverManager.getConnection(url, username, password);
    // Succes!
} catch (SQLException e) {
    // Fail!
} finally {
    if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
}

Tomcat:

try {
    new URL(url).openConnection().connect();
    // Succes!
} catch (IOException e) {
    // Fail!
}

如果您想要更具体的状态,例如检查某个数据库表是否可用或特定的webapp资源是否可用,则必须分别触发更具体的 SELECT 语句或HTTP请求。

If you want a bit more specific status, e.g. checking if a certain DB table is available or a specific webapp resource is available, then you have to fire a more specific SELECT statement or HTTP request respectively.

这篇关于如何检查MySQL和Tomcat是否正在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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