3 Replies Latest reply on Feb 13, 2004 11:02 PM by 807582

    Monitoring a URL!

      Hey Guys

      How would you go about constantly and effeciently monitoring a url?

      Currently, I am doing it in the most logical and simple way that came to me.

      I have a timer that wakes up every second, makes a connection to a url using the URL and URLConnection class, checks the lastModified time for the file (index.html) in my case. If the
      current timestamp is different than the previous one, I know the file has been altered.

      I am curious to find out if there exists a better (in terms of efficiency) way to monitor a url.

      Any thoughts?


      private class PageLoader extends TimerTask {

      BufferedReader data;
      String url = "23.87.345.234/index.html";

      public PageLoader(){}

      public void run() {
      try {
      String line;
      url = new URL(string);
      uconn = url.openConnection();
      long newtimestamp = uconn.getLastModified();

      if(newtimestamp > oldtimestamp){
      oldtimestamp = newtimestamp;
      BufferedReader data = new BufferedReader(new InputStreamReader(url.openStream()) );
      StringBuffer buf = new StringBuffer();

           while ( ( line = data.readLine() ) != null ) {
           buf.append( line + "\n" );

           System.err.println("Nothing has changed.");

      }catch( Exception ioe ){
        • 1. Re: Monitoring a URL!

          You've two choices when checking something... polling or catching events.
          Since the webserver isn't going to send you an event to say it's changed, you've gone the only route possible. Check it yourself at periodic intervals.

          Checking the timestamp is the quickest, which means you do not have to parse the URL.
          If you want accuracy, to ensure the file has changed, even if the timestamp has not, you'd need to generate
          a checksum on the URL stream contents.

          But, I can't see any significant improvement on what you done...
          Are you just fishing for complements !

          • 2. Re: Monitoring a URL!

            Cheers for the reply Mate.

            :) I am certainly not looking for compliments; I would rather have someone improve my code after
            dissecting it.

            I do appreciate your time.

            • 3. Re: Monitoring a URL!
              The HTTP protocol allows for a conditional GET, which will retrieve the file only if it's newer than a certain timestamp. Using that, you won't have to download the entire page each time you poll the server.