0

I found we can set up retry count in azure function using below annotation

@FixedDelayRetry(maxRetryCount = 3, delayInterval = "00:00:05")

question is how do we get current retry count? I need to copy message to failed queue after 3 unsuccessful retry, could not find any way to do that.

Issue mentioned at : https://github.com/Azure/azure-webjobs-sdk/issues/2595 is closed.

I could not understand final resolution? do we have any property on ExecutionContext that can be used to get current execution count?

1 Answers1

0

By using the retry listener you can be able to get the retry count

Something like this

 @Retryable( value = SQLException.class, maxAttempts = 5, 
                backoff = @Backoff(delay = 100), listeners = {"retryListener"})
    void testMethod(String abc) throws SQLException{
    //some method body that could throw sql exception
    };

Below is the example code on printing the retry count on error

@Slf4j
@Component
class RetryListener extends RetryListenerSupport {

    @Override
    public <T, E extends Throwable> void close(RetryContext context,
                                               RetryCallback<T, E> callback, Throwable throwable) {

        log.error("Unable to recover from  Exception");
        log.error("Error ", throwable);
        super.close(context, callback, throwable);
    }

    @Override
    public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
        log.error("Exception Occurred, Retry Count {} ", context.getRetryCount());
        super.onError(context, callback, throwable);
    }

    @Override
    public <T, E extends Throwable> boolean open(RetryContext context,
                                                 RetryCallback<T, E> callback) {
        log.error("Exception Occurred, Retry Session Started ");
        return super.open(context, callback);
    }
}

Also,

how do we get current retry count?

To get the current retry count you need to provide @Retryable in Interceptor property

Below is the example code.

int retry = RetrySynchronizationManager.getContext().getRetryCount();

Check the SO1 and SO2 for related information.

SaiSakethGuduru
  • 2,218
  • 1
  • 5
  • 15