Abhishek
Abhishek

Reputation: 21

How to configure Delay For First retry in Spring Retry Framework

Im working on an application where im using spring @Retryable to perform retry operation for 3 times (By default retry count is 3). Below is my sample code

@Retryable(
        value= CustomException.class,
        backoff = @Backoff(
                delay = 5000,
                multiplier = 2,
                maxDelay = 20000
        )
) 
public void performRetry() throws CustomException {
    try {

        if(RetrySynchronizationManager.getContext().getRetryCount()==0) { // retry count starts from 0.
            logger.info("Starting Retry attempts. Thread name {}. Giving delay of 3 seconds", Thread.currentThread().getName());
            Thread.sleep(3000);
        }

        boolean response = service.execute();  // calling main service

        // Rest of the code

    } catch(Exception e) {
        logger.error("Retry Count {} Error occurred {}", RetrySynchronizationManager.getContext().getRetryCount(), e.getMessage());
        throw new CustomException(e.getMessage());
    }
}

My query is that i want to introduce a delay of 3 seconds before doing first attempt (not first retry) i.e. calling my main service service.execute(); for the first time.

Since in spring retry it immediately triggers the first attempt without any delay. So in my code before im calling my main service service.execute(); for the first time im using Thread.sleep(3000);

Im using RetrySynchronizationManager.getContext().getRetryCount() to get retry count. So if its 0 meaning first attempt, 1 means second attempt (first retry call) and 2 means third attempt (second retry call).

Is there any other way that i can give delay apart from using thread.sleep()?

Is there any way i can specify delay for the first retry in @Retryable annotation?

Waiting for an early response.

Upvotes: 2

Views: 1887

Answers (1)

Gary Russell
Gary Russell

Reputation: 174564

My query is that i want to introduce a delay of 3 seconds before doing first retry i.e. calling my main service service.execute(); for the first time.

The first retry is the second call.

There is no way to configure a delay for the first call; you would have to do that in your calling code.

Upvotes: 0

Related Questions