Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

BATCH-1718: Added optimistic flag

  • Loading branch information...
commit 35fd6e28263b21b637db9d6a8660192d6cb56206 1 parent 2875065
@mminella mminella authored Gunnar Hillert committed
View
37 src/main/java/org/springframework/retry/policy/CompositeRetryPolicy.java
@@ -27,13 +27,24 @@
/**
* A {@link RetryPolicy} that composes a list of other policies and delegates
* calls to them in order.
- *
+ *
* @author Dave Syer
- *
+ * @author Michael Minella
+ *
*/
public class CompositeRetryPolicy implements RetryPolicy {
RetryPolicy[] policies = new RetryPolicy[0];
+ private boolean optimistic = false;
+
+ /**
+ * Setter for optimistic.
+ *
+ * @param optimistic
+ */
+ public void setOptimistic(boolean optimistic) {
+ this.optimistic = optimistic;
+ }
/**
* Setter for policies.
@@ -54,12 +65,26 @@ public void setPolicies(RetryPolicy[] policies) {
public boolean canRetry(RetryContext context) {
RetryContext[] contexts = ((CompositeRetryContext) context).contexts;
RetryPolicy[] policies = ((CompositeRetryContext) context).policies;
- for (int i = 0; i < contexts.length; i++) {
- if (!policies[i].canRetry(contexts[i])) {
- return false;
+
+ boolean retryable = true;
+
+ if(optimistic) {
+ retryable = false;
+ for (int i = 0; i < contexts.length; i++) {
+ if (policies[i].canRetry(contexts[i])) {
+ retryable = true;
+ }
}
}
- return true;
+ else {
+ for (int i = 0; i < contexts.length; i++) {
+ if (!policies[i].canRetry(contexts[i])) {
+ retryable = false;
+ }
+ }
+ }
+
+ return retryable;
}
/**
View
12 src/test/java/org/springframework/retry/policy/CompositeRetryPolicyTests.java
@@ -135,4 +135,16 @@ public void testParent() throws Exception {
assertSame(context, child.getParent());
}
+ public void testOptimistic() throws Exception {
+ CompositeRetryPolicy policy = new CompositeRetryPolicy();
+ policy.setOptimistic(true);
+ policy.setPolicies(new RetryPolicy[] { new MockRetryPolicySupport() {
+ public boolean canRetry(RetryContext context) {
+ return false;
+ }
+ }, new MockRetryPolicySupport() });
+ RetryContext context = policy.open(null);
+ assertNotNull(context);
+ assertTrue(policy.canRetry(context));
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.