title: java implementation : polling a web service
body: <p>Please find below my requirement.</p>
  
  <p>Requirement : Poll a web service. Two crucial parameters of polling max_timeout, polling_interval are configured in properties file. Overall objective is to spend for a time of  overall in obtaining the response. If we get the response with in max_timeout, we can return the response to client. Otherwise we will throw an error saying that the operation is unsuccessful.</p>
  
  <p>Below is the code snippet I have written.</p>
  
  <pre><code>int maxTimeOut = 10;
  int interval   = 2;
  
  int iterations = maxTimeOut/interval;
  boolean success = false;
  
  for (int i = 0; i &lt; iterations; i++)
  {
      System.out.println("Number of iteration = " + i);
      try
      {
          Thread.sleep(interval * 1000);
          System.out.println("Waited for " + interval + " seconds");
  
          success =  getWSResponse(i);
          System.out.println("CALL" + ((success) ? "SUCCESSFUL" : "FAIL"));
  
          if(success) break;
  
      }catch (InterruptedException ie)
      {
          System.out.println(ie.getMessage());
      }
  }
  
  //Send the success flag to client
  </code></pre>
  
  <p>Could you correct me if this is the proper implementation of polling. I am little bit concerned that this code assumes that webservice call returns in no time. If this takes 2-3 seconds (usually it does ), then we would be spending more than max_timeout overall for POLLING alone. How could we fix this. Is there any better approach than this.</p>