graalvm js 加载远端js 文件


graalvm js 支持了基于远端模式的js 文件加载(当然是处于实验阶段的,生产谨慎使用),以下是一个简单的demo

环境准备

  • 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>
  • 开启实验特性的远端url 加载
Engine engine = Engine.newBuilder().option("js.load-from-url","true").allowExperimentalOptions(true).build();
  • 代码
// 复用了同一个engine,可以提高性能
public  static void loadURL(Engine engine) throws IOException {
        Context context = Context.newBuilder().allowAllAccess(true).allowHostClassLoading(true).allowIO(true).engine(engine).build();
        Source mysource =Source.newBuilder("js"," load(\"http://127.0.0.1:8080/mylogin.js\")","rongdemo").build();
        context.eval(mysource);
        Value callapp = context.getBindings("js").getMember("mylogin");
        System.out.println(callapp.execute());
}
  • mylogin.js 内容
function mylogin(){
    var BigDec = Java.type('java.math.BigDecimal');
    var bd = new BigDec("0.1");
    console.log(bd.add(bd).toString());
    return "dalong rong feng"
}
  • 运行效果

 

 

参考资料

https://github.com/graalvm/graaljs/blob/master/docs/user/JavaScriptCompatibility.md


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM