java 集成graalvm js 引擎

java 内置的js 引擎nashorn已经是被淘汰的,oracle 比较推荐的是使用graalvm js,以下是一个简单的集成试用

环境准备

  • pom.xml
   <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dalong.ex</groupId>
    <artifactId>qlex-learnint</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <encoding>UTF-8</encoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>QLExpress</artifactId>
            <version>3.2.0</version>
        </dependency>
<!--        如果使用了js-scriptengine 以下可选-->
<!--        <dependency>-->
<!--            <groupId>org.graalvm.truffle</groupId>-->
<!--            <artifactId>truffle-api</artifactId>-->
<!--            <version>20.2.0</version>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>org.graalvm.sdk</groupId>-->
<!--            <artifactId>graal-sdk</artifactId>-->
<!--            <version>20.2.0</version>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.graalvm.js</groupId>
            <artifactId>js-scriptengine</artifactId>
            <version>20.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.graalvm.js</groupId>
            <artifactId>js</artifactId>
            <version>20.2.0</version>
        </dependency>
    </dependencies>
    <build>
        <!-- Maven Shade Plugin -->
        <finalName>my-expression-app</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>Application</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project> 
  • application.java
    使用两种方法一种是polyglot 模式,一种是基于ScriptEngineManager模式的
 
 import org.graalvm.polyglot.Context;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
/**
 @author dalong
*/
public class Application {
    public static void main(String[] args) throws ScriptException, NoSuchMethodException {
       // qlExpression();
        method1();
        method2();
    }
    public static void method1()  {
        System.out.println("Hello Java!");
        try (Context context = Context.newBuilder().allowAllAccess(true).build()) {
            context.eval("js", "print('Hello JavaScript!');");
            context.eval("js", "let user = {name:\"dalong\",age:333}; print(JSON.stringify(user))");
            java.math.BigDecimal v = context.eval("js",
                    "var BigDecimal = Java.type('java.math.BigDecimal');" +
                            "BigDecimal.valueOf(10).pow(20)")
                    .asHostObject();
            System.out.println(v.toString());
        }
    }
    public static void method2() throws ScriptException, NoSuchMethodException {
      //   注意此处可以直接使用js,因为js-scriptengine 的spi 注册的时候会自动处理了内置的nashorn
        ScriptEngine eng = new ScriptEngineManager().getEngineByName("js");
        eng.eval("let user = {name:\"dalong\",age:333}; print(JSON.stringify(user))");
    }
}

说明

graalvm js 在性能以及js 支持上是很不错的,是很值的使用,es4x 就是基于graalvm js的,同时新版本的cratedb也是基于此的

参考资料

https://www.graalvm.org/reference-manual/js/JavaInteroperability/ 
https://github.com/graalvm/graaljs/blob/master/docs/user/JavaScriptCompatibility.md

posted on   荣锋亮  阅读(3773)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-08-26 使用plotly dash-component-boilerplate 生成自己的组件
2019-08-26 podium layout 说明
2019-08-26 podium podlets 说明
2019-08-26 podium micro-frontends 简单试用
2019-08-26 podium服务器端的微前端开发框架
2018-08-26 pipelinedb 滑动窗口
2018-08-26 pipelinedb Continuous transforms 操作

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示