Help Center/ FunctionGraph/ Developer Guide/ Java/ Function Development Overview
Updated on 2025-12-10 GMT+08:00

Function Development Overview

FunctionGraph supports the following Java runtimes:

  • Java 8
  • Java 11
  • Java 17
  • Java 21 (only available in ME-Riyadh and TR-Istanbul)

Function Syntax

Java function syntax: 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.

The Java function handler is in the format of [package name].[class name].[function name]. Configure or modify handler parameters by referring to function handler.

Java Initializer

For details about the initializer, see Initializer.

Initializer format: [Package name].[Class name].[Execution function name]

For example, if the initializer is named com.huawei.Demo.my_initializer, FunctionGraph loads the my_initializer function defined in the com.huawei 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.

  • 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-2.0.5.sha256) provides context, event, and logging APIs.

  • Event APIs

    Event structure definitions are added to the Java SDK. Currently, DMS, DIS, SMN, timer, APIG, and Kafka triggers are supported. The definitions make coding much simpler when triggers are required.

    1. APIG trigger
      • APIGTriggerEvent methods
        Table 1 APIGTriggerEvent methods

        Method

        Description

        isBase64Encoded()

        Checks whether the body of an event is encoded using Base64.

        getHttpMethod()

        Obtains the HTTP request method.

        getPath()

        Obtains the HTTP request path.

        getBody()

        Obtains the HTTP request body.

        getPathParameters()

        Obtains all path parameters.

        getRequestContext()

        Obtains APIG configuration information. (The APIGRequestContext object is returned.)

        getHeaders()

        Obtains the HTTP request header.

        getQueryStringParameters()

        Obtains query parameters.

        The value of a query parameter cannot be an array. To support an array, customize the corresponding event structure.

        getRawBody()

        Obtains the content before Base64 encoding.

        getUserData()

        Obtains the user data set in the APIG custom authorizer.

        Table 2 APIGRequestContext methods

        Method

        Description

        getApiId()

        Obtains the API ID.

        getRequestId()

        Obtains the request ID of an API request.

        getStage()

        Obtains the name of the environment in which an API has been published.

        getSourceIp()

        Obtains the source IP address in the APIG custom authorizer.

      • APIGTriggerResponse methods
        Table 3 APIGTriggerResponse construction methods

        Method

        Description

        APIGTriggerResponse()

        Set the following parameters: headers: null statusCode: 200 body: "" isBase64Encoded: false

        APIGTriggerResponse(statusCode, headers, body)

        Set the value of isBase64Encoded to false, and use the input values of other parameters.

        APIGTriggerResponse(statusCode, headers, isBase64Encoded, body)

        Set the parameters in sequence.

        Table 4 APIGTriggerResponse methods

        Method

        Description

        setBody(String body)

        Sets the message body.

        setHeaders(Map<String,String> headers)

        Sets the HTTP response header to be returned.

        setStatusCode(int statusCode)

        Sets the HTTP status code.

        setBase64Encoded(boolean isBase64Encoded)

        Configures Base64 encoding for the response body.

        setBase64EncodedBody(String body)

        Encodes the input with Base64 and configures it in the body.

        addHeader(String key, String value)

        Adds a group of HTTP headers.

        removeHeader(String key)

        Removes the specified header.

        addHeaders(Map<String,String> headers)

        Adds multiple headers.

        APIGTriggerResponse methods have the headers attribute, which can be initialized using the setHeaders method or a constructor function with the headers parameter.

    2. DIS trigger
      Table 5 DISTriggerEvent methods

      Method

      Description

      getShardID()

      Obtains the partition ID.

      getMessage()

      Obtains the DIS message body (DISMessage structure).

      getTag()

      Obtains the version of a function.

      getStreamName()

      Obtains the stream name.

      Table 6 DISMessage methods

      Method

      Description

      getNextPatitionCursor()

      Obtains the next partition cursor.

      getRecords()

      Obtains message records (DISRecord structure).

      getMillisBehindLatest()

      Reserved (Currently, 0 is returned.)

      Table 7 DISRecord methods

      Method

      Description

      getPartitionKey()

      Obtains the data partition.

      getData()

      Obtains data.

      getRawData()

      Obtains UTF-8 data strings decoded using Base64.

      getSequenceNumber()

      Obtains the sequence number (ID of each record).

    3. DMS trigger
      Table 8 DMSTriggerEvent methods

      Method

      Description

      getQueueId()

      Obtains the queue ID.

      getRegion()

      Obtains the region name.

      getEventType()

      Obtains the event type. ("MessageCreated" is returned.)

      getConsumerGroupId()

      Obtains the consumer group ID.

      getMessages()

      Obtains DMS messages (DMSMessage structure).

      Table 9 DMSMessage methods

      Method

      Description

      getBody()

      Obtains the DMS message body.

      getAttributes()

      Obtains the message attribute set.

    4. SMN trigger
      Table 10 SMNTriggerEvent method

      Method

      Description

      getRecord()

      Obtains the message records (SMNRecord structure).

      Table 11 SMNRecord methods

      Method

      Description

      getEventVersion()

      Obtains event version. (The current version is 1.0.)

      getEventSubscriptionUrn()

      Obtains the subscription Uniform Resource Name (URN).

      getEventSource()

      Obtains the event source.

      getSmn()

      Obtains the message body (SMNBody structure).

      Table 12 SMNBody methods

      Method

      Description

      getTopicUrn()

      Obtains the topic URN.

      getTimeStamp()

      Obtains the timestamp of a message.

      getMessageAtrributes()

      Obtains the message attribute set.

      getMessage()

      Obtains the message body.

      getType()

      Obtains the message type.

      getMessageId()

      Obtains the message ID.

      getSubject()

      Obtains the message topic.

    5. Timer trigger
      Table 13 TimerTriggerEvent methods

      Method

      Description

      getVersion()

      Obtains the version name. (The current version is 1.0.)

      getTime()

      Obtains the current time.

      getTriggerType()

      Obtains trigger type (Timer).

      getTriggerName()

      Obtains the trigger name.

      getUserEvent()

      Obtains the additional information of the trigger.

    6. Kafka trigger
      Table 14 Kafka trigger

      Method

      Description

      getEventVersion

      Obtains event version.

      getRegion

      Obtains region information.

      getEventTime

      Obtains event time.

      getTriggerType

      Obtains trigger type

      getInstanceId

      Obtains the instance ID.

      getRecords

      Obtains record.

    1. When using an APIG trigger, set the first parameter of the handler function (for example, handler) to handler(APIGTriggerEvent event, Context context).
    2. The preceding TriggerEvent methods have corresponding set methods, which are recommended for local debugging. DIS and LTS triggers have getRawData() methods, but do not have setRawData() methods.
  • 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 15 describes the context APIs provided by FunctionGraph.

    Table 15 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.

    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.

    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. The cache duration is 10 minutes. That is, the same content is returned within 10 minutes. To use this method, you need to configure an agency for the function.

    getSecuritySecretKey( )

    Obtains the SecuritySecretKey (valid for 24 hours) with an agency. The cache duration is 10 minutes. That is, the same content is returned within 10 minutes. To use this method, you need to configure an agency for the function.

    getSecurityToken( )

    Obtains the SecurityToken (valid for 24 hours) with an agency. The cache duration is 10 minutes. That is, the same content is returned within 10 minutes. To 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( )

    Obtains CPU usage of a function.

    getPackage( )

    Obtains a function group.

    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.

  • Logging APIs

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

    Table 16 Logging API

    Method

    Description

    RuntimeLogger()

    Records user input logs. Method: log(String string).