开发者社区 > 云原生 > Serverless > 正文

有Serverless 工作流的Java SDK 使用示例吗?

有Serverless 工作流的Java SDK 使用示例吗?

展开
收起
小天使爱美 2020-03-27 11:20:31 1325 0
1 条回答
写回答
取消 提交回答
  • 本文介绍使用 Java SDK 的详细流程,包括环境要求、安装依赖和快速使用三部分。

    环境要求 要使用 Alibaba Cloud SDK for Java,您需要一个云账号以及一对 Access Key ID 和 Access Key Secret。 请在阿里云控制台中的AccessKey 管理页面上创建和查看您的 AccessKey,或者联系您的系统管理员。 要使用 Alibaba Cloud SDK for Java 访问某个产品的 API,您需要事先在阿里云控制台中开通这个产品。 Alibaba Cloud SDK for Java 需要 1.6 以上的 JDK。 安装依赖 无论您要使用哪个产品的开发工具包,都必须安装 aliyun-java-sdk-core。例如,对 Serverless 工作流 SDK 的调用,您需要安装aliyun-java-sdk-core 和 aliyun-java-sdk-fnf。

    通过 Maven 来管理项目依赖(推荐)

    如果您使用 Apache Maven 来管理 Java 项目,只需在项目的 pom.xml 文件加入相应的依赖项即可。

    com.aliyun aliyun-java-sdk-core [4.3.2,5.0.0) com.aliyun aliyun-java-sdk-fnf [1.0.0,5.0.0) 如果 Maven 没有从中央存储库下载 JAR 包,则需要将此依赖项添加到pom.xml文件中,否则将报告 NoClassDefFoundError 异常。

    com.google.code.gson gson 2.8.5
    快速使用 下文将以创建一个流程,发起一次执行并获取执行详情为例展示如何使用 Java SDK 调用 Serverless 工作流服务。

    调用 Alibaba Cloud SDK for Java 的 3 个主要步骤:

    创建 DefaultAcsClient 实例并初始化。 创建 API 请求并设置参数。 发起请求并处理应答或异常。 注意 下文仅提供 Serverless 工作流产品的使用流程,如果您在使用过程中遇到调试等问题或希望使用高级功能(连接池、HTTPS、代理、日志)等功能,请参见 README-CN。 请求方式

    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);
    }
    

    }
    创建客户端并利用上述函数发起一系列调用

    说明 如果您需要不加改造进行调试的话,请将下述 public 类与上述“请求方式”代码块置于同一个文件中,避免在 import 时报错。 public class Main { public static void main(String[] args) { // Create DefaultAcsClient DefaultProfile profile = DefaultProfile.getProfile( " ", // 地域ID " ", // RAM 账号的AccessKey ID " "); // RAM 账号Access Key Secret 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();
        }
    }
    

    }

    2020-03-27 11:21:06
    赞同 展开评论 打赏

快速交付实现商业价值。

相关产品

  • 函数计算
  • 相关电子书

    更多
    Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
    The Reactive Cloud Native Arch 立即下载
    JAVA开发手册1.5.0 立即下载