PowerJob的Evaluator方法工作流程源碼解讀
序
本文主要研究一下PowerJob的Evaluator
Evaluator
tech/powerjob/server/core/evaluator/Evaluator.java
public interface Evaluator {
/**
* 使用給定輸入計算表達式
*
* @param expression 可執(zhí)行的表達式
* @param input 輸入
* @return 計算結果
*/
Object evaluate(String expression, Object input);
}Evaluator接口定義了evaluate方法,它有expression和input兩個參數(shù),返回計算結果
GroovyEvaluator
tech/powerjob/server/core/evaluator/GroovyEvaluator.java
@Slf4j
@Component
public class GroovyEvaluator implements Evaluator {
private static final ScriptEngine ENGINE = new ScriptEngineManager().getEngineByName("groovy");
@Override
@SneakyThrows
public Object evaluate(String expression, Object input) {
Bindings bindings = ENGINE.createBindings();
bindings.put("context", input);
return ENGINE.eval(expression, bindings);
}
}GroovyEvaluator實現(xiàn)了Evaluator,它內(nèi)置了一個groovy的ScriptEngine,其evaluate方法先創(chuàng)建bindings,然后傳入?yún)?shù),最后通過ENGINE.eval(expression, bindings)計算結果
DecisionNodeHandler
tech/powerjob/server/core/workflow/hanlder/impl/DecisionNodeHandler.java
@Slf4j
@Component
public class DecisionNodeHandler implements ControlNodeHandler {
private final GroovyEvaluator groovyEvaluator = new GroovyEvaluator();
/**
* 處理判斷節(jié)點
* 1. 執(zhí)行腳本
* 2. 根據(jù)返回值 disable 掉相應的邊以及節(jié)點
*/
@Override
public void handle(PEWorkflowDAG.Node node, PEWorkflowDAG dag, WorkflowInstanceInfoDO wfInstanceInfo) {
String script = node.getNodeParams();
if (StringUtils.isBlank(script)) {
log.error("[Workflow-{}|{}]decision node's param is blank! nodeId:{}", wfInstanceInfo.getWorkflowId(), wfInstanceInfo.getWfInstanceId(), node.getNodeId());
throw new PowerJobException("decision node's param is blank!");
}
// wfContext must be a map
HashMap<String, String> wfContext = JSON.parseObject(wfInstanceInfo.getWfContext(), new TypeReference<HashMap<String, String>>() {
});
Object result;
try {
result = groovyEvaluator.evaluate(script, wfContext);
} catch (Exception e) {
log.error("[Workflow-{}|{}]failed to evaluate decision node,nodeId:{}", wfInstanceInfo.getWorkflowId(), wfInstanceInfo.getWfInstanceId(), node.getNodeId(), e);
throw new PowerJobException("can't evaluate decision node!");
}
boolean finalRes;
if (result instanceof Boolean) {
finalRes = ((Boolean) result);
} else if (result instanceof Number) {
finalRes = ((Number) result).doubleValue() > 0;
} else {
log.error("[Workflow-{}|{}]decision node's return value is illegal,nodeId:{},result:{}", wfInstanceInfo.getWorkflowId(), wfInstanceInfo.getWfInstanceId(), node.getNodeId(), JsonUtils.toJSONString(result));
throw new PowerJobException("decision node's return value is illegal!");
}
handleDag(finalRes, node, dag);
}
//......
}DecisionNodeHandler實例化了groovyEvaluator,其handle方法通過groovyEvaluator.evaluate(script, wfContext)來計算node的結果
小結
PowerJob的Evaluator接口定義了evaluate方法,它有expression和input兩個參數(shù),返回計算結果;GroovyEvaluator實現(xiàn)了Evaluator,它內(nèi)置了一個groovy的ScriptEngine,其evaluate方法先創(chuàng)建bindings,然后傳入?yún)?shù),最后通過ENGINE.eval(expression, bindings)計算結果。
以上就是PowerJob的Evaluator方法工作流程源碼解讀的詳細內(nèi)容,更多關于PowerJob Evaluator工作流程的資料請關注腳本之家其它相關文章!
相關文章
idea創(chuàng)建springboot項目,java版本只能選擇17和21的解決方案
這篇文章主要介紹了idea創(chuàng)建springboot項目,java版本只能選擇17和21的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
如何解決java.util.zip.ZipFile解壓后被java占用問題
這篇文章主要介紹了如何解決java.util.zip.ZipFile解壓后被java占用問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
Java使用ByteBuffer進行多文件合并和拆分的代碼實現(xiàn)
因為驗證證書的需要,需要把證書文件和公鑰給到客戶,考慮到多個文件交互的不便性,所以決定將2個文件合并成一個文件交互給客戶,但是由于是加密文件,采用字符串形式合并后,拆分后文件不可用,本文給大家介紹了Java使用ByteBuffer進行多文件合并和拆分,需要的朋友可以參考下2024-09-09
IDEA maven compile報錯OutOfMemoryError(內(nèi)存溢出)解決及jvm分析
遇到Maven編譯時報OutOfMemoryError錯誤通常因為默認的堆內(nèi)存大小不足,本文就來介紹一下OutOfMemoryError(內(nèi)存溢出)解決,具有一定的參考價值,感興趣的可以了解一下2024-10-10
Mybatis中<if>和<choose>的區(qū)別及“=”判斷方式
這篇文章主要介紹了Mybatis中<if>和<choose>的區(qū)別及“=”判斷方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06

