可以自定义输入Groovy或者JS代码,然后执行。
如何检测到这些自定义输入的脚本代码执行超时 或者是 死循环之类的。
比如说我想要它执行5秒之后如果还没有结果,就返回给我某个信息,或抛出一个异常。
//根据名称得到脚本引擎
ScriptEngine engine = factory.getEngineByName("groovy");
//Map inputData = (Map)((Map)input).get("data");
Object inputData = input;
String scriptCode = preprocessNode.getParameters();
Map output=new HashMap<>();
Bindings binding = engine.createBindings();
binding.put("input",inputData);
binding.put("output",output);
try {
engine.eval(scriptCode,binding);
Invocable invokeEngine = (Invocable)engine;
output = (Map) invokeEngine.invokeFunction("executeMethod",null);
} catch (ScriptException e) {
logger.error("ScriptException : "+e.getMessage());
} catch (NoSuchMethodException e) {
logger.error("NoSuchMethodException : "+e.getMessage());
}finally {
countDownLatch.countDown();
}
return JSONObject.toJSON(output);
可以使用带时间参数的join,如果join抛出异常,调用线程的interrupt终止它。