How can i check if MySQL and Tomcat are running?

Mark picture Mark · Aug 13, 2010 · Viewed 8.1k times · Source

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.

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?

Thanks

Answer

BalusC picture BalusC · Aug 13, 2010

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!
}

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.