BATCH-1718: Added optimistic flag

This commit is contained in:
Michael Minella
2012-10-02 11:25:47 -05:00
committed by Gunnar Hillert
parent 2875065d7c
commit 35fd6e2826
2 changed files with 43 additions and 6 deletions

View File

@@ -27,13 +27,24 @@ import org.springframework.retry.context.RetryContextSupport;
/**
* 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 class CompositeRetryPolicy implements RetryPolicy {
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 File

@@ -135,4 +135,16 @@ public class CompositeRetryPolicyTests extends TestCase {
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));
}
}