0

I have to do the following:

1.- Read some rows from DB (could be more than 1).
2.- Fill an ArrayList with the data.
3.- Invoke WebService with the data one row at a time.
4.- If there is a problem on the transmission of any row, try 3 more times.

The problem is that I am using a For Each Loop to set the data and I don't know If I can repeat an iteration on that loop.

I was thinking about using the regular For Loop as explained in: How does the Java 'for each' loop work? But i have 2 issues with that:
How can I get the right way to get the "length" in my case?
Where I have to use the brackets [i] in my case?

Should I rearrange the code?

This is a brief of my code:

ArrayList<SomeBean> v = new ArrayList<SomeBean>();

while(index < 1000) 
{   
    //Read some data from DB

    //Fill the arraylist

    for(SomeBean s : v)
    {
        if (repeat == 0)
        {
            //Use the Data of the current iteration to invoke Webservice
            String column1 = s.getColumn1();
            String column2 = s.getColumn2();
            String column3 = s.getColumn3();
            ...
        }
        else
        {
            //Don´t use the Data of current iteration, try again with the same data
        }

        try 
        {
            //Invoke WebService and send the data

            if (answer == OK)
            {
                //Print OK
            }
            else
            {
                //Print NOT OK
            }
        }                       
        catch (Exception) 
        {
            //Print the Exception
            repeat++;

           if (repeat > 3)
           {
            repeat = 0; //Go to read a new group of data
            index++;
           }                   
        }
    }
}
Community
  • 1
  • 1
Rodrick
  • 595
  • 10
  • 27
  • 3
    You have to have another loop within the for-each loop for the repeat. A better way would be to have a separate method which calls the webservice, and retries `n` times on error. Call that method once from the parent for-each loop. This way if you want to change the retry mechanism etc you can do that in the separate method. – Nivas Feb 10 '16 at 18:13
  • Thanks @Nivas, I put another loop leaving inside the try and catch. Is not elegant but solves the issue. – Rodrick Feb 10 '16 at 19:21

1 Answers1

0

This is a perfect task for recursion. The idea here is that if an error occurs in the logic and the number of attempts exceeds the predetermined limit, it fails. Otherwise, it tries again by recursively calling itself.

// Our class variables
private int attempts = 0, limit = 3;

// A method to call our recursive method for the first time.
void BeginProcessing()
{
     ProcessData();
}

// Our recursive method containing our read logic.
void ProcessData()
{
    try
    {
        /* Insert read logic here. */
    }
    catch(Exception)
    {
        attempts++;

        if (attempts<=limit)
        {
            ProcessData();
        }
        else
        {
            System.out.println("Attempted to process " + limit + " times.");        
        }
    }
}
  • Very nice way to doing it but I have to do too much rearrange of my code. Thanks. – Rodrick Feb 10 '16 at 19:22
  • If you do not care about efficiency, the person who commented on your question works - adding another loop. However, if you have the extra time, I would highly recommend attempting to implement this recursive solution, as its big O is O(n), three times more efficient than your current O(n^3) triple-nested loop solution. While I can completely respect your decision to not mark this as the "correct" answer since you are not going to use it at this time, it may be wise to mark it as such for future developers attempting to Google a similar solution. Best regards, Omnus Ruthius – Omnus_Ruthius Feb 10 '16 at 19:52
  • No problem! Best of luck. – Omnus_Ruthius Feb 10 '16 at 19:58