Ioanna Katsanou
Ioanna Katsanou

Reputation: 504

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

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 {}

Upvotes: 4

Views: 5132

Answers (2)

Emanuel Graf
Emanuel Graf

Reputation: 766

First of all, I'd put the execution of that long command into a new thread so it will not block the Main Thread with the UI etc.

an approach:

Thread thr = new Thread() {
            public void run() {
                boolean error =false;
                boolean success=false;
                int time =0;

                try {
                while(tries<3&&!success){
                    //HERE GOES YOUR METHOD (connectWithNoTimeout(); ?)! Make sure to make the boolean "Success" = true if the connection is established
                    while (!error&&time<3) {
                        time++;
                        Thread.sleep(1000);
                       }
 }

 } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }


                }
        };

It's mostly written by hand, you need to make changes, copy & paste will not work

Upvotes: 2

Amongalen
Amongalen

Reputation: 3131

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();
    }

Upvotes: 3

Related Questions