This topic describes the structure characteristics and examples of event handlers in Java.

Handler interface

When you are programming in Java, you must implement the interfaces provided by Function Compute. The fc-java-core library defines the following two interfaces for event handlers.

  • StreamRequestHandler

    Uses streams to receive the input event data and returns the execution result. You must read the input data from input streams and then write the execution result to output streams.

  • PojoRequestHandler

    Uses the plain old Java object (POJO) class, which allows you to customize the input and output of the handler. Both the input and output must be of the POJO type.

StreamRequestHandler

The following sample code provides an example of StreamRequestHandler:

package example;

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

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

public class HelloFC implements StreamRequestHandler {

    @Override
    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        outputStream.write(new String("hello world").getBytes());
    }
}           
  • Package and class names

    Java is different from other languages in execution methods due to its packages. The handler in the sample code is example.HelloFC::handleRequest, where example specifies the Java package name, HelloFC specifies the class name, and handleRequest specifies the class method.

    Note The package name and class name can be customized. However, they must be consistent with the value of Request Handler parameter in the function configurations.
  • Implemented interfaces

    Your code must implement the predefined interfaces provided by Function Compute. In the preceding example, StreamRequestHandler is implemented. The inputStream parameter of the handler specifies the input parameters when you invoke a function and the outputStream parameter is used to return the execution result.

  • 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. For more information, see Context object.

  • Output

    A function that implements StreamRequestHandler returns the execution result by using the outputStream parameter.

  • Interface library
    The dependency of the com.aliyun.fc.runtime package can be referenced in the pom.xml file below:
    <dependency>
        <groupId>com.aliyun.fc.runtime</groupId>
        <artifactId>fc-java-core</artifactId>
        <version>1.4.1</version>
    </dependency>           

    You can visit the Maven repository to obtain the latest version of the fc-java-core package.

Before you create a function, you must compress the code and its dependency fc-java-core into a JAR package. For more information about how to compress the code and its dependencies, see Compile and deploy code packages.

PojoRequestHandler

The following sample code provides an example of PojoRequestHandler: The object of SimpleRequest is an object that can be serialized into a JSON string, such as an object of the POJO type.

// HelloFC.java
package example;

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

public class HelloFC implements PojoRequestHandler<SimpleRequest, SimpleResponse> {

    @Override
    public SimpleResponse handleRequest(SimpleRequest request, Context context) {
        String message = "Hello, " + request.getFirstName() + " " + request.getLastName();
        return new SimpleResponse(message);
    }
}            
// SimpleRequest.java
package example;

public class SimpleRequest {
    String firstName;
    String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public SimpleRequest() {}
    public SimpleRequest(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}            
// SimpleResponse.java
package example;

public class SimpleResponse {
    String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public SimpleResponse() {}
    public SimpleResponse(String message) {
        this.message = message;
    }
}            

The following sample code describes the input event parameters:

{
  "firstName": "FC",
  "lastName": "aliyun"
}            

Sample programs

The Function Compute libraries contain sample programs that use various handler types and interfaces. Each sample program contains methods for easy compilation and deployment. Examples: