4 Replies Latest reply: Aug 1, 2012 1:11 PM by aksarben RSS

    method execution retry after a time interval

    948734
      Class A {

      public void doSomething() {
      // Step 1: Initialize variables
      // Step 2: ... do something else
      // Step 3: call method in the same class which creates connections to external resources.
      // e.g. Step 3 execution time is unknown... Step 3 tries to create JMS connection.
      // In some situations step 3 might take more time to create JMS connections.
      // Need to stop creating connections if it exceeds certain time and retry step 3 for couple of times and exit the application
      }

      }

      public class CreateJmsService implements Runnable{
           
           public void createConsumer() {
                System.out.println("Creating consumer");
                try {
                     Thread.sleep(2000);
                } catch (InterruptedException e) {               
                     e.printStackTrace();
                }
                System.out.println("Created consumer");          
           }
           @Override
           public void run() {
                
                System.out.println("Starting JMS configuration.....");
                // Timer start here
                createConsumer(); // stop executing this method if it takes more than 2000 milliseconds and retry for another 2 times
                
                // close/stop timer          
                
           }
      }

      Edited by: 945731 on Jul 30, 2012 10:13 AM
        • 1. Re: method execution retry after a time interval
          EJP
          And your question is?
          • 2. Re: method execution retry after a time interval
            948734
            How do I stop executing the current thread and kill the processing thread and create a new thread to retry for certain number of times...
            • 3. Re: method execution retry after a time interval
              gimbal2
              945731 wrote:
              How do I stop executing the current thread and kill the processing thread and create a new thread to retry for certain number of times...
              You design the code, write it and test it. Standard developer tasks. If you can't do it and cannot do the research required to figure out how, there is something horribly wrong. If you're a novice there should be a senior you can turn to for guidance.

              Stop executing a thread: you interrupt threads to do that (with the interrupt() method). That does not magically work, thread code needs to be designed to be interruptable. Research on your part, a good book on Threads will surely help.
              Create new thread: well you already know how to do that
              Certain number of times: maintain an integer that keeps track of how many times you interrupted and restarted the thread

              With those three pieces of the puzzle you really must be able to put it all together yourself.
              • 4. Re: method execution retry after a time interval
                aksarben
                We do a similar thing in our database classes. Inside a loop, we execute a statement, and if it succeeds, we exit the loop. Else, we examine the error to see if it's a type we have determined (after years of painful experience) to be "transient." We define "transient" errors as those that may go way on their own (i.e., record locked, resource busy, etc.). If it's a transient error, and we haven't reached our (app-configurable) retry limit, we wait a specified length of time (say, a minute or two or five), then retry the statement.

                Pretty simple logic, but it works wonders in our nighttime batch processing where many apps are competing for the database's attention.