上下文
当函数计算运行您的函数时,会将上下文(Context)对象传递到执行方法中。该对象包含有关调用、服务、函数和执行环境等信息。Context接口定义如下:
package com.ctg.faas.runtime;
/**
* The context object allows you to access useful information available within the Tianyi cloud
* function compute execution environment
*/
public interface Context {
/**
* Gets the function request ID associated with the request. <p> This is
* the same ID returned to the client that called invoke(). This ID is reused for retries on the
* same request. </p>
*
* @return The request id
*/
public String getRequestId();
/**
* Gets the credentials of the execution role
*
* @return The request Credentials
*/
public Credentials getExecutionCredentials();
/**
* Gets the function related parameters
*
* @return The request function params
*/
public FunctionParam getFunctionParam();
/**
* Gets the service related parameters
*
* @return The request service info
*/
public Service getService();
/**
* Gets the function compute logger instance associated with the context object
*
* @return The fc logger
*/
public FunctionComputeLogger getLogger();
/**
* Gets the function compute opentracing instance associated with the context object
*
* @return The fc tracing
*/
public OpenTracing getTracing();
/**
* Gets the function compute retry count associated with the context object
*
* @return The retry count
*/
public int getRetryCount();
}
示例:获取函数相关信息
使用Context获取函数Handler和Initializer:
package example;
import com.ctg.faas.runtime.Context;
import com.ctg.faas.runtime.FunctionParam;
import com.ctg.faas.runtime.PojoRequestHandler;
public class PojoHandler implements PojoRequestHandler<String, String> {
@Override
public String handleRequest(String in, Context context) {
FunctionParam functionParam = context.getFunctionParam();
String initializer = functionParam.getFunctionInitializer();
String handler = functionParam.getFunctionHandler();
return String.format("Function initializer is: %s, handler is: %s", initializer, handler);
}
}