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.worker;
 
 import java.util.Map;
 
 
 
 class WorkflowClockImpl implements WorkflowClock {
 
     private static final Log log = LogFactory.getLog(WorkflowClockImpl.class);
 
     private final class TimerCancellationHandler implements ExternalTaskCancellationHandler {
 
         private final String timerId;
 
         private TimerCancellationHandler(String timerId) {
             this. = timerId;
         }
 
         @Override
         public void handleCancellation(Throwable cause) {
             .cancelTimer(new Runnable() {
 
                 @Override
                 public void run() {
                     OpenRequestInfo<?, ?> scheduled = .remove();
                     ExternalTaskCompletionHandle context = scheduled.getCompletionHandle();
                     context.complete();
                 }
             });
         }
     }
 
     private final DecisionsHelper decisions;
 
     private final Map<StringOpenRequestInfo<?, ?>> scheduledTimers = new HashMap<StringOpenRequestInfo<?, ?>>();
 
     private long replayCurrentTimeMilliseconds;
 
     private boolean replaying = true;
 
     WorkflowClockImpl(DecisionsHelper decisions) {
         this. = decisions;
     }
 
     @Override
     public long currentTimeMillis() {
         return ;
     }
 
     void setReplayCurrentTimeMilliseconds(long replayCurrentTimeMilliseconds) {
         this. = replayCurrentTimeMilliseconds;
     }
 
     @Override
     public boolean isReplaying() {
         return ;
     }
 
     void setReplaying(boolean replaying) {
         this. = replaying;
     }
 
     @Override
     public Promise<VoidcreateTimer(long delaySeconds) {
         return createTimer(delaySecondsnull);
     }
 
     @Override
     public <T> Promise<T> createTimer(final long delaySecondsfinal T userContext) {
        if (delaySeconds < 0) {
            throw new IllegalArgumentException("Negative delaySeconds: " + delaySeconds);
        }
        if (delaySeconds == 0) {
            return Promise.asPromise(userContext);
        }
        final OpenRequestInfo<T, Objectcontext = new OpenRequestInfo<T, Object>(userContext);
        final StartTimerDecisionAttributes timer = new StartTimerDecisionAttributes();
        timer.setStartToFireTimeout(FlowHelpers.secondsToDuration(delaySeconds));
        final String timerId = .getNextId();
        timer.setTimerId(timerId);
        String taskName = "timerId=" + timer.getTimerId() + ", delaySeconds=" + timer.getStartToFireTimeout();
        new ExternalTask() {
            @Override
            protected ExternalTaskCancellationHandler doExecute(ExternalTaskCompletionHandle handlethrows Throwable {
                .startTimer(timeruserContext);
                context.setCompletionHandle(handle);
                .put(timerIdcontext);
                return new TimerCancellationHandler(timerId);
            }
        }.setName(taskName);
        context.setResultDescription("createTimer " + taskName);
        return context.getResult();
    }
    @SuppressWarnings({ "rawtypes""unchecked" })
    void handleTimerFired(Long eventIdTimerFiredEventAttributes attributes) {
        String timerId = attributes.getTimerId();
        if (.handleTimerClosed(timerId)) {
            OpenRequestInfo scheduled = .remove(timerId);
            if (scheduled != null) {
                ExternalTaskCompletionHandle completionHandle = scheduled.getCompletionHandle();
                scheduled.getResult().set(scheduled.getUserContext());
                completionHandle.complete();
            }
        }
        else {
            .debug("handleTimerFired not complete");
        }
    }
    @SuppressWarnings({ "rawtypes" })
    void handleStartTimerFailed(HistoryEvent event) {
        String timerId = attributes.getTimerId();
        if (.handleStartTimerFailed(event)) {
            OpenRequestInfo scheduled = .remove(timerId);
            if (scheduled != null) {
                ExternalTaskCompletionHandle completionHandle = scheduled.getCompletionHandle();
                Object createTimerUserContext = scheduled.getUserContext();
                String cause = attributes.getCause();
                Throwable failure = new StartTimerFailedException(event.getEventId(), timerIdcreateTimerUserContextcause);
                completionHandle.fail(failure);
            }
        }
        else {
            .debug("handleStartTimerFailed not complete");
        }
    }
    void handleTimerCanceled(HistoryEvent event) {
        TimerCanceledEventAttributes attributes = event.getTimerCanceledEventAttributes();
        String timerId = attributes.getTimerId();
        if (.handleTimerCanceled(event)) {
            OpenRequestInfo<?, ?> scheduled = .remove(timerId);
            if (scheduled != null) {
                ExternalTaskCompletionHandle completionHandle = scheduled.getCompletionHandle();
                CancellationException exception = new CancellationException();
                completionHandle.fail(exception);
            }
        }
        else {
            .debug("handleTimerCanceled not complete");
        }
    }
New to GrepCode? Check out our FAQ X