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
   *
   *      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.batch.retry.policy;
 
 import java.util.List;
 
A org.springframework.batch.retry.RetryPolicy that composes a list of other policies and delegates calls to them in order.

Author(s):
Dave Syer
 
 public class CompositeRetryPolicy implements RetryPolicy {
 
 	RetryPolicy[] policies = new RetryPolicy[0];

Setter for policies.

Parameters:
policies
 
 	public void setPolicies(RetryPolicy[] policies) {
 		this. = Arrays.asList(policies).toArray(new RetryPolicy[policies.length]);
 	}

Delegate to the policies that were in operation when the context was created. If any of them cannot retry then return false, oetherwise return true.

 
 	public boolean canRetry(RetryContext context) {
 		RetryContext[] contexts = ((CompositeRetryContextcontext).;
 		RetryPolicy[] policies = ((CompositeRetryContextcontext).;
 		for (int i = 0; i < contexts.lengthi++) {
 			if (!policies[i].canRetry(contexts[i])) {
 				return false;
 			}
 		}
 		return true;
 	}

Delegate to the policies that were in operation when the context was created. If any of them fails to close the exception is propagated (and those later in the chain are closed before re-throwing).

 
 	public void close(RetryContext context) {
 		RetryContext[] contexts = ((CompositeRetryContextcontext).;
 		RetryPolicy[] policies = ((CompositeRetryContextcontext).;
 		RuntimeException exception = null;
 		for (int i = 0; i < contexts.lengthi++) {
 			try {
 				policies[i].close(contexts[i]);
 			}
 			catch (RuntimeException e) {
 				if (exception == null) {
 					exception = e;
 				}
 			}
 		}
 		if (exception != null) {
 			throw exception;
 		}
 	}

Creates a new context that copies the existing policies and keeps a list of the contexts from each one.

 
 	public RetryContext open(RetryContext parent) {
 		List<RetryContextlist = new ArrayList<RetryContext>();
 		for (int i = 0; i < .i++) {
			list.add([i].open(parent));
		}
		return new CompositeRetryContext(parentlist);
	}

Delegate to the policies that were in operation when the context was created.

	public void registerThrowable(RetryContext contextThrowable throwable) {
		RetryContext[] contexts = ((CompositeRetryContextcontext).;
		RetryPolicy[] policies = ((CompositeRetryContextcontext).;
		for (int i = 0; i < contexts.lengthi++) {
			policies[i].registerThrowable(contexts[i], throwable);
		}
		((RetryContextSupportcontext).registerThrowable(throwable);
	}
	private class CompositeRetryContext extends RetryContextSupport {
		public CompositeRetryContext(RetryContext parentList<RetryContextcontexts) {
			super(parent);
			this. = contexts.toArray(new RetryContext[0]);
		}
	}
New to GrepCode? Check out our FAQ X