Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.amazonaws.services.simpleworkflow.flow.interceptors;
 
 import java.util.Date;
 
AsyncExecutor implementation that executes command according to provided schedule. Command is expected to contain only non blocking asynchronous code.

Author(s):
fateev
public class AsyncScheduledExecutor implements AsyncExecutor {
    private final InvocationSchedule schedule;
    private final WorkflowClock clock;
    public AsyncScheduledExecutor(InvocationSchedule scheduleWorkflowClock clock) {
        this. = schedule;
        this. = clock;
    }
    public void execute(AsyncRunnable command) {
        scheduleNext(commandnew Date(.currentTimeMillis()), 0, Promise.asPromise((Date)null));
    }
    private void scheduleNext(final AsyncRunnable commandDate startTimeint pastInvocationsCountfinal Promise<Dateinvoked) {
        Date currentTime = new Date(.currentTimeMillis());
        long nextInvocationDelay = .nextInvocationDelaySeconds(currentTimestartTimeinvoked.get(), pastInvocationsCount);
        if (nextInvocationDelay >= 0) {
            Promise<VoidnextInvocationTimer = .createTimer(nextInvocationDelay);
            // Recursing from task (or @Asynchronous) is always OK
            executeAccordingToSchedule(commandstartTimepastInvocationsCountnextInvocationTimer);
        }
    }
    private void executeAccordingToSchedule(final AsyncRunnable commandfinal Date startTimefinal int pastInvocationsCountPromise<VoidnextInvocationTimer) {
        final Settable<Dateinvoked = new Settable<Date>();
        new TryFinally(nextInvocationTimer) {
            private Date lastInvocationTime;
            @Override
            protected void doTry() throws Throwable {
                 = new Date(.currentTimeMillis());
                command.run();
            }
            @Override
            protected void doFinally() throws Throwable {
                // It is common mistake to recurse from doFinally or doCatch.
                // As code in doFinally and in doCatch is non cancelable it 
                // makes the whole branch non cancelable which is usually 
                // not intended. 
                invoked.set();
            }
        };
        new Task(invoked) {
            @Override
            protected void doExecute() throws Throwable {
                scheduleNext(commandstartTimepastInvocationsCount + 1, invoked);
            }
        };
    }
New to GrepCode? Check out our FAQ X