Graalvm初步尝鲜
graalvm
官网:
https://www.graalvm.org/docs/getting-started/
===================================================
1、解压
tar -xvf graalvm-ee-darwin-amd64-19.2.0.1.tar.gz
===================================================
2、设置PATH
export PATH=/Users/lemonhall/development/graalvm-ee-19.2.0.1/Contents/Home/bin:$PATH
===================================================
3、设置JAVA_HOME
export JAVA_HOME=/Users/lemonhall/development/graalvm-ee-19.2.0.1/Contents/Home
===================================================
4、多语言
node中掺杂java
node --jvm --polyglot index.js
const express = require('express');
const app = express();
app.listen(3000);
app.get('/', function(req, res) {
var text = 'Hello World!';
const BigInteger = Java.type(
'java.math.BigInteger');
text += BigInteger.valueOf(2)
.pow(100).toString(16);
res.send(text);
})
然后就是http://192.168.50.138:3000/
看到:
Hello World!10000000000000000000000000
===================================================
5、java访问js
import org.graalvm.polyglot.*;
class Polyglot {
public static void main(String[] args) {
Context polyglot = Context.create();
Value array = polyglot.eval("js", "[1,2,42,4]");
int result = array.getArrayElement(2).asInt();
System.out.println(result);
}
}
javac Polyglot.java
java Polyglot
===================================================
6、互访
https://www.graalvm.org/docs/reference-manual/embed/
测试一下用js来当做规则引擎;
===================================================
7、spring boot
https://spring.io/projects/spring-boot/
7.1 安装maven
7.1.1 下载
http://maven.apache.org/download.cgi
下载:
wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3
.6.2/binaries/apache-maven-3.6.2-bin.tar.gz
tar zxvf apache-maven-3.6.2-bin.tar.gz
7.1.2 安装
配置一下PATH
export PATH="$PATH:/Users/lemonhall/development/apache-maven-3.6.2/bin"
7.1.3 验证
mvn -v
7.2 安装spring boot
界面上一定要选上web,否则啥都干不了了
package com.example.demo;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@SpringBootApplication
@RestController
@EnableAutoConfiguration
public class DemoApplication {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
运行起来:
mvn spring-boot:run
验证:
http://192.168.50.138:8080/
===================================================
8、整合spring-boot和graalvm
8.1 小改一下试试
加入
import org.graalvm.polyglot.*;
修改home函数为:
@RequestMapping("/")
String home() {
Context polyglot = Context.create();
Value array = polyglot.eval("js", "[1,2,42,4]");
int result = array.getArrayElement(2).asInt();
return "Hello World! "+result;
}
然后最后文件长这样:
package com.example.demo;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
import org.graalvm.polyglot.*;
@SpringBootApplication
@RestController
@EnableAutoConfiguration
public class DemoApplication {
@RequestMapping("/")
String home() {
Context polyglot = Context.create();
Value array = polyglot.eval("js", "[1,2,42,4]");
int result = array.getArrayElement(2).asInt();
return "Hello World! "+result;
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
然后是编译:
mvn compile
运行起来:
mvn spring-boot:run
验证:
http://192.168.50.138:8080/
结果是:Hello World! 42
===================================================
8.2 深入一点点
package com.example.demo;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
import org.graalvm.polyglot.*;
@SpringBootApplication
@RestController
@EnableAutoConfiguration
public class DemoApplication {
Context context = Context.newBuilder()
.allowAllAccess(true)
.build();
@RequestMapping("/")
String home() {
String route01 = "var a='Hello From route01';";
String route02 = "function test(){"+
"var a='Hello From route02';"+
"return a;"+
"}"+
" test();";
context.getBindings("js").putMember("javaNumber", 42);
String result = context.eval("js",route02).asString();
return result;
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
===================================================
===================================================
8.3 结合真实例子来搞
https://www.xttblog.com/?p=2601
JAVA规则引擎 Drools 教程
其实本质上来说,JAVA的规则引擎就是个if else then 执行器
入参都是从外部给过来的,执行的动作也是;
给js的上下文(规则引擎当中注入java的操作);
package com.example.demo;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
import org.graalvm.polyglot.*;
@SpringBootApplication
@RestController
@EnableAutoConfiguration
public class DemoApplication {
public class Services {
@HostAccess.Export
public void createEmployee(String name) {
System.out.println(name);
}
public void exitVM() {
System.exit(1);
}
}
Context context = Context.newBuilder()
.allowAllAccess(true)
.build();
Services services = new Services();
@RequestMapping("/")
String home() {
String route01 = "var a='Hello From route01';";
String route02 = "function test(){"+
"var a='Hello From route02';"+
"let emp = services.createEmployee('John Doe');"+
"console.log('console from guest language');"+
"return a;"+
"}"+
" test();";
context.getBindings("js").putMember("javaNumber", 42);
context.getBindings("js").putMember("services", services);
String result = context.eval("js",route02).asString();
return result;
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
---------------------------------------------
每次执行都能正确返回:
2019-10-22 13:42:46.680 INFO 8279 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms
John Doe
console from guest language
John Doe
console from guest language
===================================================
===================================================
8.4 写一个接近于话费充值的例子
TODO
===================================================
===================================================
8.5 写一个从配置文件读取规则js并动态执行的例子
-
邻家的の柠檬叔 转发了这篇日记
可以,写起来应该可以写得很爽,初步已经OK了,可以js执行java的程序,接下来写一个模拟业务的例子,最后再看怎么封装一下工具类,最后带上性能测试,就可以了;
2019-10-22 13:47:12
邻家的の柠檬叔的最新日记 · · · · · · ( 全部 )
- 与她的告别信 (2人喜欢)
- 2025年2月21日AI多领域阅读列表汇总
- 触发搜索过于敏感问题 (1人喜欢)
- deepseek v3的指令服从性-作为相关度排序器 (1人喜欢)
- 《史上最大机器人🤖》
热门话题 · · · · · · ( 去话题广场 )
-
加载中...