在 AWS Step 函数中获取状态机的所有失败执行并动态执行 在Java中

问题描述

我想获取所有失败的执行并需要动态重新触发它们。

PS:在 stepfunction 定义中,我有适当的重试机制,现在我想动态地重新运行失败的执行。

我需要在java中实现它。请帮我解决这个问题。

提前致谢。

解决方法

您可以使用 AWS Step Functions API 获取执行列表:

https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/sfn/SfnClient.html#listExecutions-

然后您可以通过调用属于 ListExecutionsResponse 对象的executions() 方法(由listExecutions 方法) https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/sfn/model/ExecutionListItem.html

使用这个对象 - 你可以做两件事:

1 - 检查状态 - https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/sfn/model/ExecutionStatus.html

2 - 获取状态机 ARN 值 - https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/sfn/model/ExecutionListItem.html#stateMachineArn--

使用状态机 ARN 值,您可以使用 AWS Step Functions Java API V2 执行状态机:

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sfn.SfnClient;
import software.amazon.awssdk.services.sfn.model.*;
import java.io.FileReader;
import java.io.IOException;
import java.util.UUID;
// snippet-end:[stepfunctions.java2.start_execute.import]

public class StartExecution {

       public static void main(String[] args) {
            final String USAGE = "\n" +
                    "Usage:\n" +
                    "    StartExecution <stateMachineArn> <jsonFile>\n\n" +
                    "Where:\n" +
                    "    stateMachineArn - the ARN of the state machine.\n\n" +
                    "    jsonFile - A JSON file that contains the values to pass to the worflow.\n" ;

            if (args.length != 2) {
                System.out.println(USAGE);
                System.exit(1);
            }

            String stateMachineArn = args[0];
            String jsonFile = args[1];
            Region region = Region.US_EAST_1;
            SfnClient sfnClient = SfnClient.builder()
                    .region(region)
                    .build();

           String exeArn = startWorkflow(sfnClient,stateMachineArn,jsonFile);
           System.out.println("The execution ARN is" +exeArn);
           sfnClient.close();
        }

        // snippet-start:[stepfunctions.java2.start_execute.main]
        public static String startWorkflow(SfnClient sfnClient,String stateMachineArn,String jsonFile) {

            String json = getJSONString(jsonFile);

            // Specify the name of the execution by using a GUID value.
            UUID uuid = UUID.randomUUID();
            String uuidValue = uuid.toString();
            try {

                StartExecutionRequest executionRequest = StartExecutionRequest.builder()
                        .input(json)
                        .stateMachineArn(stateMachineArn)
                        .name(uuidValue)
                        .build();

                StartExecutionResponse response = sfnClient.startExecution(executionRequest);
                return response.executionArn();


            } catch (SfnException e) {
                System.err.println(e.awsErrorDetails().errorMessage());
                System.exit(1);
            }
            return "";
        }

    private static String getJSONString(String path) {

        try {
            JSONParser parser = new JSONParser();
            JSONObject data = (JSONObject) parser.parse(new FileReader(path));//path to the JSON file.
            String json = data.toJSONString();
            return json;
        } catch (IOException |  org.json.simple.parser.ParseException e) {
            e.printStackTrace();
        }
        return "";
   }
    // snippet-end:[stepfunctions.java2.start_execute.main]
 }