This topic describes how to use Alibaba Cloud SDK for Java.

Prerequisites

  • An Alibaba Cloud account and an AccessKey pair are created. An AccessKey pair contains an AccessKey ID and an AccessKey secret. You can go to the Security Management page of the Alibaba Cloud Management Console to create or view AccessKey pairs. You can also contact your system administrator to obtain an AccessKey pair.
  • Serverless Workflow is activated in the Alibaba Cloud Management Console. This allows you to use Alibaba Cloud SDK for Java to call the API operations of Serverless Workflow.
  • Java Development Kit (JDK) 1.6 or later is installed.

Dependency installation

You must install aliyun-java-sdk-core to use the SDK of an Alibaba Cloud service. For example, to use Serverless workflow SDK, you must install aliyun-java-sdk-core and aliyun-java-sdk-fnf.

We recommend that you use Apache Maven to manage project dependencies. If you use Apache Maven to manage a Java project, add the following dependencies to the pom.xml file of the project:

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>[4.3.2,5.0.0)</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-fnf</artifactId>
    <version>[1.0.0,5.0.0)</version>
</dependency>

If you do not use Apache Maven to download the JAR package from the central repository, add the following dependency to the pom.xml file. Otherwise, a NoClassDefFoundError error is reported.

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>        

Quick start

The following example shows you how to use Alibaba Cloud SDK for Java to call Serverless workflow. In this example, a flow is created, an execution is initiated, and the execution details are queried.

To call Alibaba Cloud SDK for Java, perform the following steps:

  1. Create a DefaultAcsClient object and initialize the object.
  2. Create an API request and set the required parameters.
  3. Initiate the API request and process the response or exceptions.
Notice The following example describes how to use Alibaba Cloud SDK for Java to call Serverless workflow. When you call the service, you may encounter a debugging issue or want to use advanced features such as the connection pool, HTTPS, proxy, and log features. For more information, see README.

The following code provides an example on how to create an API request:

package com.test;

import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.fnf.model.v20190315.*;

class FnFOperations {
    static String flowName = "xxx";
    static String execName = "xxx";
    static String flowDesc = "xxx";
    static String flowDef = "xxx";
    static String roleArn = "xxx";
    static String flowType = "xxx";

    static CreateFlowResponse createFlow(IAcsClient fnfClient) throws ClientException {
        CreateUserRequest request = new CreateUserRequest();
        request.setName(flowName);
        request.setDefinition(flowDef);
        request.setType(flowType);
        request.setDescription(flowDesc);
        request.setRoleArn(roleArn);
        return fnfClient.getAcsResponse(request);
    }

    static StartExecutionResponse startExecution(IAcsClient fnfClient) throws ClientException {
        StartExecutionRequest request = new StartExecutionRequest();
        request.setFlowName(flowName);
        request.setExecutionName(execName);
        return fnfClient.getAcsResponse(request);
    }

    static DescribeExecutionResponse describeExecution(IAcsClient fnfClient) throws ClientException {
        DescribeInvocationsRequest request = new DescribeInvocationsRequest();
        request.setFlowName(flowName);
        request.setExecutionName(execName);
        return fnfClient.getAcsResponse(request);
    }

    static GetExecutionHistoryResponse getExecutionHistory(IAcsClient fnfClient) throws ClientException {
        GetExecutionHistoryRequest request = new GetExecutionHistoryRequest();
        request.setFlowName(flowName);
        request.setExecutionName(execName);
        return fnfClient.getAcsResponse(request);
    }
}            

Create a client object and use the preceding functions to initiate a series of requests.

Note If you need to perform debugging without modification, place the following public class in the same file as the preceding code block that is used to create an API request. This helps prevent errors upon import.
public class Main {
    public static void main(String[] args) {
        // Create a DefaultAcsClient object.
        DefaultProfile profile = DefaultProfile.getProfile(
                "<your-region-id>",          // The ID of the region. 
                "<your-access-key-id>", // The AccessKey ID of the RAM user. 
                "<your-access-key-secret>"); //The AccessKey secret of the RAM user. 
        IAcsClient client = new DefaultAcsClient(profile);

        try {
            // Create a flow.
            CreateFlowResponse createFlowResponse = FnFOperations.createFlow(client);
            System.out.println(creatFlowResponse);
            // Initiate an execution.
            StartExecutionResponse startExeResp = FnFOperations.startExecution(client);
            System.out.println(startExeResp);
            // Query the details of the execution.
            DescribeExecutionResponse descExeResp = FnFOperations.describeExecution(client);
            System.out.println(descExeResp);
        } catch (ClientException e) {
            e.printStackTrace();
        }

        try {
            GetExecutionHistoryResponse resp = FnFOperations.getExecutionHistory(client);
            for (GetExecutionHistoryResponse.EventsItem event:resp.getEvents()) {
                System.out.printf("event %s status: %s%n", event.getStepName(), event.getType());
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}