0 results found
DeCo
janino使用
2019/05/09 java 脚本 janino

pom依赖

<dependency>
    <groupId>org.codehaus.janino</groupId>
    <artifactId>janino</artifactId>
    <version>3.0.12</version>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.44</version>
</dependency>

测试代码

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.codehaus.commons.compiler.CompileException;
import org.codehaus.janino.ScriptEvaluator;
import org.junit.Test;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class JaninoTest {

    private static final String JSON_STR = "{\"evalMatches\":[{\"value\":1501,\"metric\":\"billItemQry ERROR\"," +
            "\"tags\":{\"level.keyword\":\"ERROR\",\"name.keyword\":\"billItemQry\"}},{\"value\":1501," +
            "\"metric\":\"EXTHIRDQRY ERROR\",\"tags\":{\"level.keyword\":\"ERROR\",\"name.keyword\":\"EXTHIRDQRY\"}}," +
            "{\"value\":1501,\"metric\":\"qryIsOnWay ERROR\",\"tags\":{\"level.keyword\":\"ERROR\",\"name" +
            ".keyword\":\"qryIsOnWay\"}},{\"value\":1501,\"metric\":\"autoTestWXKFNew ERROR\",\"tags\":{\"level" +
            ".keyword\":\"ERROR\",\"name.keyword\":\"autoTestWXKFNew\"}},{\"value\":1501," +
            "\"metric\":\"checkLineQuality ERROR\",\"tags\":{\"level.keyword\":\"ERROR\",\"name" +
            ".keyword\":\"checkLineQuality\"}},{\"value\":1501,\"metric\":\"ERROR checkRateStateNew\"," +
            "\"tags\":{\"level.keyword\":\"ERROR\",\"name.keyword\":\"checkRateStateNew\"}},{\"value\":1501," +
            "\"metric\":\"qryDslamState ERROR\",\"tags\":{\"level.keyword\":\"ERROR\",\"name" +
            ".keyword\":\"qryDslamState\"}},{\"value\":1501,\"metric\":\"ERROR BALRELATIONHIS\",\"tags\":{\"level" +
            ".keyword\":\"ERROR\",\"name.keyword\":\"BALRELATIONHIS\"}},{\"value\":1501,\"metric\":\"INSTANT_FEE_SE " +
            "ERROR\",\"tags\":{\"level.keyword\":\"ERROR\",\"name.keyword\":\"INSTANT_FEE_SE\"}},{\"value\":1501," +
            "\"metric\":\"PointInfoQry ERROR\",\"tags\":{\"level.keyword\":\"ERROR\",\"name" +
            ".keyword\":\"PointInfoQry\"}}],\"message\":\"5分钟内 ERROR 已超过 1 次\",\"ruleId\":1,\"ruleName\":\"接口 error " +
            "alert\",\"ruleUrl\":\"http://localhost/grafana/d/P75lX0jik/jie-kou-metrics?fullscreen\\u0026edit" +
            "\\u0026tab=alert\\u0026panelId=18\\u0026orgId=1\",\"state\":\"alerting\",\"title\":\"[Alerting] 接口 error" +
            " alert\"}";

    private static final String SCRIPT = "JSONArray evalMatches = json.getJSONArray(\"evalMatches\");" +
            "        StringBuilder sb = new StringBuilder(16);" +
            "        String sp = \"\";" +
            "        for (int i = 0; i < evalMatches.size(); i++) {" +
            "            JSONObject jsonObject = evalMatches.getJSONObject(i);" +
            "            String metric = jsonObject.getString(\"metric\");" +
            "            String value = jsonObject.getString(\"value\");" +
            "            sb.append(sp).append(\"指标:\").append(metric).append(\", 值:\").append(value);" +
            "            sp = \"\\n\";" +
            "        }" +
            "        return sb.toString();";

    private final static List<String> DEFAULT_PARAM_NAME_LIST = new ArrayList<>();
    private final static List<Class> DEFAULT_PARAM_TYPE_LIST = new ArrayList<>();

    static {
        DEFAULT_PARAM_NAME_LIST.add("json");
        DEFAULT_PARAM_TYPE_LIST.add(JSONObject.class);
    }

    @Test
    public void janino() throws CompileException, InvocationTargetException {
        JSONObject json = JSON.parseObject(JSON_STR);
        // JSONArray evalMatches = json.getJSONArray("evalMatches");
        // StringBuilder sb = new StringBuilder(16);
        // String sp = "";
        // for (int i = 0; i < evalMatches.size(); i++) {
        //     JSONObject jsonObject = evalMatches.getJSONObject(i);
        //     String metric = jsonObject.getString("metric");
        //     String value = jsonObject.getString("value");
        //     sb.append(sp).append("指标:").append(metric).append(", 值:").append(value);
        //     sp = "&#x000A;";
        // }
        // return sb.toString();

        // import com.alibaba.fastjson.JSON;
        // import com.alibaba.fastjson.JSONArray;
        // import com.alibaba.fastjson.JSONObject;
//        ScriptEvaluator evaluator = new ScriptEvaluator(
//                "import com.alibaba.fastjson.JSONArray;" +
//                        "import com.alibaba.fastjson.JSONObject;" + SCRIPT,
//                String.class,
//                DEFAULT_PARAM_NAME_LIST.toArray(new String[]{}),
//                DEFAULT_PARAM_TYPE_LIST.toArray(new Class[]{})
//        );
        ScriptEvaluator evaluator = new ScriptEvaluator();
        // 参数列表、参数类型
        evaluator.setParameters(DEFAULT_PARAM_NAME_LIST.toArray(new String[]{}), DEFAULT_PARAM_TYPE_LIST.toArray(new Class[]{}));
        // 返回类型
        evaluator.setReturnType(String.class);
        // 脚本、需要导包
        evaluator.cook("import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject;" + SCRIPT);
        // 执行脚本
        Object evaluate = evaluator.evaluate(new Object[]{json});
        System.out.println(evaluate);
    }
}

输出

指标:billItemQry ERROR, 值:1501
指标:EXTHIRDQRY ERROR, 值:1501
指标:qryIsOnWay ERROR, 值:1501
指标:autoTestWXKFNew ERROR, 值:1501
指标:checkLineQuality ERROR, 值:1501
指标:ERROR checkRateStateNew, 值:1501
指标:qryDslamState ERROR, 值:1501
指标:ERROR BALRELATIONHIS, 值:1501
指标:INSTANT_FEE_SE ERROR, 值:1501
指标:PointInfoQry ERROR, 值:1501
请杯咖啡呗~
支付宝
微信
本文作者:DeCo
版权声明:本文首发于DeCo的博客,转载请注明出处!