Class RetryPolicy
- java.lang.Object
-
- com.microsoft.azure.servicebus.primitives.RetryPolicy
-
- Direct Known Subclasses:
RetryExponential
public abstract class RetryPolicy extends Object
Represents an abstraction of a policy for retrying messaging operations when an exception is encountered. Some exceptions encountered by a sender or receiver can be transient like ServerBusy and the operation will succeed if retried. Clients can specify a retry policy usingConnectionStringBuilder
which guides senders and receivers to automatically retry the failed operation before throwing the exception to the client application. Users should not implement this class, instead should use one of the provided implementations throughgetDefault()
orgetNoRetry()
.- Since:
- 1.0
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
RetryPolicy(String name)
Creates an instance of RetryPolicy with the given name.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static RetryPolicy
getDefault()
Retry policy that provides exponentially increasing retry intervals with each successive failure.Duration
getNextRetryInterval(String clientId, Exception lastException, Duration remainingTime)
Gets the interval after which nextRetry should be attempted, based on the last exception encountered and the remaining time before the operation times out.static RetryPolicy
getNoRetry()
Gets a retry policy that doesn't retry any operations, effectively disabling retries.protected int
getRetryCount(String clientId)
void
incrementRetryCount(String clientId)
Increments the number of successive retry attempts made by a client.static boolean
isRetryableException(Exception exception)
Determines if an exception is retry-able or not.protected abstract Duration
onGetNextRetryInterval(String clientId, Exception lastException, Duration remainingTime, int baseWaitTime)
Adjusts the interval after which nextRetry should be attempted, based on the last exception encountered, the remaining time before the operation times out and the minimum wait time before retry.void
resetRetryCount(String clientId)
Resets the number of retry attempts made by a client.String
toString()
-
-
-
Constructor Detail
-
RetryPolicy
protected RetryPolicy(String name)
Creates an instance of RetryPolicy with the given name.- Parameters:
name
- name of the policy
-
-
Method Detail
-
incrementRetryCount
public void incrementRetryCount(String clientId)
Increments the number of successive retry attempts made by a client.- Parameters:
clientId
- id of the client retrying a failed operation
-
resetRetryCount
public void resetRetryCount(String clientId)
Resets the number of retry attempts made by a client. This method is called by the client when retried operation succeeds.- Parameters:
clientId
- id of the client that just retried a failed operation and succeeded.
-
isRetryableException
public static boolean isRetryableException(Exception exception)
Determines if an exception is retry-able or not. Only transient exceptions should be retried.- Parameters:
exception
- exception encountered by an operation, to be determined if it is retry-able.- Returns:
- true if the exception is retry-able (like ServerBusy or other transient exception), else returns false
-
getDefault
public static RetryPolicy getDefault()
Retry policy that provides exponentially increasing retry intervals with each successive failure. This policy is suitable for use by use most client applications and is also the default policy if no retry policy is specified.- Returns:
- a retry policy that provides exponentially increasing retry intervals
-
getNoRetry
public static RetryPolicy getNoRetry()
Gets a retry policy that doesn't retry any operations, effectively disabling retries. Clients can use this retry policy in case they do not want any operation automatically retried.- Returns:
- a retry policy that doesn't retry any operations
-
getRetryCount
protected int getRetryCount(String clientId)
-
getNextRetryInterval
public Duration getNextRetryInterval(String clientId, Exception lastException, Duration remainingTime)
Gets the interval after which nextRetry should be attempted, based on the last exception encountered and the remaining time before the operation times out.- Parameters:
clientId
- id of the sender or receiver or client object that encountered the exception.lastException
- last exception encounteredremainingTime
- remainingTime to retry before the operation times out- Returns:
- duration after which the operation will be retried. Returns null when the operation should not retried.
-
onGetNextRetryInterval
protected abstract Duration onGetNextRetryInterval(String clientId, Exception lastException, Duration remainingTime, int baseWaitTime)
Adjusts the interval after which nextRetry should be attempted, based on the last exception encountered, the remaining time before the operation times out and the minimum wait time before retry. Clients can override this method to specify a wait time based on the exception encountered.- Parameters:
clientId
- id of the sender or receiver or client object that encountered the exception.lastException
- last exception encounteredremainingTime
- remainingTime to retry before the operation times outbaseWaitTime
- minimum wait time determined by the base retry policy. Overriding methods can return a different value.- Returns:
- duration after which the operation will be retried. Returns null when the operation should not retried
-
-