The attachment must have been stripped by something ...

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


/**
 * @author Hannes Schmidt.
 */

public class UrlWatchdog {

    private static int sleepSeconds = 60;
    private final static String watchedURL = "http://put.your.url/here";;
    private final static String expectedContent = "your text too look for in
the response";
    private final static String restartCommand = "your restart command";


    public static void main( String[] args ) {
        try {
            URL url = new URL( watchedURL );
            for(;;) {
                boolean success = false;
                try {
                    URLConnection con = url.openConnection();
                    con.setUseCaches( false );
                    try {
                        con.connect();
                        if( parseResponse( con ) ) {
                            success = true;
                        }
                    } catch( IOException e ) {
                        e.printStackTrace();
                    }
                } catch( IOException e ) {
                    e.printStackTrace();
                }
                if( ! success ) {
                    restart();
                }

                try {
                    Thread.sleep( sleepSeconds * 1000 );
                } catch( InterruptedException e ) {
                    e.printStackTrace();
                }
            }
        } catch( MalformedURLException e ) {
            e.printStackTrace();
        }
    }


    private static boolean parseResponse( URLConnection con ) {
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        boolean success = false;
        try {
            try {
                is = con.getInputStream();
                isr = new InputStreamReader( is );
                br = new BufferedReader( isr );
                String line;
                while( null != ( line = br.readLine() ) ) {
                    if( -1 != line.indexOf( expectedContent ) ) {
                        success = true;
                    }
                }
            } finally {
                if( br != null ) br.close();
                if( isr != null ) isr.close();
                if( is != null ) is.close();
            }
        } catch( Exception e ) {
            e.printStackTrace();
        }
        return success;
    }


    private static void restart() {
        try {
            Runtime.getRuntime().exec( restartCommand );
        } catch( IOException e ) {
            e.printStackTrace();
        }
    }
}

----- Original Message -----
From: "Ron Day" <[EMAIL PROTECTED]>
To: "Tomcat Users List" <[EMAIL PROTECTED]>
Sent: Wednesday, February 26, 2003 3:59 AM
Subject: RE: crontab problems


> No Code attached ???????
>
> -----Original Message-----
> From: Hannes Schmidt [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, February 25, 2003 8:02 PM
> To: Tomcat Users List
> Subject: Re: crontab problems
>
>
> I attached a version of my sample code that actually works (JDK used
1.4.1).
>
> Regarding your problem: I don't understand why bouncing Tomcat would
resolve
> a DNS problem. The UnknownHostException is a indication that something is
> wrong with DNS or the resolver library.
>
> ----- Original Message -----
> From: "Ron Day" <[EMAIL PROTECTED]>
> To: "Tomcat Users List" <[EMAIL PROTECTED]>
> Sent: Wednesday, February 26, 2003 1:37 AM
> Subject: RE: crontab problems
>
>
> > I have code very similar to this that does work............... except
> > whenever I get an unknowHostException, it seems to be cached somewhere
> until
> > I bounce Tomcat. My code actually pulses another website (in a thread
> every
> > 3 minutes)that is on the network, rather than check Tomcat. In place of
> your
> > streams, I actually set the request method to "HEAD", and interrogate
the
> > Response Code. This is easy to do in HTTPURLConnection.
> >
> > I instantiate the URL outside the loop (and hence only once), and I am
> > wondering if this is the source of my problem. I plan to test it ASAP.
> >
> > ron
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to