This topic describes the structure of event handlers in Java and provides examples.
Handler interface
When you program in Java, you must implement the interfaces provided by Function Compute. The fc-java-core library defines the following two interfaces for event handlers.
-
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. -
Uses the plain old Java object (POJO) type to receive the input
event
and returns responses. You can 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 packages. The handler in the sample code is
example.HelloFC::handleRequest
, whereexample
specifies the Java package name,HelloFC
specifies the class name, andhandleRequest
specifies the class method.Note The package name and class name can be customized. However, they must be consistent with the value of Handler parameter in the function configurations. For more information about how to configure Handler, see Create a function. - 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
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. - Return values
A function that implements
StreamRequestHandler
returns the execution result by using theoutputStream
parameter. - Interface library
The dependency of the
com.aliyun.fc.runtime
package can be referenced in thepom.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
- java11-blank-stream-event: uses the event callbacks in the stream format.
- java11-blank-pojo-event: uses the event callbacks in the POJO format.