Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2006-2007 the original author or authors.
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 package org.springframework.batch.retry.backoff;
Implementation of BackOffPolicy that increases the back off period for each retry attempt in a given set using the exponential function.

This implementation is thread-safe and suitable for concurrent access. Modifications to the configuration do not affect any retry sets that are already in progress.

The setInitialInterval(long) property controls the initial value passed to java.lang.Math.exp(double) and the setMultiplier(double) property controls by how much this value is increased for each subsequent attempt.

Rob Harrop
Dave Syer
 public class ExponentialBackOffPolicy implements BackOffPolicy {

The default 'initialInterval' value - 100 millisecs. Coupled with the default 'multiplier' value this gives a useful initial spread of pauses for 1-5 retries.
 	public static final long DEFAULT_INITIAL_INTERVAL = 100L;

The default maximum backoff time (30 seconds).
 	public static final long DEFAULT_MAX_INTERVAL = 30000L;

The default 'multiplier' value - value 2 (100% increase per backoff).
 	public static final double DEFAULT_MULTIPLIER = 2;

The initial sleep interval.
 	private volatile long initialInterval = ;

The maximum value of the backoff period in milliseconds.
 	private volatile long maxInterval = ;

The value to increment the exp seed with for each retry attempt.
 	private volatile double multiplier = ;
 	private Sleeper sleeper = new ObjectWaitSleeper();

Public setter for the Sleeper strategy.

sleeper the sleeper to set defaults to ObjectWaitSleeper.
 	public void setSleeper(Sleeper sleeper) {
 		this. = sleeper;

Set the initial sleep interval value. Default is 100 millisecond. Cannot be set to a value less than one.
 	public void setInitialInterval(long initialInterval) {
 		this. = (initialInterval > 1 ? initialInterval : 1);

Set the multiplier value. Default is '2.0'. Hint: do not use values much in excess of 1.0 (or the backoff will get very long very fast).
 	public void setMultiplier(double multiplier) {
 		this. = (multiplier > 1.0 ? multiplier : 1.0);

Setter for maximum back off period. Default is 30000 (30 seconds). the value will be reset to 1 if this method is called with a value less than 1. Set this to avoid infinite waits if backing off a large number of times (or if the multiplier is set too high).

maxInterval in milliseconds.
	public void setMaxInterval(long maxInterval) {
		this. = maxInterval > 0 ? maxInterval : 1;

The initial period to sleep on the first backoff.

the initial interval
	public long getInitialInterval() {

The maximum interval to sleep for. Defaults to 30 seconds.

the maximum interval.
	public long getMaxInterval() {
		return ;

The multiplier to use to generate the next backoff interval from the last.

the multiplier in use
	public double getMultiplier() {
		return ;

Returns a new instance of BackOffContext configured with the 'expSeed' and 'increment' values.
	public BackOffContext start(RetryContext context) {

Pause for a length of time equal to ' exp(backOffContext.expSeed)'.
	public void backOff(BackOffContext backOffContextthrows BackOffInterruptedException {
		try {
		catch (InterruptedException e) {
			throw new BackOffInterruptedException("Thread interrupted while sleeping"e);
	private static class ExponentialBackOffContext implements BackOffContext {
		private final double multiplier;
		private long interval;
		private long maxInterval;
		public ExponentialBackOffContext(long expSeeddouble multiplierlong maxInterval) {
			this. = expSeed;
			this. = multiplier;
			this. = maxInterval;
		public synchronized long getSleepAndIncrement() {
			long sleep = this.;
			if (sleep > ) {
				sleep = (long;
			else {
				this. *= this.;
			return sleep;
	public String toString() {
		return ClassUtils.getShortName(getClass()) + "[initialInterval=" +  + ", multiplier="
 + ", maxInterval=" +  + "]";
New to GrepCode? Check out our FAQ X