Updated on 2023-12-29 GMT+08:00

Developing Functions in Java (Using Eclipse)

Function Syntax

The following is the syntax for creating a handler function in Java:

Scope Return parameter Function name (User-defined parameter, Context)

  • Scope: It must be defined as public for the function that FunctionGraph invokes to execute your code.
  • Return parameter: user-defined output, which is converted into a character string and returned as an HTTP response. The HTTP response is a JSON string.
  • Function name: user-defined function name.
  • User-defined parameter: FunctionGraph supports only one user-defined parameter. For complex parameters, define them as an object and provide data through JSON strings. When invoking a function, FunctionGraph parses the JSON strings as an object.
  • Context: runtime information provided for executing the function. For details, see SDK APIs.

When creating a function in Java, define a handler in the format of [Package name].[Class name].[Function name].

Java Initializer

FunctionGraph supports the following Java runtime:

  • Java 8 (runtime = Java8)

    Initializer syntax:

    [Package name].[Class name].[Execution function name]

    For example, if the initializer is named com.Demo.my_initializer, FunctionGraph loads the my_initializer function defined in the com file.

    To use Java to build initialization logic, define a Java function as the initializer. The following is a simple initializer:

    public void my_initializer(Context context)
    {
    RuntimeLogger log = context.getLogger();
    log.log(String.format("ak:%s", context.getAccessKey()));
    }
  • Function Name

    The function name my_initializer must be the initializer function name specified for a function.

    For example, if the initializer is named com.Demo.my_initializer, FunctionGraph loads the my_initializer function defined in the com file.

  • context

    The context parameter contains the runtime information about a function. For example, request ID, temporary AK, and function metadata.

SDK APIs

The Java SDK (verification file: fss-java-sdk-sha256) provides context and logging APIs.

  • Context APIs

    The context APIs are used to obtain the context, such as agency AK/SK, current request ID, allocated memory space, and number of CPUs, required for executing a function.

    Table 1 describes the context APIs provided by FunctionGraph.

    Table 1 Context methods

    Method

    Description

    getRequestID( )

    Obtains a request ID.

    getRemainingTimeInMilliSeconds ( )

    Obtains the remaining running time of a function.

    getAccessKey( )

    Obtains the AK (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    NOTE:

    FunctionGraph has stopped maintaining the getAccessKey API in the Runtime SDK. You cannot use this API to obtain a temporary AK.

    getSecretKey( )

    Obtains the SK (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    NOTE:

    FunctionGraph has stopped maintaining the getSecretKey API in the Runtime SDK. You cannot use this API to obtain a temporary SK.

    getSecurityAccessKey( )

    Obtains the SecurityAccessKey (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    getSecuritySecretKey( )

    Obtains the SecuritySecretKey (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    getSecurityToken( )

    Obtains the SecurityToken (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    getUserData(string key)

    Uses keys to obtain the values passed by environment variables.

    getFunctionName( )

    Obtains the name of a function.

    getRunningTimeInSeconds ( )

    Obtains the timeout of a function.

    getVersion( )

    Obtains the version of a function.

    getMemorySize( )

    Obtains the allocated memory.

    getCPUNumber( )

    CPU usage of a function.

    getPackage( )

    Obtains a function group, that is, an app.

    getToken( )

    Obtains the token (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function.

    getLogger( )

    Obtains the logger method provided by the context. By default, information such as the time and request ID is output.

    getAlias

    Obtains function alias.

    Results returned by using the getToken(), getAccessKey(), and getSecretKey() methods contain sensitive information. Exercise caution when using these methods.

  • Logging API

    Table 2 describes the logging API provided in the Java SDK.

    Table 2 Logging API

    Method

    Description

    RuntimeLogger()

    Records user input logs using the method log(String string).

Execution Result

The execution result consists of the function output, summary, and log output.

Table 3 Description of the execution result

Parameter

Successful Execution

Failed Execution

Function Output

The defined function output information is returned.

A JSON file that contains errorMessage and stackTrace is returned. The format is as follows:

{
  "errorMessage": "",
  "stackTrace": []
}

errorMessage: Error message returned by the runtime.

stackTrace: Stack error information returned by the runtime.

Summary

Request ID, Memory Configured, Execution Duration, Memory Used, and Billed Duration are displayed.

Request ID, Memory Configured, Execution Duration, Memory Used, and Billed Duration are displayed.

Log Output

Function logs are printed. A maximum of 4 KB logs can be displayed.

Error information is printed. A maximum of 4 KB logs can be displayed.