一、GraalVM.js介绍
是一款 JavaScript 解释器/编译器,能够在 JVM 上运行 Node.js 应用;
主要应用场景
因为JS是动态语言,不需要编译,因此可以通过JS动态改变程序执行逻辑,比如:风控规则,服务编排等等
二、代码工程
pom.xml
springboot-democom.et1.0-SNAPSHOT4.0.0graalvm-js88org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-autoconfigureorg.springframework.bootspring-boot-starter-testtestorg.graalvm.jsjs-scriptengine20.2.0org.graalvm.jsjs20.2.0
属性文件
server:port: 8088
启动类
package com.et.graalvm.js;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class DemoApplication { public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args); }}
代码仓库
https://github.com/Harries/springboot-demo
三、测试
本地调用
package com.et.graalvm.js;import org.junit.Test;import javax.script.*;/** * @author liuhaihua * @version 1.0 * @ClassName com.et.graalvm.js.JSTest * @Description todo * @date 2024年03月07日 9:47 */public class JSTest {@Testpublicvoid test10(){// 创建JavaScript引擎ScriptEngine jsEngine = new ScriptEngineManager().getEngineByName("js");//ScriptEngine jsEngine = new ScriptEngineManager().getEngineByExtension("js");//ScriptEngine jsEngine = new ScriptEngineManager().getEngineByMimeType("text/javascript");// 方式一,默认设置变量jsEngine.put("hello", "jack");// 方式二,使用binding设置变量SimpleBindings bindings = new SimpleBindings();bindings.put("hello","world");jsEngine.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);// 方式三,使用Context设置变量ScriptContext scriptContext = jsEngine.getContext();scriptContext.setAttribute("hello", "polo", ScriptContext.ENGINE_SCOPE);try {// 方式一,直接调用jsEngine.eval("print(hello)");// 方式二,编译调用(需要重复调用,建议先编译后调用)if (jsEngine instanceof Compilable){CompiledScript compileScript = ((Compilable) jsEngine).compile("print(hello)");if (compileScript != null){for (int i = 0; i < 10; i++) {compileScript.eval();}}}Invocable invocable = ((Invocable)jsEngine);// 方式三,使用JavaScript中的顶层方法jsEngine.eval("function greet(name) { print('Hello, ' + name); } ");invocable.invokeFunction("greet", "tom");// 方式四,使用某个对象的方法jsEngine.eval("var obj = { getGreeting: function(name){ return 'hello, ' + name; } } ");Object obj = jsEngine.get("obj");Object result = invocable.invokeMethod(obj, "getGreeting", "kitty");System.out.println(result);} catch (ScriptException | NoSuchMethodException e) {e.printStackTrace();}}}
远程调用
package com.et.graalvm.js;import java.io.InputStream;import java.io.InputStreamReader;import java.io.BufferedReader;import java.io.IOException;import java.net.URL;import java.net.URLConnection;import javax.script.ScriptEngine;import javax.script.ScriptEngineManager;import javax.script.ScriptException;public class RemoteJSTest {public static void main(String[] args) throws IOException, ScriptException {URL jsUrl = new URL("https://example.com/script.js"); // js文件的URLURLConnection connection = jsUrl.openConnection();InputStream inputStream = connection.getInputStream(); // 获取js文件的流BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));StringBuilder sb = new StringBuilder();String line;while ((line = reader.readLine()) != null) {sb.append(line).append("\n"); // 将js文件的内容存入StringBuilder}reader.close();inputStream.close();ScriptEngineManager engineManager = new ScriptEngineManager();ScriptEngine engine = engineManager.getEngineByName("nashorn"); // 获取Nashorn引擎String script = sb.toString(); // js文件的内容engine.eval(script); // 运行js文件Object result = engine.eval("hello()"); // 调用js文件中名为"hello"的函数System.out.println(result); // 输出结果}}
四、引用
https://www.graalvm.org/reference-manual/js/JavaInteroperability/
https://github.com/graalvm/graaljs/blob/master/docs/user/JavaScriptCompatibility.md
http://www.liuhaihua.cn/archives/710296.html