janino 使用

ScriptEvaluator

导包

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>${janino.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>

测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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;

/**
* @author YL
*/
public class JaninoTest {

private static Class<?>[] THROWN_EXCEPTIONS = new Class[1];

static {
THROWN_EXCEPTIONS[0] = Exception.class;
}

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 = \"&#x000A;\";" +
" 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;";
// sp = "\\n";
// }
// 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[]{}),
THROWN_EXCEPTIONS
);
Object evaluate = evaluator.evaluate(new Object[]{json});
System.out.println(evaluate);
}
}

输出

1
2
3
4
5
6
7
8
9
10
指标: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
  • 本文作者: forever杨
  • 本文链接: https://blog.yl-online.top/posts/b4b6f0b8.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。如果文章内容对你有用,请记录到你的笔记中。本博客站点随时会停止服务,请不要收藏、转载!