我正在使用 JDK8 Nashorn 脚本引擎。我的脚本在 Java 类中注册一个回调,如下所示:
stream.create().input(".env/router-memory-list/router1").management().onChange(function(){
print("onChange: "+input.current());
});
回调函数中的input
引用是在执行回调之前从 Java 线程中设置并在之后删除(ctx.engineScope
是脚本的 Binding 对象):
ctx.engineScope.put("input", input);
input.current(message).executeCallback();
ctx.engineScope.remove("input");
它通常工作正常,但我在执行回调时偶尔会遇到这种错误:
<eval>:15 ReferenceError: "input" is not defined
at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)
at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:319)
at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:291)
at jdk.nashorn.internal.objects.Global.__noSuchProperty__(Global.java:1432)
at jdk.nashorn.internal.scripts.Script$Recompilation$4$383$\^eval\_.L:13(<eval>:15)
at jdk.nashorn.javaadapters.java.lang.Runnable.run(Unknown Source)
at com.swiftmq.impl.streams.comp.io.ManagementInput.executeCallback(ManagementInput.java:105)
at com.swiftmq.impl.streams.processor.StreamProcessor.visit(StreamProcessor.java:92)
at com.swiftmq.impl.streams.processor.po.POMgmtMessage.accept(POMgmtMessage.java:28)
at com.swiftmq.tools.pipeline.PipelineQueue.process(PipelineQueue.java:35)
at com.swiftmq.tools.queue.SingleProcessorQueue.dequeue(SingleProcessorQueue.java:135)
at com.swiftmq.tools.pipeline.PipelineQueue$QueueProcessor.run(PipelineQueue.java:69)
at com.swiftmq.impl.threadpool.standard.PoolThread.run(Unknown Source)
我试图用 a 包围调用代码,synchronized
希望它可能是一个线程问题,但没有成功。查看堆栈跟踪可能是重新编译问题,其中动态设置的对象Binding
正在丢失。有任何想法吗?
编辑 - 这是我为这个脚本创建绑定的方式:
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName((String) entity.getProperty("script-language").getValue());
if (engine == null)
throw new Exception("Engine for script-language '" + entity.getProperty("script-language").getValue() + "' not found!");
ScriptContext newContext = new SimpleScriptContext();
ctx.engineScope = newContext.getBindings(ScriptContext.ENGINE_SCOPE);