All Products
Search
Document Center

Function Compute:Event handlers

Last Updated:Apr 01, 2026

Function Compute provides two Java interfaces for event handlers in the fc-java-core library: StreamRequestHandler and PojoRequestHandler. Choose the interface based on how you want to handle input and output.

InterfaceWhen to use
StreamRequestHandlerWhen you need full control over raw bytes — reads from InputStream and writes to OutputStream
PojoRequestHandlerWhen your input and output map naturally to Java objects — automatically deserializes JSON into a typed POJO

Prerequisites

Before you begin, ensure that you have:

  • Added the fc-java-core dependency to your pom.xml

  • Packaged your code and its dependencies into a JAR file

Add the fc-java-core dependency

Add the following to your pom.xml:

<dependency>
    <groupId>com.aliyun.fc.runtime</groupId>
    <artifactId>fc-java-core</artifactId>
    <version>1.4.1</version>
</dependency>

Check the Maven repository for the latest version.

StreamRequestHandler

StreamRequestHandler passes the invocation payload as a raw byte stream. Read from inputStream to get the event data, and write your result to outputStream.

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());
    }
}

Handler string format

When implementing StreamRequestHandler, set the Handler parameter in your function configuration to:

{package}.{ClassName}::handleRequest

For the example above: example.HelloFC::handleRequest

ComponentValueDescription
exampleJava package nameMust match the package declaration in your code
HelloFCClass nameMust match the class that implements the interface
handleRequestMethod nameRequired when implementing StreamRequestHandler

The package name and class name can be customized, but must match the Handler parameter in your function configuration. For details, see Create a function.

Context object

The context parameter provides runtime information for the current invocation, including the request ID and a temporary AccessKey pair. Its type is com.aliyun.fc.runtime.Context. For details, see Context object.

PojoRequestHandler

PojoRequestHandler automatically deserializes the JSON invocation payload into a typed input object and serializes your return value back to JSON. Both the input and output types must be plain old Java objects (POJOs).

The following example reads a firstName and lastName from the event payload and returns a greeting message.

// 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;
    }
}

Example event payload

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

Deploy your function

Package your code and the fc-java-core dependency into a JAR file before creating the function. For packaging instructions, see Compile and deploy code packages.

What's next

Explore sample programs with complete build and deployment scripts: