[英]How do I write a Java method, to be called from server side JavaScript, that takes a JS callback function as an argument?
I'm trying to write a Java API that will be called from server side JavaScript running under the JDK 7 JS engine.我正在尝试编写一个 Java API,它将从在 JDK 7 JS 引擎下运行的服务器端 JavaScript 调用。 The idea is to give consumers the ability to write JS code like below, registering a callback that is later executed by another call to a Java method:
这个想法是让消费者能够编写如下 JS 代码,注册一个回调,稍后由另一个对 Java 方法的调用执行:
myJavaObj.registerCallback(function (param) {
// do stuff here
});
myJavaObj.methodThatTriggersCallback();
Here is some test code I'm using:这是我正在使用的一些测试代码:
import javax.script.Invocable;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class JSTestClient {
private String successCallback;
public void handleSuccess(String successCallback) {
this.successCallback = successCallback;
}
public void doStuff() throws ScriptException, NoSuchMethodException {
ScriptEngineManager manager = new ScriptEngineManager();
Invocable engine = (Invocable) manager.getEngineByName("JavaScript");
engine.invokeFunction(successCallback, "TEST SUCCESS");
}
}
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Main {
public static void main(String[] args) {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
String js =
"var client = new Packages.JSTestClient();\n" +
"client.handleSuccess(function (response) {\n" +
" java.lang.System.out.println(response);\n" +
"});\n" +
"client.doStuff();";
try {
engine.eval(js); // Expecting this to output "TEST SUCCESS"
} catch (ScriptException e) {
e.printStackTrace();
}
}
}
But when I run this I get a java.lang.NoSuchMethodException because it is interpreting the string:但是当我运行它时,我得到一个 java.lang.NoSuchMethodException 因为它正在解释字符串:
function (response) { java.lang.System.out.println(response); }as a function name.
Your handleSuccess
method takes a String
as the argument, but you're calling it with a JavaScript function
object.您的
handleSuccess
方法将String
作为参数,但您正在使用 JavaScript function
对象调用它。 You need to change it to accept a Function
.您需要更改它以接受
Function
。
Since you're using Java 7, you are using the modifiedMozilla Rhino engine, where the implementation classes were moved from package org.mozilla.javascript
to sun.org.mozilla.javascript.internal
.由于您使用的是 Java 7,因此您使用的是经过修改的Mozilla Rhino引擎,其中实现类从包
org.mozilla.javascript
移动到sun.org.mozilla.javascript.internal
。
To get your code running, replace the JSTestClient
with this:要运行您的代码,请将
JSTestClient
替换为:
import sun.org.mozilla.javascript.internal.Context;
import sun.org.mozilla.javascript.internal.Function;
public class JSTestClient {
private Function successCallback;
public void handleSuccess(Function successCallback) {
this.successCallback = successCallback;
}
public void doStuff() {
this.successCallback.call(Context.getCurrentContext(), null, null,
new Object[] { "TEST SUCCESS" });
}
}
Be aware that Java 8 changed to the Nashorn engine, and the code for interacting with Nashorn is entirely different.请注意,Java 8 更改为 Nashorn 引擎,与 Nashorn 交互的代码完全不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.