This topic describes how to use SDK for Java.

Prerequisites

  • You must have an Alibaba Cloud account and an AccessKey pair (AccessKey ID and AccessKey secret) to use Alibaba Cloud SDK for Java. You can log on to the Security Management page to create or view the AccessKey pair. You can also contact your system administrator to obtain the AccessKey pair.
  • To use Alibaba Cloud SDK to access API operations of a service, you must activate this service in the Alibaba Cloud Management Console.
  • Alibaba Cloud SDK for Java requires JDK 1.6 or later.

Dependency installation

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

(Recommended) Add dependencies by using Maven

If you use Apache Maven to manage Java projects, you only need to add related 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 Maven does not download the JAR package from the central repository, you need to add this dependency to the pom.xml file. Otherwise, NoClassDefFoundError is reported.

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

Quick start

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

Perform the following steps to call Alibaba Cloud SDK for Java:

  1. Create a DefaultAcsClient instance and initialize the instance.
  2. Create an API request and configure parameters.
  3. Initiate the API request and handle the response or exception.
Notice The following example describes how to use Serverless workflow. When you use 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-CN.

Request method

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 {
        CreateFlowRequest request = new CreateFlowRequest();
        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 {
        DescribeExecutionRequest request = new DescribeExecutionRequest();
        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 and use the preceding functions to initiate a series of calls.

Note If you need to implement debugging without modification, place the following public class in the same file as the preceding request method code block to avoid errors upon import.
public class Main {
    public static void main(String[] args) {
        // Create DefaultAcsClient
        DefaultProfile profile = DefaultProfile.getProfile(
                "<your-region-id>",          // The region ID
                "<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 Flow
            CreateFlowResponse creatFlowResponse = FnFOperations.createFlow(client);
            System.out.println(creatFlowResponse);
            // Start Execution
            StartExecutionResponse startExeResp = FnFOperations.startExecution(client);
            System.out.println(startExeResp);
            // Describe 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();
        }
    }
}