An initializer function is used to ensure certain business logic to be successfully executed on a Docker instance only once. This topic describes the structure and features of Java initializer functions.

Predefined API operation

If you need to add an initializer to a Java runtime environment, you must additionally implement the predefined API operation for initializer functions, regardless of whether your function uses the Stream type for input or use the Pojo type for custom input and output.

The following shows the predefined API operation for Initializer function.

package com.aliyun.fc.runtime;

import java.io.IOException;
public interface FunctionInitializer {

    /**
     * The interface to handle a function compute initialize request
     *
     * @param context The function compute initialize environment context object.
     * @throws IOException IOException during I/O handling
     */
    void initialize(Context context) throws IOException;
}        

Example of a simple function that uses the Stream type for input

The following example shows a simple combination of an initializer function and a function that uses the Stream type for input:

package aliyun.serverless.test.example;

import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.FunctionComputeLogger;
import com.aliyun.fc.runtime.StreamRequestHandler;
import com.aliyun.fc.runtime.FunctionInitializer;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class InitializerAndStreamRequest implements StreamRequestHandler, FunctionInitializer {
    @Override
    public void initialize(Context context) {
        FunctionComputeLogger logger = context.getLogger();
        logger.debug(String.format("RequestID is %s %n", context.getRequestId()));
    }

    @Override
    public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
        FunctionComputeLogger logger = context.getLogger();
        logger.debug(String.format("RequestID is %s %n", context.getRequestId()));
        output.write(new String("hello world!").getBytes());
        output.flush();
    }
}      

The initializer method added to InitializerAndStreamRequest is an initializer and has the following features:

  • Package and class names

    Similar to event functions, initializer functions can be assigned any package and class names. An initializer function is in the format of {package}.{class}::{method}. According to the definition, the initializer function in this example is aliyun.serverless.test.example.InitializerAndStreamRequest::initialize.

  • Implemented API operation

    Your code must implement the API operation predefined by Function Compute. In the preceding example, the FunctionInitializer API operation is implemented in the initializer. The initializer function has only one input parameter: the context parameter.

  • The context parameter. The context parameter contains the runtime information of a function, such as the request ID and the temporary AccessKey pair. This parameter is of the com.aliyun.fc.runtime.Context type.

  • Returned data

    Functions that implement the FunctionInitializer API operation do not return any value.

Example of a function that uses the Pojo type for input

The following example shows a simple combination of an initializer function and a function that uses the Pojo type for input:

package aliyun.serverless.test.example;

import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.PojoRequestHandler;
import com.aliyun.fc.runtime.FunctionInitializer;
import com.aliyun.fc.runtime.FunctionComputeLogger;

public class InitializerAndPojoRequest implements FunctionInitializer,PojoRequestHandler<SimpleRequest, SimpleResponse> {
    @Override
    public void initialize(Context context)  {
        FunctionComputeLogger logger = context.getLogger();
        logger.debug(String.format("RequestID is %s %n", context.getRequestId()));
    }

    @Override
    public SimpleResponse handleRequest(SimpleRequest request, Context context) {
        FunctionComputeLogger logger = context.getLogger();
        logger.debug(String.format("RequestID is %s %n", context.getRequestId()));
        String message = "Hello, " + request.getFirstName() + " " + request.getLastName();
        return new SimpleResponse(message);
    }
}