Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 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;
 
 
Repeats every call to a wrapped object method according to provided schedule. If RetryPolicy is provided calls that fail are retried according to it. Retry count is reset at each scheduled invocation.

Author(s):
fateev
 
 public class ScheduleDecorator implements Decorator {
 
 
     private final class DecoratorInvocationHandler implements InvocationHandler {
 
         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();
             }
             Class<?> returnType = method.getReturnType();
             boolean isVoidReturnType = ..equals(returnType);
             final Settable<Objectresult = isVoidReturnType ? null : new Settable<Object>();
             new TryFinally() {
 
                 Object r;
 
                 @Override
                 protected void doTry() throws Throwable {
                     .execute(new AsyncRunnable() {
 
                         @Override
                         public void run() throws Throwable {
                             try {
                                  = method.invoke(args);
                             }
                             catch (InvocationTargetException ite) {
                                 throw ite.getTargetException();
                             }
                         }
                     });
                 }
 
                 @Override
                 protected void doFinally() throws Throwable {
                     if (result != null) {
                         result.set();
                     }
                 }
 
             };
             return result;
         }
     }
     
     private final AsyncScheduledExecutor scheduledExecutor;
 
     public ScheduleDecorator(InvocationSchedule scheduleWorkflowClock clock) {
          = new AsyncScheduledExecutor(scheduleclock);
     }
 
     public ScheduleDecorator(InvocationSchedule schedule) {
         this(schedulenew 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 boolean isDecorated(Method methodObject[] args) {
        return !method.getDeclaringClass().equals(Object.class);
    }
New to GrepCode? Check out our FAQ X