How to set timeout in a method in Java and retry method for a periodic amount of time

Ioanna Katsanou :

I java a java method, which makes a connection to a web service. Sometimes this method takes too long to make the connection. I want for example it it takes longer than 5 seconds, then to stop the current procedure and restart all over for 3 more times. If all times fail, then abort completely.

I have written the following until now:

        private ConnectionInterface  connectWithTimeout() throws MalformedURLException, Exception {

        ExecutorService executor = Executors.newCachedThreadPool();
        Callable<Object> task = new Callable<Object>() {
            public Object call() throws InterruptedException, MalformedURLException, Exception {
                return connectWithNoTimeout();  //This is the method that takes to long. If this method takes more than 5 seconds, I want to cancel and retry for 3 more times. Then abort completely.
            }
        };
        Future<Object> future = executor.submit(task);
        try {
            Object result = future.get(5, TimeUnit.SECONDS);
        } catch (TimeoutException ex) {

            System.out.println( "Timeout Occured");

        } catch (InterruptedException e) {
          System.out.println( " "InterruptedException Occured");


        } catch (ExecutionException e) {
            System.out.println( ""ExecutionException Occured");


        } finally {

            future.cancel(true); // here the method gets canceled. How do I retry it?
        }
        System.out.println( "Connected !!");
        return connectWithNoTimeout();
}



private ConnectionInterface  connectWithNoTimeout() throws MalformedURLException, Exception {}
Amongalen :

Your method already has a 5 seconds timeout. All you need to do now is to add some kind a loop with 3 repeats. You need a counter of timeouts and a break after successful attempt. Not sure what you want to do when other exceptions happen, added breaks there as well. Following code should do the job:

private ConnectionInterface  connectWithTimeout() throws MalformedURLException, Exception {
        int repeatCount = 0;

        ExecutorService executor = Executors.newCachedThreadPool();
        Callable<Object> task = new Callable<Object>() {
            public Object call() throws InterruptedException, MalformedURLException, Exception {
                return connectWithNoTimeout();  //This is the method that takes to long. If this method takes more than 5 seconds, I want to cancel and retry for 3 more times. Then abort completely.
            }
        };

        while (repeatCount < 3){
          Future<Object> future = executor.submit(task);
          try {
              Object result = future.get(5, TimeUnit.SECONDS);
              break;

          } catch (TimeoutException ex) {
            repeatCount++;
            System.out.println( "Timeout Occured");

          } catch (InterruptedException e) {
            System.out.println( " "InterruptedException Occured");
            break; 

          } catch (ExecutionException e) {
              System.out.println( "ExecutionException Occured");
            break;    

          } finally {

              future.cancel(true); // here the method gets canceled. How do I retry it?
          }
        }
        System.out.println( "Connected !!");
        return connectWithNoTimeout();
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=134053&siteId=1