All Products
Search
Document Center

Function Compute (2.0):Manage stateful asynchronous invocations

Last Updated:Jun 19, 2023

This topic describes how to use the SDK for Java to manage stateful asynchronous invocations for functions.

SDK sample code

package com.mycompany.FcSample;

import com.aliyuncs.fc.client.FunctionComputeClient;

import com.aliyuncs.fc.request.*;
import com.aliyuncs.fc.response.*;

import com.aliyuncs.fc.constants.Const;

import com.aliyuncs.fc.model.*;

import java.io.IOException;


public class FcSample {
    private static final String REGION = "cn-hangzhou";

    public static void main(final String[] args) throws IOException {
        /*
        The AccessKey pair of an Alibaba Cloud account can be used to access all API operations. Using these credentials to perform operations in Function Compute is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. 
        We recommend that you do not save the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account may be compromised. 
        In this example, the AccessKey pair is saved to the environment variables for authentication. 
        Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables in your local environment before you run the sample code. 
        The ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are automatically configured after you configure the execution permissions in the runtime of Function Compute. 
        */
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessSecretKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        String accountId = System.getenv("ACCOUNT_ID");

        // Initialize the client. 
        FunctionComputeClient fcClient = new FunctionComputeClient(REGION, accountId, accessKey, accessSecretKey);

        // Specify the endpoint of the client in the format of http://{accountId}.{regionId}.fc.aliyuncs.com. 

        // Publish an asynchronous invocation configuration. 
        AsyncConfig config = new AsyncConfig();
        config.setStatefulInvocation(true);

        String invocationID = "ivk-id";
        String SERVICE_NAME = "";
        String FUNCTION_NAME = "";
        PutFunctionAsyncConfigRequest putFunctionAsyncConfigRequest = new PutFunctionAsyncConfigRequest(SERVICE_NAME, "", FUNCTION_NAME);
        putFunctionAsyncConfigRequest.setAsyncConfig(config);
        PutFunctionAsyncConfigResponse pResp = fcClient.putFunctionAsyncConfig(putFunctionAsyncConfigRequest);

        // Asynchronously invoke the function. 
        // Take note of the request header passed in the setHeader() method. The setHeader() method is used to set the header metadata of the response. 
        InvokeFunctionRequest request = new InvokeFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
        request.setHeader("x-fc-invocation-type", Const.INVOCATION_TYPE_ASYNC);
        request.setStatefulAsyncInvocationId(invocationID);
        InvokeFunctionResponse ivkResp = fcClient.invokeFunction(request);

        // Query the information about a stateful asynchronous invocation. 
        GetStatefulAsyncInvocationRequest req = new GetStatefulAsyncInvocationRequest(SERVICE_NAME, "", FUNCTION_NAME, invocationID);
        GetStatefulAsyncInvocationResponse resp = fcClient.getStatefulAsyncInvocation(req);

        // Query stateful asynchronous invocations. 
        ListStatefulAsyncInvocationsRequest lReq = new ListStatefulAsyncInvocationsRequest(SERVICE_NAME, FUNCTION_NAME);
        lReq.setInvocationIdPrefix("stateful-invocationId");
        lReq.setIncludePayload(true);
        lReq.setLimit(100);
        ListStatefulAsyncInvocationsResponse lResp = fcClient.listStatefulAsyncInvocations(lReq);

        // Terminate a stateful asynchronous invocation. 
        StopStatefulAsyncInvocationRequest sReq = new StopStatefulAsyncInvocationRequest(SERVICE_NAME, "", FUNCTION_NAME, invocationID);
        StopStatefulAsyncInvocationResponse sResp = new StopStatefulAsyncInvocationResponse();
        sResp = fcClient.stopStatefulAsyncInvocation(sReq);
    }
}