public Promise startWorkflowExecution()

in src/main/java/com/amazonaws/services/simpleworkflow/flow/DynamicWorkflowClientImpl.java [180:277]


    public <T> Promise<T> startWorkflowExecution(final Object[] arguments, final StartWorkflowOptions startOptionsOverride,
            final Class<T> returnType, Promise<?>... waitFor) {
        checkState();
        final Settable<T> result = new Settable<T>();
        if (runId.isReady()) {
            runId = new Settable<String>();
            workflowExecution.setRunId(null);
        }
        new TryFinally(waitFor) {

            Promise<StartChildWorkflowReply> reply;

            @Override
            protected void doTry() throws Throwable {
                StartChildWorkflowExecutionParameters parameters = new StartChildWorkflowExecutionParameters();
                parameters.setWorkflowType(workflowType);
                final String convertedArguments = ThreadLocalMetrics.getMetrics().recordSupplier(
                    () -> dataConverter.toData(arguments),
                    dataConverter.getClass().getSimpleName() + "@" + MetricName.Operation.DATA_CONVERTER_SERIALIZE.getName(),
                    TimeUnit.MILLISECONDS
                );
                parameters.setInput(convertedArguments);
                if (!startAttempted) {
                    parameters.setWorkflowId(workflowExecution.getWorkflowId());
                    requestedWorkflowId = workflowExecution.getWorkflowId();
                    startAttempted = true;
                } else {
                    // Subsequent attempts (e.g. on retry) use the same workflow id as the initial attempt
                    parameters.setWorkflowId(requestedWorkflowId);
                    workflowExecution.setWorkflowId(requestedWorkflowId);
                }
                final StartChildWorkflowExecutionParameters startParameters = parameters.createStartChildWorkflowExecutionParametersFromOptions(
                        schedulingOptions, startOptionsOverride);
                GenericWorkflowClient client = getGenericClientToUse();
                reply = client.startChildWorkflow(startParameters);
                runId.setDescription("runId of " + reply.getDescription());
                result.setDescription(reply.getDescription());
                new Task(reply) {

                    @Override
                    protected void doExecute() throws Throwable {
                        StartChildWorkflowReply r = reply.get();
                        if (!runId.isReady()) {
                            runId.set(r.getRunId());
                            workflowExecution.setRunId(r.getRunId());
                            workflowExecution.setWorkflowId(r.getWorkflowId());
                        }
                    }
                };
            }

            @Override
            protected void doCatch(Throwable e) throws Throwable {
                if (e instanceof ChildWorkflowFailedException) {
                    ChildWorkflowFailedException taskFailedException = (ChildWorkflowFailedException) e;
                    try {
                        String details = taskFailedException.getDetails();
                        if (details != null) {
                            final Throwable cause = ThreadLocalMetrics.getMetrics().recordSupplier(
                                () -> dataConverter.fromData(details, Throwable.class),
                                dataConverter.getClass().getSimpleName() + "@" + MetricName.Operation.DATA_CONVERTER_DESERIALIZE.getName(),
                                TimeUnit.MILLISECONDS
                            );
                            if (cause != null && taskFailedException.getCause() == null) {
                                taskFailedException.initCause(cause);
                            }
                        }
                    }
                    catch (DataConverterException dataConverterException) {
                        if (dataConverterException.getCause() == null) {
                            dataConverterException.initCause(taskFailedException);
                        }
                        throw dataConverterException;
                    }
                }

                throw e;
            }

            @Override
            protected void doFinally() throws Throwable {
                if (reply != null && reply.isReady() && reply.get().getResult().isReady()) {
                    if (returnType.equals(Void.class)) {
                        result.set(null);
                    }
                    else {
                        final T output = ThreadLocalMetrics.getMetrics().recordSupplier(
                            () -> dataConverter.fromData(reply.get().getResult().get(), returnType),
                            dataConverter.getClass().getSimpleName() + "@" + MetricName.Operation.DATA_CONVERTER_DESERIALIZE.getName(),
                            TimeUnit.MILLISECONDS
                        );
                        result.set(output);
                    }
                }
            }
        };
        return result;
    }