Permalink
Browse files

Merge pull request #5 from mminella/BATCH-1718

# By Michael Minella
* mminella-BATCH-1718:
  BATCH-1718: Added optimistic flag
  • Loading branch information...
2 parents 2875065 + 35fd6e2 commit 907bba3c2c43caa3fa4da15ec749f1c2c4839013 Gunnar Hillert committed Oct 3, 2012
@@ -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;
}
/**
@@ -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));
+ }
}

0 comments on commit 907bba3

Please sign in to comment.