java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > PowerJob Evaluator工作流程

PowerJob的Evaluator方法工作流程源码解读

作者:codecraft

这篇文章主要介绍了PowerJob的Evaluator方法工作流程源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

本文主要研究一下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)计算结果。

以上就是PowerJob的Evaluator方法工作流程源码解读的详细内容,更多关于PowerJob Evaluator工作流程的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文