Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i check if MySQL and Tomcat are running?

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

like image 784
Mark Avatar asked Aug 12 '10 23:08

Mark


2 Answers

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.

like image 188
BalusC Avatar answered Oct 02 '22 01:10

BalusC


I assume that you know the ports of which are running in advance (or from configuration files). The easiest way to check is to make socket connections to those ports like a telnet program does. Something like:

public boolean isServerUp(int port) {
    boolean isUp = false;
    try {
        Socket socket = new Socket("127.0.0.1", port);
        // Server is up
        isUp = true;
        socket.close();
    }
    catch (IOException e)
    {
        // Server is down
    }
    return isUp;
}

Usage:

isTomcatUp = isServerUp(8080);
isMysqlUp = isServerUp(3306);

However, I would say that is a false-negative check.. Sometimes it says server UP but the server is stuck or not responding...

like image 32
instcode Avatar answered Oct 01 '22 23:10

instcode