class StatefulRetryOperationsInterceptor implements MethodInterceptor {

	private transient final Log logger = LogFactory.getLog(getClass());

	private MethodArgumentsKeyGenerator keyGenerator;

	private MethodInvocationRecoverer<?> recoverer;

	private NewMethodArgumentsIdentifier newMethodArgumentsIdentifier;

	private RetryOperations retryOperations;

	private String label;

	private Classifier<? super Throwable, Boolean> rollbackClassifier;

	private boolean useRawKey;

	public StatefulRetryOperationsInterceptor() {
		RetryTemplate retryTemplate = new RetryTemplate();
		retryTemplate.setRetryPolicy(new NeverRetryPolicy());
		this.retryOperations = retryTemplate;
	}

	public void setRetryOperations(RetryOperations retryTemplate) {
		Assert.notNull(retryTemplate, "'retryOperations' cannot be null.");
		this.retryOperations = retryTemplate;
	}

	/**
	 * Public setter for the {@link MethodInvocationRecoverer} to use if the retry is
	 * exhausted. The recoverer should be able to return an object of the same type as the
	 * target object because its return value will be used to return to the caller in the
	 * case of a recovery.
	 * @param recoverer the {@link MethodInvocationRecoverer} to set
	 */
	public void setRecoverer(MethodInvocationRecoverer<?> recoverer) {
		this.recoverer = recoverer;
	}

	/**
	 * Rollback classifier for the retry state. Default to null (meaning rollback for
	 * all).
	 * @param rollbackClassifier the rollbackClassifier to set
	 */
	public void setRollbackClassifier(Classifier<? super Throwable, Boolean> rollbackClassifier) {
		this.rollbackClassifier = rollbackClassifier;
	}

	public void setKeyGenerator(MethodArgumentsKeyGenerator keyGenerator) {
		this.keyGenerator = keyGenerator;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	/**
	 * Public setter for the {@link NewMethodArgumentsIdentifier}. Only set this if the
	 * arguments to the intercepted method can be inspected to find out if they have never
	 * been processed before.
	 * @param newMethodArgumentsIdentifier the {@link NewMethodArgumentsIdentifier} to set
	 */
	public void setNewItemIdentifier(NewMethodArgumentsIdentifier newMethodArgumentsIdentifier) {
		this.newMethodArgumentsIdentifier = newMethodArgumentsIdentifier;
	}

	/**
	 * Set to true to use the raw key generated by the key generator. Should only be set
	 * to true for cases where the key is guaranteed to be unique in all cases. When
	 * false, a compound key is used, including invocation metadata. Default: false.
	 * @param useRawKey the useRawKey to set.
	 */
	public void setUseRawKey(boolean useRawKey) {
		this.useRawKey = useRawKey;
	}

	/**
	 * Wrap the method invocation in a stateful retry with the policy and other helpers
	 * provided. If there is a failure the exception will generally be re-thrown. The only
	 * time it is not re-thrown is when retry is exhausted and the recovery path is taken
	 * (though the {@link MethodInvocationRecoverer} provided if there is one). In that
	 * case the value returned from the method invocation will be the value returned by
	 * the recoverer (so the return type for that should be the same as the intercepted
	 * method).
	 * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
	 * @see MethodInvocationRecoverer#recover(Object[], Throwable)
	 *
	 */
	@Override
	public Object invoke(final MethodInvocation invocation) throws Throwable {

		if (this.logger.isDebugEnabled()) {
			this.logger.debug("Executing proxied method in stateful retry: " + invocation.getStaticPart() + "("
					+ ObjectUtils.getIdentityHexString(invocation) + ")");
		}

		Object[] args = invocation.getArguments();
		Object defaultKey = Arrays.asList(args);
		if (args.length == 1) {
			defaultKey = args[0];
		}

		Object key = createKey(invocation, defaultKey);
		RetryState retryState = new DefaultRetryState(key,
				this.newMethodArgumentsIdentifier != null && this.newMethodArgumentsIdentifier.isNew(args),
				this.rollbackClassifier);

		Object result = this.retryOperations.execute(new StatefulMethodInvocationRetryCallback(invocation, label),
				this.recoverer != null ? new ItemRecovererCallback(args, this.recoverer) : null, retryState);

		if (this.logger.isDebugEnabled()) {
			this.logger.debug("Exiting proxied method in stateful retry with result: (" + result + ")");
		}

		return result;

	}

	private Object createKey(final MethodInvocation invocation, Object defaultKey) {<FILL_FUNCTION_BODY>}

	/**
	 * @author Dave Syer
	 *
	 */
	private static final class StatefulMethodInvocationRetryCallback
			extends MethodInvocationRetryCallback<Object, Throwable> {

		private StatefulMethodInvocationRetryCallback(MethodInvocation invocation, String label) {
			super(invocation, label);
		}

		@Override
		public Object doWithRetry(RetryContext context) throws Exception {
			context.setAttribute(RetryContext.NAME, label);
			try {
				return this.invocation.proceed();
			}
			catch (Exception | Error e) {
				throw e;
			}
			catch (Throwable e) {
				throw new IllegalStateException(e);
			}
		}

	}

	/**
	 * @author Dave Syer
	 *
	 */
	private static final class ItemRecovererCallback implements RecoveryCallback<Object> {

		private final Object[] args;

		private final MethodInvocationRecoverer<?> recoverer;

		/**
		 * @param args the item that failed.
		 */
		private ItemRecovererCallback(Object[] args, MethodInvocationRecoverer<?> recoverer) {
			this.args = Arrays.asList(args).toArray();
			this.recoverer = recoverer;
		}

		@Override
		public Object recover(RetryContext context) {
			return this.recoverer.recover(this.args, context.getLastThrowable());
		}

	}

}

		Object generatedKey = defaultKey;
		if (this.keyGenerator != null) {
			generatedKey = this.keyGenerator.getKey(invocation.getArguments());
		}
		if (generatedKey == null) {
			// If there's a generator and he still says the key is null, that means he
			// really doesn't want to retry.
			return null;
		}
		if (this.useRawKey) {
			return generatedKey;
		}
		String name = StringUtils.hasText(label) ? label : invocation.getMethod().toGenericString();
		return Arrays.asList(name, generatedKey);
	
<no_super_class>