Skip to content

aliyun/fc-java-libs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fc-java-libs

maven version build status

Requirements

  • Java 1.8 and above

License

APACHE2.0

Example

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