在Node js中调用Java方法 [英] Calling a java method in Node js

查看:41
本文介绍了在Node js中调用Java方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从节点js调用Java中定义的函数.

I am trying to call a function defined in a java from node js .

示例:

public class A{
 public void show(){
   System.out.prntln("Invoked from Node JS");
 }
}

和一个节点js文件

console.log("In Node JS");
//define calling A like A a = new A();
a.show();

我可能完全错了,但是我正在尝试从节点js访问Java函数.

I may be totally wrong but I am trying to access a java function from node js .

推荐答案

这是一个很好的问题.通常,有几种方法可以实现语言互操作:

This is a great question. In general, there are several approaches to language inter-operation:

  1. 在完全独立的隔离程序/进程中运行代码,并使用进程间通信(IPC)或其他网络协议(TCP或基于TCP的高层协议(例如HTTP),通常带有REST-ful API,或某种形式的RPC系统)在使用不同语言编写的两个进程之间发送信息.

  1. Running code in completely separate, isolated programs / processes, and using interprocess communication (IPC) or other networking protocols (TCP or higher level protocols built on top of TCP like HTTP, often with a REST-ful API, or some form of RPC system) to send information between the two processes that have been written in different languages.

将一种语言转换"为另一种语言(例如,使用JSweet或TeaVM编译器将Java代码转换为JavaScript代码),然后使用一种语言从原始代码中创建单个应用程序/处理程序,从另一种语言(现在与最终应用程序中内置的另一种语言使用相同的语言)转换的代码.

"Transpiling" one language into the other (e.g. using the JSweet or TeaVM transpilers to convert Java code to JavaScript code), and then creating a single application / process out of the original code in one language together with the transpiled code from the other language (which is now in the same language as the other code being built into that final application).

使用通用中间语言和允许代码进行互操作的低级本机"接口.大多数语言与C都有某种形式的互操作(因为C是大多数操作系统支持的公分母).尽管这不适用于客户端JavaScript(尽管某些原则仍与Native Client(NaCL)相关),但对于NodeJ,您可以使用 cwrap .进入C语言后,您可以使用 Java本机接口(JNI)(尽管可以通过允许 SWIG 自动生成大部分为您准备的样板,而不是直接写入JNI规范.

Using a common intermediate language and low-level "native" interfaces that allow the code to interoperate. Most languages have some form of interoperation with C (because C is a common denominator supported by most operating systems). While this would not work with client-side JavaScript (though some of the principles are still be relevant with Native Client (NaCL)), with NodeJs, you can call into C code using node-gyp and cwrap. Once you are in C land, you can call into Java using the Java Native Interface (JNI) (though making it possible to call your Java code from C using JNI is probably more easily accomplished by letting SWIG autogenerate much of the boilerplate for this for you, rather than directly writing to the JNI specification).

与所有事物一样,各种方法也需要权衡:

As with all things, there are tradeoffs to the various approaches:

  • 方法1:
    • 优点:
      • 相对简单
      • 几乎可以使用任何编程语言
      • 每个子系统都与另一个子系统完全隔离
      • 每个系统都可以以惯用的语言进行调试
      • 必须定义共享协议
        • 可能会导致重复的冗余代码
        • 协议必须保持同步
        • 更改必须向后兼容,否则将中断
        • 注意:协议缓冲区可以帮助解决此问题
        • must define shared protocol
          • can result in redundant, duplicated code
          • protocols must be kept in sync
          • changes must be backwards-compatible or it will break
          • NOTE: protocol buffers can help with this
          • 子系统之间的数据加密
          • 端点的访问控制
          • 优点:
            • 无序列化/反序列化开销
            • 可以使用针对目标语言的惯用法调试最终系统
            • 并非所有语言都可以从一种语言转换成另一种语言
            • 即使编译器支持两种语言:
              • 通常仅支持该语言的一部分
              • 可能需要修复/修改代码以允许其进行翻译
              • 可能需要修复/修改转译器
              • 翻译中的语义稍有不同会导致细微,令人惊讶的错误
              • 优点:
                • 无序列化/反序列化开销
                • 比方法2更多的支持
                • 无需用任何一种语言重写原始代码
                • 必须成为SWIG等深奥工具的专家
                • 结果很难调试
                  • NodeJS代码的堆栈跟踪突然包含C,JVM,Java代码
                  • 调试工具无法轻松跨越语言(例如,最终可能会逐步通过JVM代码解释Java,而不是逐步通过实际的Java代码)

                  我已经使用过带有方法#1和方法#3的系统(以及使用方法#2的系统的信息),因此我强烈建议尽可能使用方法#1;只有当您发现序列化开销不合理(并且您无法优化通信协议/机制来处理该问题)时,我才会冒险进入其他领域.话虽如此,如果语言非常相似(例如从TypeScript到JavaScript的翻译),则方法2可以成功;如果使用这种机制的范围非常有限(例如,只需要公开一种语言),则方法3可以成功.小但经常被称为/对性能敏感的函数).

                  Having used systems with approach #1 and approach #3 (as well as hearing of systems using approach #2), I would strongly recommend using approach #1 wherever possible; only if you find the serialization overhead to be untenable (and you are not able to optimize the communication protocol / mechanism to handle that problem) would I venture into the other waters. That being said, approach #2 can be successful if the languages are very similar (like transpilation from TypeScript to JavaScript), and approach #3 can be successful if the use of this mechanism is very limited in scope (e.g. just need to expose one small but frequently called / performance-sensitive function this way).

                  这篇关于在Node js中调用Java方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆