The following is the syntax for creating a handler function in Java:
Scope Return parameter Function name (User-defined parameter, Context)
When creating a function in Java, define a handler in the format of [Package name].[Class name].[Function name].
FunctionGraph supports the following Java runtime:
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.Demo 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()));
}
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.Demo file.
The context parameter contains the runtime information about a function. For example, request ID, temporary AK, and function metadata.
The Java SDK provides context, and logging 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.
Method | Description |
|---|---|
getRequestID( ) | Obtains a request ID. |
getRemainingTimeInMilligetRunningTimeInSecondsSeconds ( ) | Obtains the remaining running time of a function. |
getAccessKey( ) | Obtains the AK (valid for 24 hours) of 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) of 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( ) | Obtains CPU usage of a function. |
getProjectID( ) | Obtains a project ID. |
getPackage( ) | Obtains a function group, that is, an app. |
getToken( ) | Obtains the token (valid for 24 hours) of 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. |
Results returned by using the getToken(), getAccessKey(), and getSecretKey() methods contain sensitive information. Exercise caution when using these methods.
Table 2 describes the logging API provided in the Java SDK.
Method | Description |
|---|---|
RuntimeLogger() | Records user input logs using the method log(String string). |
Perform the following procedure to develop a Java function:
Figure 1 Creating a project

Download the to a local development environment, and decompress the SDK package, as shown in Figure 2.
Figure 2 Decompressing the downloaded SDK

Create a folder named lib in the project directory, copy the Runtime-1.1.3.jar file in the SDK package to the lib folder, and add the JAR file as a dependency of the project, as shown in Figure 3.
Figure 3 Configuring the dependency

Figure 4 Creating a package named com.demo

1234567891011121314151617181920212223242526package com.demo;import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.Map;import com.services.runtime.Context;import com.services.runtime.entity.smn.SMNTriggerEvent;import com.services.runtime.entity.timer.TimerTriggerEvent;public class TriggerTests {public String smnTest(SMNTriggerEvent event, Context context){System.out.println(event);return "ok";}}public String timerTest(TimerTriggerEvent event, Context context){System.out.println(event);return "ok";}}
Figure 5 Packaging the project files

Figure 6 Selecting a format

Figure 7 Specifying the destination path

Select Timer and click Save.
The function execution result consists of three parts: function output (returned by callback), summary, and logs (output by using the console.log or getLogger() method).
Change the handler to com.demo.TriggerTests.smnTest and change the event template to smn-event-template, and execute the function.
Create a Person class in the project, as shown in Figure 8.
Figure 8 Creating a Person class

Create a class named PersonTest.java, and add a handler function to the class, as shown in Figure 9.
Figure 9 Creating a class named PersonTest.java

After exporting the new package, upload it to the function, change the function handler to com.demo.PersonTest.personTest, and click Save.
In the Configure Test Event dialog box, select blank-template, enter a test event, and click Save.
Click Create, and then click Test.
The execution result consists of the function output, summary, and log output.
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: 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. |