2

I am learning Spring Retry and have the following question on its capability:

I have a function which connects to a third party application API. It is anticipated that this can throw different exceptions like Timeout or IO exceptions. I was trying to see if spring's retry template will be able to handle such nested exceptions on "each" occurence. For example, when we set the maxAttempts to 3 for individual exceptions, then it should attempt 3 times at each occurence even if it is within a retry. for example, on attempt 1 - Exception 1 occured and next attempt (attempt 2) it was successful, but now exception 2 happens, based on the retryPolicy it should initiate attempt 1 for Exception 2.

Is this possible?

Based on a small POC, I see that it was trying only 3 times even when both types were thrown:

Controller Class with retryTemplate:

         retryTemplate.execute(new RetryCallback<Customer, Exception>(){
             @Override
                public Customer doWithRetry(RetryContext arg0) throws Exception{
                System.out.println("count #"+arg0.getRetryCount());
                if(arg0.getRetryCount()>0) {
                    System.out.println("throwable getClass Canonical Name "+arg0.getLastThrowable().getClass().getCanonicalName());
                }

                return  customerService.getCustomerDetails(choice);
             }});

Exception Simulation class:

Random r = new Random();

            int i = r.nextInt();
            i+=1;

            System.out.println("value of i "+i);
            if(i%2==0) {
                throw new Exception1();
            }
            else {
                throw new Exception2();
            }

Here are the logs:

count #0
in getCustomerDetails
value of i -1050226395
count #1
throwable getClass Canonical Name Exception1
in getCustomerDetails
value of i 824190506
count #2
throwable getClass Canonical Name Exception2
in getCustomerDetails
value of i 1210506150
Exception1

Expected result: Attempt for 3 times for each occurence of the exception

Actual Result: It is attempting only for total of 3 .

Below is my RetryPolicy: ExceptionClassifierRetryPolicy has the policyMap for both type of exceptions

public RetryTemplate retryTemplate() {

    RetryTemplate retryTemplate = new RetryTemplate();
    final SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();
    simpleRetryPolicy.setMaxAttempts(3);
    FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
    backOffPolicy.setBackOffPeriod(1500);
    final Map<Class<? extends Throwable>, RetryPolicy> policyMap = new HashMap<>();
    policyMap.put(Exception1.class, simpleRetryPolicy);
    policyMap.put(Exception2.class, simpleRetryPolicy);
    final ExceptionClassifierRetryPolicy retryPolicy = new ExceptionClassifierRetryPolicy();
    retryPolicy.setPolicyMap(policyMap);
    retryTemplate.setBackOffPolicy(backOffPolicy);
    retryTemplate.setRetryPolicy(retryPolicy);
    return retryTemplate;
}

Questions:

  1. Can Spring retry be used for this scenario?
  2. If so, what is required to be changed in the above implementation?

Please guide.

1 Answer 1

2

You can use ExceptionClassifierRetryPolicy, but be sure to assign different instances of SimpleRetryPolicy for each exception type:

ExceptionClassifierRetryPolicy retryPolicy = new ExceptionClassifierRetryPolicy();
Map<Class<? extends Throwable>, RetryPolicy> policyMap = new HashMap<>();

policyMap.put(Exception1.class, new SimpleRetryPolicy(3));
policyMap.put(Exception2.class, new SimpleRetryPolicy(3));

retryPolicy.setPolicyMap(policyMap);
retryTemplate.setRetryPolicy(retryPolicy);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.