简体   繁体   English

如何将结果从我的 java 代码传递给 nodeJs?

[英]How to pass a result from my java code to nodeJs?

I am writing an algo to analyze market data in Java, to visualize my data i'd like to make use of an existing charting library from tradingview.我正在编写一个算法来分析 Java 中的市场数据,以可视化我的数据,我想利用交易视图中的现有图表库。 This free charting library runs on nodeJS.这个免费的图表库在 nodeJS 上运行。

I'm having a lot of trouble understanding how to populate it with my data resulting from my algorithm.我在理解如何用我的算法产生的数据填充它时遇到了很多麻烦。

For example my Java code returns a "List< Candlestick >" object, how do i send this to the Javascript code running on the nodeJs ?例如,我的 Java 代码返回一个“List< Candlestick >”对象,我如何将它发送到在 nodeJs 上运行的 Javascript 代码?

if someone would be so kind to give some global directions in how to approach this it would be very much appreciated.如果有人愿意就如何解决这个问题提供一些全球指导,我们将不胜感激。

My assumption here is that you have java code and the result you want to display is on nodejs.我的假设是你有 java 代码,你想要显示的结果在 nodejs 上。

While at this moment calling data from the API is suggested, also there is one more option we can use to solve your problem.虽然此时建议从 API 调用数据,但我们还可以使用另一个选项来解决您的问题。 This is a good case of polyglot programming.这是多语言编程的一个很好的例子。 I have used graalvm .我用过graalvm

Installation via sdkman GraalVM通过 sdkman GraalVM 安装

sdk install java 21.1.0.r11-grl

NodeJS节点

gu install nodejs

Both Main Projects are located here https://gitlab.com/graalvm-java-to-nodejs两个主要项目都位于这里https://gitlab.com/graalvm-java-to-nodejs

  • Java Project (has method which return a list) Java 项目(具有返回列表的方法)
  • NodeJS Project(loads Java Class in NodeJS and call method on class reference to get the list) NodeJS项目(在NodeJS中加载Java类并在类引用上调用方法以获取列表)

Add any code to java library in my case I have a class which just return list of Point as given below:在我的情况下,将任何代码添加到 java 库中,我有一个类,它只返回 Point 列表,如下所示:

public class GraphData {
    public List<Point> getPoints() {
        return List.of(new Point(1, 1), new Point(3, 5));
    }
}

where Point is a POJO class to hold (x, y) value.其中Point是一个用于保存 (x, y) 值的 POJO 类。

Clone this java project https://gitlab.com/graalvm-java-to-nodejs/graalvm-simple-java and execute ./gradlew clean build this should give you a executable jar which can be executed java -jar file.jar command.克隆这个 java 项目https://gitlab.com/graalvm-java-to-nodejs/graalvm-simple-java并执行./gradlew clean build这应该会给你一个可执行的 jar 可以执行 java -jar file.jar 命令.

Now, clone https://gitlab.com/graalvm-java-to-nodejs and install dependencies npm install then execute node --jvm --vm.cp=/home/ashish/IdeaProjects/graavlvm/java-lib-gvm/build/libs/java-lib-gvm-1.0-SNAPSHOT.jar bin/www现在,克隆https://gitlab.com/graalvm-java-to-nodejs并安装依赖项npm install然后执行node --jvm --vm.cp=/home/ashish/IdeaProjects/graavlvm/java-lib-gvm/build/libs/java-lib-gvm-1.0-SNAPSHOT.jar bin/www

Relevant code which interacts with java is as below:与java交互的相关代码如下:

var GraphDataJavaRef = Java.type('in.silentsudo.GraphData');
var graphData = new GraphDataJavaRef();
var data = graphData.getPoints();

in.silentsudo.GraphData class is loaded from the jar file which is provided to node program with argument named --jvm --vm.cp path/to/file.jar in.silentsudo.GraphData类从提供给节点程序的 jar 文件加载,参数名为--jvm --vm.cp path/to/file.jar

Once you navigate to localhost:3000, you should see导航到 localhost:3000 后,您应该会看到

Express Tutorial
Welcome to Express Tutorial

Response from Java class
[Point{x=1, y=1}, Point{x=3, y=5}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM