聊聊PowerJob的Evaluator

发布时间:2024年01月12日

本文主要研究一下PowerJob的Evaluator

Evaluator

tech/powerjob/server/core/evaluator/Evaluator.java

public interface Evaluator {
    /**
     * 使用给定输入计算表达式
     *
     * @param expression 可执行的表达式
     * @param input      输入
     * @return 计算结果
     */
    Object evaluate(String expression, Object input);

}

Evaluator接口定义了evaluate方法,它有expression和input两个参数,返回计算结果

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实现了Evaluator,它内置了一个groovy的ScriptEngine,其evaluate方法先创建bindings,然后传入参数,最后通过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();

    /**
     * 处理判断节点
     * 1. 执行脚本
     * 2. 根据返回值 disable 掉相应的边以及节点
     */
    @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两个参数,返回计算结果;GroovyEvaluator实现了Evaluator,它内置了一个groovy的ScriptEngine,其evaluate方法先创建bindings,然后传入参数,最后通过ENGINE.eval(expression, bindings)计算结果。

文章来源:https://blog.csdn.net/hello_ejb3/article/details/135543654
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。