Skip to content

Commit

Permalink
Add @EnableRetry support (like @async)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Syer committed Apr 22, 2014
1 parent 06f766a commit 705eabc
Show file tree
Hide file tree
Showing 14 changed files with 1,133 additions and 63 deletions.
49 changes: 10 additions & 39 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.0.4.BUILD-SNAPSHOT</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
<name>Spring Retry</name>
<description><![CDATA[Spring Retry provides an abstraction around retrying failed operations, with an emphasis on declarative control of the process and policy-based bahaviour that is easy to extend and customize. For instance, you can configure a plain POJO operation to retry if it fails, based on the type of exception, and with a fixed or exponential backoff.
]]></description>
Expand All @@ -21,7 +21,7 @@
<packaging>jar</packaging>
<properties>
<maven.test.failure.ignore>true</maven.test.failure.ignore>
<spring.framework.version>3.0.5.RELEASE</spring.framework.version>
<spring.framework.version>4.0.3.RELEASE</spring.framework.version>
</properties>
<profiles>
<profile>
Expand Down Expand Up @@ -123,22 +123,22 @@
</url>
</site>
<repository>
<id>spring-release</id>
<id>repo.spring.io</id>
<name>Spring Release Repository</name>
<url>s3://maven.springframework.org/release</url>
<url>https://repo.spring.io/libs-release-local</url>
</repository>
<snapshotRepository>
<id>spring-snapshot</id>
<id>repo.spring.io</id>
<name>Spring Snapshot Repository</name>
<url>s3://maven.springframework.org/snapshot</url>
<url>https://repo.spring.io/libs-snapshot-local</url>
</snapshotRepository>
</distributionManagement>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -156,7 +156,7 @@
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.6</version>
<version>1.7.4</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -185,22 +185,7 @@
</dependency>
</dependencies>

<pluginRepositories>
<pluginRepository>
<id>com.springsource.repository.bundles.release</id>
<url>http://repository.springsource.com/maven/bundles/release</url>
</pluginRepository>
</pluginRepositories>


<build>
<extensions>
<extension>
<groupId>org.springframework.build.aws</groupId>
<artifactId>org.springframework.build.aws.maven</artifactId>
<version>3.0.0.RELEASE</version>
</extension>
</extensions>
<pluginManagement>
<plugins>
<plugin>
Expand Down Expand Up @@ -233,8 +218,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
Expand All @@ -250,20 +235,6 @@
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>com.springsource.bundlor</groupId>
<artifactId>com.springsource.bundlor.maven</artifactId>
<version>1.0.0.RELEASE</version>
<inherited>true</inherited>
<executions>
<execution>
<id>bundlor</id>
<goals>
<goal>bundlor</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@

package org.springframework.retry.backoff;


/**
* Implementation of {@link BackOffPolicy} that pauses for a fixed period of
* time before continuing. A pause is implemented using {@link Thread#sleep(long)}.
* <p/> {@link #setBackOffPeriod(long)} is thread-safe and it is safe to call
* {@link #setBackOffPeriod} during execution from multiple threads, however
* this may cause a single retry operation to have pauses of different
* intervals.
* Implementation of {@link BackOffPolicy} that pauses for a fixed period of time before
* continuing. A pause is implemented using {@link Sleeper#sleep(long)}.
* <p/>
* {@link #setBackOffPeriod(long)} is thread-safe and it is safe to call
* {@link #setBackOffPeriod} during execution from multiple threads, however this may
* cause a single retry operation to have pauses of different intervals.
* @author Rob Harrop
* @author Dave Syer
*/
public class FixedBackOffPolicy extends StatelessBackOffPolicy implements SleepingBackOffPolicy<FixedBackOffPolicy> {
public class FixedBackOffPolicy extends StatelessBackOffPolicy implements
SleepingBackOffPolicy<FixedBackOffPolicy> {

/**
* Default back off period - 1000ms.
Expand All @@ -39,15 +39,14 @@ public class FixedBackOffPolicy extends StatelessBackOffPolicy implements Sleepi
*/
private volatile long backOffPeriod = DEFAULT_BACK_OFF_PERIOD;


private Sleeper sleeper = new ObjectWaitSleeper();

public FixedBackOffPolicy withSleeper(Sleeper sleeper) {
FixedBackOffPolicy res = new FixedBackOffPolicy();
res.setBackOffPeriod(backOffPeriod);
res.setSleeper(sleeper);
return res;
}
public FixedBackOffPolicy withSleeper(Sleeper sleeper) {
FixedBackOffPolicy res = new FixedBackOffPolicy();
res.setBackOffPeriod(backOffPeriod);
res.setSleeper(sleeper);
return res;
}

/**
* Public setter for the {@link Sleeper} strategy.
Expand All @@ -58,8 +57,7 @@ public void setSleeper(Sleeper sleeper) {
}

/**
* Set the back off period in milliseconds. Cannot be &lt; 1. Default value
* is 1000ms.
* Set the back off period in milliseconds. Cannot be &lt; 1. Default value is 1000ms.
*/
public void setBackOffPeriod(long backOffPeriod) {
this.backOffPeriod = (backOffPeriod > 0 ? backOffPeriod : 1);
Expand All @@ -80,13 +78,12 @@ public long getBackOffPeriod() {
protected void doBackOff() throws BackOffInterruptedException {
try {
sleeper.sleep(backOffPeriod);
}
catch (InterruptedException e) {
} catch (InterruptedException e) {
throw new BackOffInterruptedException("Thread interrupted while sleeping", e);
}
}

public String toString() {
return "FixedBackOffPolicy[backOffPeriod=" + backOffPeriod + "]";
}
public String toString() {
return "FixedBackOffPolicy[backOffPeriod=" + backOffPeriod + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.retry.backoff;

import java.util.Random;


/**
* Implementation of {@link BackOffPolicy} that pauses for a random period of
* time before continuing. A pause is implemented using {@link Sleeper#sleep(long)}.
* <p/> {@link #setMinBackOffPeriod(long)} is thread-safe and it is safe to call
* {@link #setBackOffPeriod} during execution from multiple threads, however
* this may cause a single retry operation to have pauses of different
* intervals.
* @author Rob Harrop
* @author Dave Syer
*/
public class UniformRandomBackOffPolicy extends StatelessBackOffPolicy implements SleepingBackOffPolicy<UniformRandomBackOffPolicy> {

/**
* Default min back off period - 500ms.
*/
private static final long DEFAULT_BACK_OFF_MIN_PERIOD = 500L;

/**
* Default max back off period - 1500ms.
*/
private static final long DEFAULT_BACK_OFF_MAX_PERIOD = 1500L;

private volatile long minBackOffPeriod = DEFAULT_BACK_OFF_MIN_PERIOD;

private volatile long maxBackOffPeriod = DEFAULT_BACK_OFF_MAX_PERIOD;

private Random random = new Random(System.currentTimeMillis());

private Sleeper sleeper = new ObjectWaitSleeper();

public UniformRandomBackOffPolicy withSleeper(Sleeper sleeper) {
UniformRandomBackOffPolicy res = new UniformRandomBackOffPolicy();
res.setMinBackOffPeriod(minBackOffPeriod);
res.setSleeper(sleeper);
return res;
}

/**
* Public setter for the {@link Sleeper} strategy.
* @param sleeper the sleeper to set defaults to {@link ObjectWaitSleeper}.
*/
public void setSleeper(Sleeper sleeper) {
this.sleeper = sleeper;
}

/**
* Set the minimum back off period in milliseconds. Cannot be &lt; 1. Default value
* is 500ms.
*/
public void setMinBackOffPeriod(long backOffPeriod) {
this.minBackOffPeriod = (backOffPeriod > 0 ? backOffPeriod : 1);
}

/**
* The minimum backoff period in milliseconds.
* @return the backoff period
*/
public long getMinBackOffPeriod() {
return minBackOffPeriod;
}

/**
* Set the maximum back off period in milliseconds. Cannot be &lt; 1. Default value
* is 1500ms.
*/
public void setMaxBackOffPeriod(long backOffPeriod) {
this.maxBackOffPeriod = (backOffPeriod > 0 ? backOffPeriod : 1);
}

/**
* The maximum backoff period in milliseconds.
* @return the backoff period
*/
public long getMaxBackOffPeriod() {
return maxBackOffPeriod;
}

/**
* Pause for the {@link #setMinBackOffPeriod(long)}.
* @throws BackOffInterruptedException if interrupted during sleep.
*/
protected void doBackOff() throws BackOffInterruptedException {
try {
long delta = maxBackOffPeriod==minBackOffPeriod ? 0 : random.nextInt((int) (maxBackOffPeriod - minBackOffPeriod));
sleeper.sleep(minBackOffPeriod + delta );
}
catch (InterruptedException e) {
throw new BackOffInterruptedException("Thread interrupted while sleeping", e);
}
}

public String toString() {
return "RandomBackOffPolicy[backOffPeriod=" + minBackOffPeriod + ", " + maxBackOffPeriod + "]";
}
}
Loading

0 comments on commit 705eabc

Please sign in to comment.