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.List;
 import java.util.Map;
 
 
 
     private static class StartChildWorkflowReplyImpl implements StartChildWorkflowReply {
 
         private String runId;
 
         private final Settable<Stringresult = new Settable<String>();
 
         public StartChildWorkflowReplyImpl(String runIdString description) {
             this. = runId;
             .setDescription(description);
         }
 
         @Override
         public String getRunId() {
             return ;
         }
 
         @Override
         public Promise<StringgetResult() {
             return ;
         }
 
         public void setResult(String value) {
             .set(value);
         }
 
     }
 
     private final class ChildWorkflowCancellationHandler implements ExternalTaskCancellationHandler {
 
         private final String workflowId;
 
         private final ExternalTaskCompletionHandle handle;
 
         private ChildWorkflowCancellationHandler(String workflowIdExternalTaskCompletionHandle handle) {
             this. = workflowId;
             this. = handle;
         }
 
         @Override
        public void handleCancellation(Throwable cause) {
            cancelAttributes.setWorkflowId();
            .requestCancelExternalWorkflowExecution(truecancelAttributesnew Runnable() {
                @Override
                public void run() {
                    OpenRequestInfo<StartChildWorkflowReplyWorkflowTypescheduled = .remove();
                    if (scheduled == null) {
                        throw new IllegalArgumentException("Workflow \"" +  + "\" wasn't scheduled");
                    }
                    .complete();
                }
            });
        }
    }
    private final DecisionsHelper decisions;
    private final WorkflowContext workflowContext;
    private final Map<StringOpenRequestInfo<VoidVoid>> scheduledSignals = new HashMap<StringOpenRequestInfo<VoidVoid>>();
    GenericWorkflowClientImpl(DecisionsHelper decisionsWorkflowContext workflowContext) {
        this. = decisions;
        this. = workflowContext;
    }
    @Override
        attributes.setWorkflowType(parameters.getWorkflowType());
        String workflowId = parameters.getWorkflowId();
        if (workflowId == null) {
            workflowId = generateUniqueId();
        }
        attributes.setWorkflowId(workflowId);
        attributes.setInput(parameters.getInput());
        attributes.setTaskStartToCloseTimeout(FlowHelpers.secondsToDuration(parameters.getTaskStartToCloseTimeoutSeconds()));
        attributes.setTaskPriority(FlowHelpers.taskPriorityToString(parameters.getTaskPriority()));
        List<StringtagList = parameters.getTagList();
        if (tagList != null) {
            attributes.setTagList(tagList);
        }
        ChildPolicy childPolicy = parameters.getChildPolicy();
        if (childPolicy != null) {
            attributes.setChildPolicy(childPolicy);
        }
        String taskList = parameters.getTaskList();
        if (taskList != null && !taskList.isEmpty()) {
            attributes.setTaskList(new TaskList().withName(taskList));
        }
        String taskName = "workflowId=" + workflowId + ", workflowType=" + attributes.getWorkflowType();
        new ExternalTask() {
            @Override
            protected ExternalTaskCancellationHandler doExecute(final ExternalTaskCompletionHandle handlethrows Throwable {
                .startChildWorkflowExecution(attributes);
                context.setCompletionHandle(handle);
                .put(attributes.getWorkflowId(), context);
                return new ChildWorkflowCancellationHandler(attributes.getWorkflowId(), handle);
            }
        }.setName(taskName);
        context.setResultDescription("startChildWorkflow " + taskName);
        return context.getResult();
    }
    @Override
    public Promise<StringstartChildWorkflow(String workflowString versionString input) {
        parameters.setWorkflowType(new WorkflowType().withName(workflow).withVersion(version));
        parameters.setInput(input);
        final Promise<StartChildWorkflowReplystarted = startChildWorkflow(parameters);
        return new Functor<String>(started) {
            @Override
            protected Promise<StringdoExecute() throws Throwable {
                return started.get().getResult();
            }
        };
    }
    @Override
    public Promise<StringstartChildWorkflow(final String workflowfinal String versionfinal Promise<Stringinput) {
        final Settable<Stringresult = new Settable<String>();
        new Task(input) {
            @Override
            protected void doExecute() throws Throwable {
                result.chain(startChildWorkflow(workflowversioninput.get()));
            }
        };
        return result;
    }
    @Override
        final OpenRequestInfo<VoidVoidcontext = new OpenRequestInfo<VoidVoid>();
        String signalId = .getNextId();
        attributes.setControl(signalId);
        attributes.setSignalName(parameters.getSignalName());
        attributes.setInput(parameters.getInput());
        attributes.setRunId(parameters.getRunId());
        attributes.setWorkflowId(parameters.getWorkflowId());
        String taskName = "signalId=" + signalId + ", workflowId=" + parameters.getWorkflowId() + ", workflowRunId="
                + parameters.getRunId();
        new ExternalTask() {
            @Override
            protected ExternalTaskCancellationHandler doExecute(final ExternalTaskCompletionHandle handlethrows Throwable {
                .signalExternalWorkflowExecution(attributes);
                context.setCompletionHandle(handle);
                final String finalSignalId = attributes.getControl();
                .put(finalSignalIdcontext);
                return new ExternalTaskCancellationHandler() {
                    @Override
                    public void handleCancellation(Throwable cause) {
                        .cancelSignalExternalWorkflowExecution(finalSignalIdnull);
                        OpenRequestInfo<VoidVoidscheduled = .remove(finalSignalId);
                        if (scheduled == null) {
                            throw new IllegalArgumentException("Signal \"" + finalSignalId + "\" wasn't scheduled");
                        }
                        handle.complete();
                    }
                };
            }
        }.setName(taskName);
        context.setResultDescription("signalWorkflowExecution " + taskName);
        return context.getResult();
    }
    @Override
    public void requestCancelWorkflowExecution(WorkflowExecution execution) {
        String workflowId = execution.getWorkflowId();
        attributes.setWorkflowId(workflowId);
        attributes.setRunId(execution.getRunId());
        boolean childWorkflow = .containsKey(workflowId);
        // TODO: See if immediate cancellation needed
        .requestCancelExternalWorkflowExecution(childWorkflowattributesnull);
    }
    @Override
    public void continueAsNewOnCompletion(ContinueAsNewWorkflowExecutionParameters continueParameters) {
        // TODO: add validation to check if continueAsNew is not set 
        .setContinueAsNewOnCompletion(continueParameters);
    }
    @Override
    public String generateUniqueId() {
        WorkflowExecution workflowExecution = .getWorkflowExecution();
        String runId = workflowExecution.getRunId();
        return runId + ":" + .getNextId();
    }
    }
        WorkflowExecution execution = attributes.getWorkflowExecution();
        String workflowId = execution.getWorkflowId();
        if (.handleChildWorkflowExecutionCanceled(workflowId)) {
            OpenRequestInfo<StartChildWorkflowReplyWorkflowTypescheduled = .remove(workflowId);
            if (scheduled != null) {
                CancellationException e = new CancellationException();
                ExternalTaskCompletionHandle completionHandle = scheduled.getCompletionHandle();
                // It is OK to fail with subclass of CancellationException when cancellation requested.
                // It allows passing information about cancellation (details in this case) to the surrounding doCatch block
                completionHandle.fail(e);
            }
        }
    }
        WorkflowExecution execution = attributes.getWorkflowExecution();
        String workflowId = execution.getWorkflowId();
        if (scheduled != null) {
            String runId = attributes.getWorkflowExecution().getRunId();
            Settable<StartChildWorkflowReplyresult = scheduled.getResult();
            if (!result.isReady()) {
                String description = "startChildWorkflow workflowId=" + workflowId + ", runId=" + runId;
                result.set(new StartChildWorkflowReplyImpl(runIddescription));
            }
        }
    }
        WorkflowExecution execution = attributes.getWorkflowExecution();
        String workflowId = execution.getWorkflowId();
        if (.handleChildWorkflowExecutionClosed(workflowId)) {
            OpenRequestInfo<StartChildWorkflowReplyWorkflowTypescheduled = .remove(workflowId);
            if (scheduled != null) {
                Exception failure = new ChildWorkflowTimedOutException(event.getEventId(), execution,
                        attributes.getWorkflowType());
                ExternalTaskCompletionHandle context = scheduled.getCompletionHandle();
                context.fail(failure);
            }
        }
    }
        WorkflowExecution execution = attributes.getWorkflowExecution();
        String workflowId = execution.getWorkflowId();
        if (.handleChildWorkflowExecutionClosed(workflowId)) {
            OpenRequestInfo<StartChildWorkflowReplyWorkflowTypescheduled = .remove(workflowId);
            if (scheduled != null) {
                Exception failure = new ChildWorkflowTerminatedException(event.getEventId(), execution,
                        attributes.getWorkflowType());
                ExternalTaskCompletionHandle context = scheduled.getCompletionHandle();
                context.fail(failure);
            }
        }
    }
        String workflowId = attributes.getWorkflowId();
            OpenRequestInfo<StartChildWorkflowReplyWorkflowTypescheduled = .remove(workflowId);
            if (scheduled != null) {
                WorkflowExecution workflowExecution = new WorkflowExecution();
                workflowExecution.setWorkflowId(workflowId);
                WorkflowType workflowType = attributes.getWorkflowType();
                String cause = attributes.getCause();
                Exception failure = new StartChildWorkflowFailedException(event.getEventId(), workflowExecutionworkflowType,
                        cause);
                ExternalTaskCompletionHandle context = scheduled.getCompletionHandle();
                context.fail(failure);
            }
        }
    }
        WorkflowExecution execution = attributes.getWorkflowExecution();
        String workflowId = execution.getWorkflowId();
        if (.handleChildWorkflowExecutionClosed(workflowId)) {
            OpenRequestInfo<StartChildWorkflowReplyWorkflowTypescheduled = .remove(workflowId);
            if (scheduled != null) {
                String reason = attributes.getReason();
                String details = attributes.getDetails();
                Exception failure = new ChildWorkflowFailedException(event.getEventId(), executionattributes.getWorkflowType(),
                        reasondetails);
                ExternalTaskCompletionHandle context = scheduled.getCompletionHandle();
                context.fail(failure);
            }
        }
    }
        WorkflowExecution execution = attributes.getWorkflowExecution();
        String workflowId = execution.getWorkflowId();
        if (.handleChildWorkflowExecutionClosed(workflowId)) {
            OpenRequestInfo<StartChildWorkflowReplyWorkflowTypescheduled = .remove(workflowId);
            if (scheduled != null) {
                ExternalTaskCompletionHandle context = scheduled.getCompletionHandle();
                String result = attributes.getResult();
                StartChildWorkflowReplyImpl startedReply = (StartChildWorkflowReplyImplscheduled.getResult().get();
                startedReply.setResult(result);
                context.complete();
            }
        }
    }
        String signalId = attributes.getControl();
            OpenRequestInfo<VoidVoidsignalContextAndResult = .remove(signalId);
            if (signalContextAndResult != null) {
                WorkflowExecution signaledExecution = new WorkflowExecution();
                signaledExecution.setWorkflowId(attributes.getWorkflowId());
                signaledExecution.setRunId(attributes.getRunId());
                Throwable failure = new SignalExternalWorkflowException(event.getEventId(), signaledExecution,
                        attributes.getCause());
                signalContextAndResult.getCompletionHandle().fail(failure);
            }
        }
    }
        if (.handleExternalWorkflowExecutionSignaled(signalId)) {
            OpenRequestInfo<VoidVoidsignalContextAndResult = .remove(signalId);
            if (signalContextAndResult != null) {
                signalContextAndResult.getResult().set(null);
                signalContextAndResult.getCompletionHandle().complete();
            }
        }
    }
New to GrepCode? Check out our FAQ X