tags:

views:

190

answers:

7

I have a method that periodically (e.g. once in every 10 secs) try to connect to a server and read some data from it. The server might not be available all the time. If the server is not available the method throws an exception.

What would be the best way to implement a wrapper method that doesn't throw an exception except if the server wasn't available for at least one minute?

+7  A: 

Keep track of when the last time you successfully reached the server was. If the server throws an exception, catch it and compare to the last time you reached the server. If that time is more than a minute, rethrow the exception.

Adam Rosenfield
Server doesn't actually throw an exception, it's the client code that throws ;) However, I still voted +1 for your answer.
Milan Babuškov
+1  A: 

You will have to record the time that you originally try to connect to the server and then catch the exception. if the time that the exception is caught is more than the original time + 1 minute, rethrow the exception. If not, retry.

brian
I done been sniped.
brian
+1  A: 

Ideally you can put a timeout on the call to the server. Failing that do a thread.sleep(600) in the catch block and try it again and fail if the second one doesn't return.

Turnkey
Timeout pops to my mind too.
marcospereira
+2  A: 

In psuedocode.

//Create Timer
//Start Timer
bool connected = false;

while (!connected)

    try {
        //Connect To DB

        connected = true;
    }
    catch (Exception ex) {
       if (more than 1 minute has passed)
          throw new Exception(ex);
    }

}
Orion Adrian
A: 

You could have a retry count, and if the desired count (6 in your case) had been met then throw an exception

int count = 0;

CheckServer(count);

public void CheckServer(count) {
try
{
  // connect to server
}
catch(Exception e)
{
 if(count < MAX_ATTEMPTS) {
    // wait 10 seconds
    CheckServer(count++)
  }
  else {
    throw e;
  }
}
}
Xian
A: 

You can set a boolean variable for whether or not the server connection has succeeded, and check it in your exception handler, like so:

class ServerTester : public Object
{
  private bool failing;
  private ServerConnection serverConnection;
  private Time firstFailure;

  public ServerTester(): failing(false)
  {
  }

  public void TestServer() throws ServerException
  {
    try
    {
       serverConnection.Connect();
       failing = false;
    }
    catch (ServerException e)
    {
      if (failing)
      {
        if (Time::GetTime() - firstFailure > 60)
        {
          failing = false;
          throw e;
        }
      }
      else
      {
        firstFailure = Time::GetTime();
        failing = true;
      }
    }
  }
}

I don't know what the actual time APIs are, since it's been a while since I last used Java. This will do what you ask, but something about it doesn't seem right. Polling for exceptions strikes me as a bit backwards, but since you're dealing with a server, I can't think of any other way off the top of my head.

Mark Verrey
A: 

Remember that exception handling is just a very specialized use of the usual "return" system. (For more technical details, read up on "monads".) If the exceptional situation you want to signal does not fit naturally into Java's exception handling system, it may not be appropriate to use exceptions.

You can keep track of error conditions the usual way: Keep a state variable, update it as needed with success/failure info, and respond appropriately as the state changes.

phyzome