Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License"). You may not
   * use this file except in compliance with the License. A copy of the License is
   * located at
   * 
   * http://aws.amazon.com/apache2.0
   * 
  * or in the "license" file accompanying this file. This file 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 com.amazonaws.services.simpleworkflow.flow.interceptors;
 
 
In case of failures repeats every call to a wrapped object method according the provided RetryPolicy.

Author(s):
fateev
 
 public class RetryDecorator implements Decorator {
 
     private final AsyncRetryingExecutor executor;
 
     public RetryDecorator(RetryPolicy retryPolicyWorkflowClock clock) {
          = new AsyncRetryingExecutor(retryPolicyclock);
     }
 
     public RetryDecorator(RetryPolicy retryPolicy) {
         this(retryPolicynew DecisionContextProviderImpl().getDecisionContext().getWorkflowClock());
     }
 
     @SuppressWarnings("unchecked")
     @Override
     public final <V> V decorate(Class<V> interfaces, V object) {
 
         Class<?>[] interfazes = { interfaces };
 
         return (V) Proxy.newProxyInstance(object.getClass().getClassLoader(), interfazesnew DecoratorInvocationHandler(object));
 
     }
 
     @SuppressWarnings("unchecked")
     @Override
     public final <V> V decorate(Class<?>[] interfaces, V object) {
 
         return (V) Proxy.newProxyInstance(object.getClass().getClassLoader(), interfacesnew DecoratorInvocationHandler(object));
     }
 
     private final class DecoratorInvocationHandler implements InvocationHandler {
 
         @SuppressWarnings({ "unchecked""rawtypes" })
         private final class RetriedRunnable implements AsyncRunnable {
 
             private final Object[] args;
 
             private final Method method;
 
             private Settable result;
 
             private RetriedRunnable(Object[] argsMethod method) {
                 this. = args;
                 Class<?> returnType = method.getReturnType();
                 boolean voidReturnType = ..equals(returnType);
                 if (!voidReturnType) {
                     if (!Promise.class.isAssignableFrom(returnType)) {
                         throw new IllegalArgumentException("Cannot decorate " + method.getName()
                                 + " as its return type is not void or Promise");
                     }
                      = new Settable();
                 }
                 this. = method;
             }
 
             @Override
             public void run() throws Throwable {
                 if ( == null) {
                     // void return type
                     .invoke();
                 }
                 else {
                     // Need to unchain as it could be chained to the result of the previous run invocation
                     .unchain();
                     .chain((Promise.invoke());
                 }
             }
 
            public Promise getResult() {
                return ;
            }
        }
        private final Object object;
        public DecoratorInvocationHandler(Object object) {
            this. = object;
        }
        @Override
        public Object invoke(Object proxyfinal Method methodfinal Object[] argsthrows Throwable {
            try {
                if (!isDecorated(methodargs)) {
                    return method.invoke(args);
                }
            }
            catch (InvocationTargetException ite) {
                throw ite.getTargetException();
            }
            RetriedRunnable command = new RetriedRunnable(argsmethod);
            .execute(command);
            return command.getResult();
        }
    }
    protected boolean isDecorated(Method methodObject[] args) {
        return !method.getDeclaringClass().equals(Object.class);
    }
New to GrepCode? Check out our FAQ X