Java 1.8 and above
Here is a simple Java function that just echo the input with request ID
package aliyun.serverless.test.example;
import com.aliyun.fc.runtime.StreamRequestHandler;
import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.FunctionComputeLogger;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* This handler just echo the input with request ID
*/
public class EchoStreamHandler implements StreamRequestHandler {
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
FunctionComputeLogger logger = context.getLogger();
logger.debug(String.format("Handle request %s", context.getRequestId()));
byte[] data = new byte[1024];
output.write(context.getRequestId().getBytes());
output.write(System.lineSeparator().getBytes());
int nRead;
while ((nRead = input.read(data, 0, data.length)) != -1) {
output.write(data, 0, nRead);
logger.debug(String.format("handler received data %s", new String(data)));
}
output.flush();
}
}