Member Avatar for feoperro

Hi,

I'm trying to create a program where I can monitor when a server running a Tomcat Application Server goes down. I will need to check an IP & PORT every 5 minutes for example and get back a boolean on whether it is up or not. I've tried googling around already, and I didn't really found what I'm looking for, hopefully someone here can help.

Thanks a lot,
-Ashton.

Simply connect and catch the exception. On an exception, it is, seemingly, not running, or not running properly. Use HttpURLConnection and connect to the actual site and you can even check the returned http codes (i.e. 404, 501, etc).

Member Avatar for feoperro

Hi masijade,

Thank you for your suggestion. Below I have done a piece of code, that works, until you try and run 2 connections simultaneously.

Valid URL:

public String GoogleServer() {
        String result = "";
        try {
            URL googleURL = new URL("http://www.google.com");
            HttpURLConnection googleResponse = (HttpURLConnection) googleURL.openConnection();
            result = "" + googleResponse.getResponseCode();
        } catch (IOException ioe) {
            System.out.println("IOE Exception: " + ioe);
        }
        return result;
    }

Invalid URL:

public String badServer() {
        String result = "";
        try {
            URL badURL = new URL("http://www.gsaadv100.com/");
            HttpURLConnection badResponse = (HttpURLConnection) badURL.openConnection();
            result = "" + badResponse.getResponseCode();
        } catch (IOException ioe) {
            System.out.println("IOE Exception: " + ioe);
        }
        return result;
    }

Now if I call them on a JSP/Servlet for example like this:

<% 
        String googleStatus = methodShelf.GoogleServer();
        String badServerStatus = methodShelf.badServer();
        out.println(googleStatus);
        out.println(badServerStatus);
%>

Then the page just hangs... Could you possibly tell me why?

Thanks,
-Ashton.

This is not the sort of thing you do from a JSP page itself. This would be something that should (if part of a webservice) be started in a thread (or a few threads) when the application starts and left running. They should then perform a pooling operation (i.e. every few minutes attempt to connect) and write the statuses in a DB (or flat file) and the JSP should do nothing but show the results.

Member Avatar for feoperro

Hi again masijade,

Thanks for the advice. I managed to find a cool little timeout facility, I tried this code and it worked:

public String testServer() {
        String result = "";
        try {
            URL testURL = new URL("http://www.google.com");
            HttpURLConnection testResponse = (HttpURLConnection) testURL.openConnection();
            testResponse.setReadTimeout(3000);
            result = "" + testResponse.getResponseCode();
            testResponse.disconnect();
        } catch (IOException ioe) {
            System.out.println("IOE Exception: " + ioe);
        }
        return result;
    }

Do you still think this is a bad route to take?

Thanks,
-Ashton.

Scriptlets alone are a bad idea in a JSP. If you insist on doing this "on the spot", then at least make a bean out of it.

Member Avatar for feoperro

lol ya, I am calling it from a bean, just used the scriptlet as an example... :) thanks a lot masijade! your advice was very helpful!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.