上下文
更新時間 2024-12-10 11:32:11
最近更新時間: 2024-12-10 11:32:11
分享文章
本文介紹在函數計算中Context的相關概念和使用示例。
上下文
當函數計算運行您的函數時,會將上下文(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);
????}
}