Recursive calls Functions - Yash-777/LearnJava GitHub Wiki

public class ServerRetryReq {
    public static void main(String[] args) {
        try {
            String res = new ServerRetryReq().callServer();
            System.out.println("\nRes:"+res);
        } catch (Exception e) {
            System.out.println("==================="+e.getMessage());
            e.printStackTrace();
        }
    }
    int retry = 0, retryFlow = -1;
    StringBuffer bufferUUIDResponses = new StringBuffer();
    Exception exception = null;
    String finalStackResponseStr = "";
    
    public static int getRandomVal(int low, int high) {
        java.util.Random r = new java.util.Random();
        int result = r.nextInt(high-low) + low;
        return result;
    }
    private String callServer() throws Exception {
        String responseStr = "";
        boolean failedReq = false;
        String UUID_retry = "";
        try {
            UUID_retry = java.util.UUID.randomUUID().toString();
            retry = 5;
            System.out.println("retry"+retry+", retryFlow:"+retryFlow+", UUID_retry:"+UUID_retry);
            int breakPoint =  getRandomVal(1, 10);
            System.out.println("breakPoint:"+breakPoint);
            if (retry != 0 && retryFlow != breakPoint) {
                int res = 10/0;  // Something like Network error/Connection timeout some status code which are listed to go for retry.
            }
            responseStr = "Pass";
        } catch (Exception e) {
            failedReq = true;
            responseStr = "Fail";
            //throw new Exception(e.getMessage()); // If we throw here then final result may impact
            exception = new Exception(e.getMessage() + ", UUID:"+UUID_retry);
        } finally {
            
            if (failedReq) { // failedReq = true;
                if (retryFlow < 0 && retry >= 0) {
                    retryFlow = retry;
                    
                    bufferUUIDResponses.append("----- Main Request");
                    bufferUUIDResponses.append("\nF UUID:" +" | "+ UUID_retry);
                    
                    if (retryFlow >= 1) {
                        retryFlow = retryFlow - 1;
                        bufferUUIDResponses.append("\n\n===== Retry Requests");
                        callServer();
                    }
                } else if (retryFlow >= 1) {
                    bufferUUIDResponses.append("\nF UUID:" +" | "+ UUID_retry);
                    retryFlow = retryFlow - 1;
                    callServer();
                } else if (retryFlow == 0) {
                    bufferUUIDResponses.append("\nF UUID:" +" | "+ UUID_retry);
                    System.out.println("All Fail Email Retry for loop:\n"+ bufferUUIDResponses );
                    
                    finalStackResponseStr = responseStr + ", UUID:"+UUID_retry+", Exception:"+exception.getMessage();
                    System.out.println("Final Fail :"+ finalStackResponseStr );
                    //throw exception; // If you wish you can throw final stack exception
                    return finalStackResponseStr;
                }
            } else {
                if (bufferUUIDResponses.length() > 0) {
                    bufferUUIDResponses.append("\nS UUID:" +" | "+ UUID_retry);
                    System.out.println("Final Pass Email Retry Loop:\n"+ bufferUUIDResponses );
                    
                    finalStackResponseStr = responseStr + ", UUID:"+UUID_retry;
                    System.out.println("Final Pass :"+ finalStackResponseStr );
                    return finalStackResponseStr;
                }
            }
        } // Finally Close
        System.out.println("Final Return :"+ responseStr + ", UUID:"+UUID_retry);
        if (finalStackResponseStr == null || finalStackResponseStr.isEmpty()) {
            finalStackResponseStr = responseStr + ", UUID:"+UUID_retry;
        }
        finalStackResponseStr = finalStackResponseStr.replaceAll(" ", "~");
        return finalStackResponseStr;
    }
}

output:

All Fail Email Retry for loop:
----- Main Request
F UUID: | a0bfa273-b5f8-4e53-9d2a-930d9ef44407

===== Retry Requests
F UUID: | 9b0f420d-cc05-4aea-9dd8-e889ab60b249
F UUID: | 97ce86c4-a0bb-4ede-a874-86ed3ad41c15
F UUID: | 61f246d3-5eb6-4919-a860-adfa354d243a
F UUID: | 9d9525ab-fd70-4975-b89c-b118b103cd40
F UUID: | 8ac308d8-5fe5-40b5-ad1d-82ca6f611206
Final Fail :Fail, UUID:8ac308d8-5fe5-40b5-ad1d-82ca6f611206, Exception:/ by zero, UUID:8ac308d8-5fe5-40b5-ad1d-82ca6f611206
Final Return :Fail, UUID:9d9525ab-fd70-4975-b89c-b118b103cd40
Final Return :Fail, UUID:61f246d3-5eb6-4919-a860-adfa354d243a
Final Return :Fail, UUID:97ce86c4-a0bb-4ede-a874-86ed3ad41c15
Final Return :Fail, UUID:9b0f420d-cc05-4aea-9dd8-e889ab60b249
Final Return :Fail, UUID:a0bfa273-b5f8-4e53-9d2a-930d9ef44407

Res:Fail,~UUID:8ac308d8-5fe5-40b5-ad1d-82ca6f611206,~Exception:/~by~zero,~UUID:8ac308d8-5fe5-40b5-ad1d-82ca6f611206