Skip to content

UTIL_Retry.Strategy

Class

apex
global interface UTIL_Retry.Strategy

Interface defining the retry strategy logic. Implement this interface to create custom retry strategies.

Since: 1.0


Methods

MethodDescription
global abstract Integer calculateBackoff(UTIL_Retry.Context context)Calculates the backoff period in seconds for the next retry
global abstract Boolean shouldRetry(UTIL_Retry.Context context)Determines if a retry should be attempted
global abstract UTIL_Retry.Strategy withBaseBackoff(Integer seconds)Sets the base backoff period in seconds (fluent API)
global abstract UTIL_Retry.Strategy withExponentialMultiplier(Decimal multiplier)Sets the exponential multiplier for exponential backoff (fluent API)
global abstract UTIL_Retry.Strategy withJitter(Boolean enabled)Enables or disables random jitter (fluent API)
global abstract UTIL_Retry.Strategy withMaximumBackoff(Integer seconds)Sets the maximum backoff cap in seconds (fluent API)
global abstract UTIL_Retry.Strategy withMaxRetries(Integer max)Sets the maximum retry attempts (fluent API)

Method Details

calculateBackoff

apex
global abstract Integer calculateBackoff(UTIL_Retry.Context context)

Calculates the backoff period in seconds for the next retry

Parameters:

Returns: Integer - The backoff period in seconds

Example:

apex
UTIL_Retry.Strategy strategy = UTIL_Retry.exponential();
UTIL_Retry.Context ctx = UTIL_Retry.newContext(1);
Integer backoffSeconds = strategy.calculateBackoff(ctx);
Datetime nextRetry = Datetime.now().addSeconds(backoffSeconds);

shouldRetry

apex
global abstract Boolean shouldRetry(UTIL_Retry.Context context)

Determines if a retry should be attempted

Parameters:

Returns: Boolean - True if a retry should be attempted, false otherwise

Example:

apex
UTIL_Retry.Strategy strategy = UTIL_Retry.exponential();
UTIL_Retry.Context ctx = UTIL_Retry.newContext(2);
if(strategy.shouldRetry(ctx))
{
    // Perform retry logic
}

withBaseBackoff

apex
global abstract UTIL_Retry.Strategy withBaseBackoff(Integer seconds)

Sets the base backoff period in seconds (fluent API)

Parameters:

  • seconds (Integer) - The base backoff period

Returns: UTIL_Retry.Strategy - This strategy for method chaining

Example:

apex
UTIL_Retry.Strategy strategy = UTIL_Retry.linear()
    .withBaseBackoff(15);

withExponentialMultiplier

apex
global abstract UTIL_Retry.Strategy withExponentialMultiplier(Decimal multiplier)

Sets the exponential multiplier for exponential backoff (fluent API)

Parameters:

  • multiplier (Decimal) - The exponential multiplier (must be >= 1.0, default: 2.0)

Returns: UTIL_Retry.Strategy - This strategy for method chaining

Example:

apex
UTIL_Retry.Strategy strategy = UTIL_Retry.exponential()
    .withExponentialMultiplier(3.0); // Faster backoff growth

withJitter

apex
global abstract UTIL_Retry.Strategy withJitter(Boolean enabled)

Enables or disables random jitter (fluent API)

Parameters:

  • enabled (Boolean) - Whether to enable jitter (adds up to 25% randomness)

Returns: UTIL_Retry.Strategy - This strategy for method chaining

Example:

apex
UTIL_Retry.Strategy strategy = UTIL_Retry.exponential()
    .withJitter(true);

withMaximumBackoff

apex
global abstract UTIL_Retry.Strategy withMaximumBackoff(Integer seconds)

Sets the maximum backoff cap in seconds (fluent API)

Parameters:

  • seconds (Integer) - Maximum backoff cap in seconds

Returns: UTIL_Retry.Strategy - This strategy for method chaining

Example:

apex
UTIL_Retry.Strategy strategy = UTIL_Retry.exponential()
    .withMaximumBackoff(600); // Cap at 10 minutes

withMaxRetries

apex
global abstract UTIL_Retry.Strategy withMaxRetries(Integer max)

Sets the maximum retry attempts (fluent API)

Parameters:

  • max (Integer) - The maximum retry attempts

Returns: UTIL_Retry.Strategy - This strategy for method chaining

Example:

apex
UTIL_Retry.Strategy strategy = UTIL_Retry.exponential()
    .withMaxRetries(5);