简体   繁体   English

Java和C ++之间的IPC

[英]IPC between Java and C++

My goal here is to make two separate applications (one in Java and other in C++, both on the same machine) read from the same SQLite database. 我的目标是从同一个SQLite数据库中创建两个独立的应用程序(一个在Java中,另一个在C ++中,在同一台机器上)。 The C++ implementation already works and has all the methods that I need for that communication. C ++实现已经有效,并且具有我进行该通信所需的所有方法。 It uses the sqlite3.h libraries. 它使用sqlite3.h库。

The first rational thing to do would be to use a JDBC or a SQLite wrapper in the Java application. 要做的第一个理性的事情是在Java应用程序中使用JDBC或SQLite包装器。 The problem is that my embedded system (POSIX) has very limited resources and takes very long to execute a simple query when I have included the necessary *.jar into it. 问题是我的嵌入式系统(POSIX)具有非常有限的资源,并且当我将必要的* .jar包含在其中时执行简单查询需要很长时间。 I have tried out the Xerial JDBC, sqlite4java, sqljet and the Javasqlite Wrapper/JDBC driver from Christian Werner. 我已经尝试过Christian Werner的Xerial JDBC,sqlite4java,sqljet和Javasqlite Wrapper / JDBC驱动程序。 The JavaVM just takes too long to load everything and execute it and performance is a critical issue. JavaVM只需要很长时间来加载所有内容并执行它,性能是一个关键问题。

As a workaround, I have managed the Java application to use system commands and run the sqlite3 command shell to execute the query and obtain the answer. 作为一种解决方法,我已经管理Java应用程序以使用系统命令并运行sqlite3命令shell来执行查询并获得答案。 I am looking for a more "stylish" and secure solution. 我正在寻找一种更“时尚”和安全的解决方案。

I actually need the Java application to use the methods from C++. 我实际上需要Java应用程序来使用C ++中的方法。 They just return a string as the methods are implemented to return only one value. 它们只是在实现方法时返回一个字符串,只返回一个值。 After a lot of IPC reading, I have reached the conclusion that I have to use named Pipes. 经过大量的IPC阅读,我得出的结论是我必须使用命名管道。 The thing is that I would have to use JNI but I have a beginner Java level and by this time, JNI is just too complex for me. 问题是我必须使用JNI,但我有一个初学Java级别,到目前为止,JNI对我来说太复杂了。 Is JNI an overkill in this case? JNI在这种情况下是否过度杀伤?

What other solution could I implement here? 我可以在这里实施哪些其他解决方案

Not sure about the performance you need over the IPC but there are several approaches: 不确定您在IPC上所需的性能,但有几种方法:

  1. use sockets 使用套接字
  2. use pipes 用管子
  3. use memorymappedfiles (using memorymappedfiles you will have a performance gain) 使用memorymappedfiles(使用memorymappedfiles,你将获得性能提升)

In either case you will need a serializer/deserializer for the objects(data) you pass from java to c++ and viceversa. 在任何一种情况下,您都需要一个序列化器/反序列化器,用于从java传递给c ++的对象(数据),反之亦然。

Depending on the data format you might need serializer/deserializer only on Java side. 根据数据格式,您可能只需要在Java端使用串行器/解串器。 (eg you send out binary data that C++ will read without needing to decode it anymore). (例如,您发送的C ++将读取的二进制数据不再需要解码)。 A good tutorial on how to use memorymapped file in java can be found here and in C++ you need to use mmap function. 关于如何在java中使用memorymapped文件的好教程可以在这里找到,在C ++中你需要使用mmap函数。

You could use swig . 你可以使用swig Swig can parse your C/C++ header and generate Java clases/functions of it. Swig可以解析您的C / C ++标头并生成它的Java clases /函数。 The generated code has jni calls to call your c++ clases or your c functions. 生成的代码有jni调用来调用你的c ++ clases或你的c函数。

Actually I was wrong. 其实我错了。 I don't need to use JNI to use named pipes in Java. 我不需要使用JNI在Java中使用命名管道。 I have successfully communicated these two processes using basic techniques. 我已经使用基本技术成功地传达了这两个过程。 In java I have just used FileOutputStream and FileInputStream to communicate with the named pipes. 在java中,我刚刚使用FileOutputStream和FileInputStream与命名管道进行通信。

This link was specially useful to me: 这个链接对我特别有用:

http://carminedimascio.com/2014/01/named-pipes-with-java/ http://carminedimascio.com/2014/01/named-pipes-with-java/

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

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