当前位置:   article > 正文

springboot集成graaljs +sandbox_delight-graaljs-sandbox

delight-graaljs-sandbox
<dependency>
    <groupId>org.javadelight</groupId>
    <artifactId>delight-nashorn-sandbox</artifactId>
    <version>0.1.31</version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.graalvm.sdk</groupId>
    <artifactId>graal-sdk</artifactId>
    <version>${graalvm.js.version}</version>
</dependency>
<dependency>
    <groupId>org.graalvm.js</groupId>
    <artifactId>js</artifactId>
    <version>${graalvm.js.version}</version>
</dependency>
<dependency>
    <groupId>org.graalvm.regex</groupId>
    <artifactId>regex</artifactId>
    <version>${graalvm.js.version}</version>
</dependency>
<dependency>
    <groupId>org.graalvm.truffle</groupId>
    <artifactId>truffle-api</artifactId>
    <version>${graalvm.js.version}</version>
</dependency>
<dependency>
    <groupId>org.graalvm.js</groupId>
    <artifactId>js-scriptengine</artifactId>
    <version>${graalvm.js.version}</version>
</dependency>
<dependency>
    <groupId>org.javadelight</groupId>
    <artifactId>delight-graaljs-sandbox</artifactId>
    <version>0.1.2</version>
    <exclusions>
        <exclusion>
            <groupId>org.javadelight</groupId>
            <artifactId>delight-nashorn-sandbox</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.graalvm.sdk</groupId>
            <artifactId>graal-sdk</artifactId>
        </exclusion>
        <exclusion>
                <groupId>org.graalvm.js</groupId>
                <artifactId>js</artifactId>
        </exclusion>
        <exclusion>
                <groupId>org.graalvm.regex</groupId>
                <artifactId>regex</artifactId>
        </exclusion>
        <exclusion>
                <groupId>org.graalvm.truffle</groupId>
                <artifactId>truffle-api</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.graalvm.js</groupId>
            <artifactId>js-scriptengine</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.webjars.bower</groupId>
            <artifactId>js-beautify</artifactId>
        </exclusion>
    </exclusions>
</dependency>
  • 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
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75

注意事项1:java 8只能使用delight-nashorn-sandbox的0.1.x版本,0.2.x版本适用于java 11以上,github上面有讲
注意事项2:打包成springboot fatjar时,需要unpack

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring-boot.version}</version>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <requiresUnpack>
            <dependency>
                <groupId>org.graalvm.js</groupId>
                <artifactId>js</artifactId>
            </dependency>
            <dependency>
                <groupId>org.graalvm.js</groupId>
                <artifactId>js-scriptengine</artifactId>
            </dependency>
            <dependency>
                <groupId>org.graalvm.regex</groupId>
                <artifactId>regex</artifactId>
            </dependency>
            <dependency>
                <groupId>org.graalvm.sdk</groupId>
                <artifactId>graal-sdk</artifactId>
            </dependency>
            <dependency>
                <groupId>org.graalvm.truffle</groupId>
                <artifactId>truffle-api</artifactId>
            </dependency>
        </requiresUnpack>
    </configuration>
</plugin>
  • 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
  • 32
  • 33
  • 34
  • 35
  • 36

理由:
在这里插入图片描述

注意事项3:graalvm js的版本不要使用最新的,20.3.5或21.2.0是一个比较稳定的版本,在各操作系统及cpu上都可用。比如arm64
注意事项4:多线程问题,可参考以下文章或demo。
graal javascript thread safety
多线程demo
multithreading
thread issue
async demo
multi threaded java javascript in graalvm
script engine instance problem

注意事项5:graalvm js支持ScriptEngine与Polyglot模式,在delight-graaljs-sandbox沙箱的实现方案中采用的是ScriptEngine,但会面临多线程的问题。按照注意事项4中提到的内容,多线程可以共享graaljs engine,但需使用不同的Context

注意事项6:crash问题,aarch64、openjdk 1.8.0_242会有问题,升级openjdk 1.8.0_332
graalvm release notes
报错如下:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x0000fffcb1ca7ef8, pid=3561071, tid=0x0000fffca253f1e0
#
# JRE version: OpenJDK Runtime Environment (8.0_242-b08) (build 1.8.0_242-b08)
# Java VM: OpenJDK 64-Bit Server VM (25.242-b08 mixed mode linux-aarch64 compressed oops)
# Problematic frame:
# V  [libjvm.so+0x8c7ef8]  oopDesc* PSPromotionManager::copy_to_survivor_space<false>(oopDesc*)+0xf8
#
# Core dump written. Default location: /data/dgaxoa/app-document-service/core or core.3561071
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

在这里插入图片描述

注意事项7:有些环境下需要使用-XX:+UseG1GC

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/366854?site
推荐阅读
相关标签
  

闽ICP备14008679号