すべてのプロダクト
Search
ドキュメントセンター

:Java Runtime

最終更新日:Mar 20, 2020

Function Compute supports the following Java runtime environment:

  • OpenJDK 1.8.0 (runtime = java8)

For more information about using the Java runtime environment, see Java Interface. The Java runtime environment has the following features:

Use context

A context is an object generated during the function compute process. It contains operation information. You can use the information in your code. It is defined as follows. For more information about implementing a context, click here.

  1. package com.aliyun.fc.runtime;
  2. public interface Context {
  3. public String getRequestId();
  4. public Credentials getExecutionCredentials();
  5. public FunctionParam getFunctionParam();
  6. public FunctionComputeLogger getLogger();
  7. }

A context contains the following information:

  1. RequestId: the unique ID of the request. You can record it for reference in case an exception occurs.
  2. FunctionParam: basic information about the called function, such as the function name, function handler, function memory, and time-out period.
  3. ExecutionCredentials: a set of temporary keys can be acquired when Function Compute plays a service role provided by users. The temporary keys are valid for 5 minutes. You can use temporary keys in your code to access related services, such as OSS. This allows you to avoid adding your own AccessKey information in the function code.
  4. Logger: the logger encapsulated by the Function Compute. For more information, see Use logging.

For example, the following code uses a set of temporary keys to upload a file to OSS:

  1. package example;
  2. import com.aliyun.fc.runtime.Context;
  3. import com.aliyun.fc.runtime.Credentials;
  4. import com.aliyun.fc.runtime.StreamRequestHandler;
  5. import com.aliyun.oss.OSSClient;
  6. import java.io.ByteArrayInputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. public class HelloFC implements StreamRequestHandler {
  11. @Override
  12. public void handleRequest(
  13. InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
  14. String endpoint = "oss-cn-shanghai.aliyuncs.com";
  15. String bucketName = "my-bucket";
  16. Credentials creds = context.getExecutionCredentials();
  17. OSSClient client = new OSSClient(
  18. endpoint, creds.getAccessKeyId(), creds.getAccessKeySecret(), creds.getSecurityToken());
  19. client.putObject(bucketName, "my-object", new ByteArrayInputStream(new String("hello").getBytes()));
  20. outputStream.write(new String("done").getBytes());
  21. }
  22. }

Use logging

The content output by function context.getLogger() is retrieved to a Logstore. The Logstore is specified when you create a service.

  1. package example;
  2. import com.aliyun.fc.runtime.Context;
  3. import com.aliyun.fc.runtime.StreamRequestHandler;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. public class HelloFC implements StreamRequestHandler {
  8. @Override
  9. public void handleRequest(
  10. InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
  11. context.getLogger().info("hello world");
  12. outputStream.write(new String("hello world").getBytes());
  13. }
  14. }

The preceding code outputs the following log content:

  1. message:2017-07-05T05:13:35.920Z a72df088-f738-cee3-e0fe-323ad89118e5 [INFO] hello world

You can use context.getLogger().warn and context.getLogger().error to output a WARN log and an ERROR log, respectively.

Use custom modules

If you need to use the custom modules, package them with the code when you create a JAR file. The following example shows how to package OSS Java SDK into a project.

  1. Add OSS Java SDK in pom.xml:

    1. <dependencies>
    2. <dependency>
    3. <groupId>com.aliyun.fc.runtime</groupId>
    4. <artifactId>fc-java-core</artifactId>
    5. <version>1.2.1</version>
    6. </dependency>
    7. <dependency>
    8. <groupId>com.aliyun.oss</groupId>
    9. <artifactId>aliyun-sdk-oss</artifactId>
    10. <version>2.6.1</version>
    11. </dependency>
    12. </dependencies>
  2. Follow the general packaging process.

See Java code packaging.

Handle exceptions

If an exception occurs when Function Compute runs your function, Function Compute captures the exception and returns a message. For example, in the following code:

  1. package example;
  2. import com.aliyun.fc.runtime.Context;
  3. import com.aliyun.fc.runtime.StreamRequestHandler;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. public class HelloFC implements StreamRequestHandler {
  8. @Override
  9. public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
  10. throw new IOException("oops");
  11. }
  12. }

When the function is invoked, the following response is returned:

  1. >>> invk hello-java -f /tmp/a.json
  2. {
  3. "errorMessage" : "oops",
  4. "errorType" : "java.io.IOException",
  5. "errorCause" : "oops",
  6. "stackTrace" : [ "example.HelloFC.handleRequest(HelloFC.java:15)" ]
  7. }
  8. Error: Request id: 45dd8d90-6b78-cce3-087c-8bf4ebc6c9af. Error type: UnhandledInvocationError

When an exception occurs, the HTTP header in the response contains X-Fc-Error-Type: UnhandledInvocationError.