All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bazaarvoice.ostrich.retry.SleepingRetry Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version
package com.bazaarvoice.ostrich.retry;

import com.bazaarvoice.ostrich.RetryPolicy;

import static com.google.common.base.Preconditions.checkArgument;

public abstract class SleepingRetry implements RetryPolicy {
    private final int _maxNumAttempts;

    protected SleepingRetry(int maxNumAttempts) {
        checkArgument(maxNumAttempts >= 0);
        _maxNumAttempts = maxNumAttempts;
    }

    @Override
    public boolean allowRetry(int numAttempts, long elapsedTimeMs) {
        checkArgument(numAttempts >= 1);
        if (numAttempts >= _maxNumAttempts) {
            return false;
        }

        try {
            Thread.sleep(getSleepTimeMs(numAttempts, elapsedTimeMs));
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return false;
        }

        return true;
    }

    protected abstract long getSleepTimeMs(int numAttempts, long elapsedTimeMs);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy