tags:

views:

248

answers:

4

My app is written in Java. There is a C++ library I need to utilize. I don't want to use JNI.

60 times a second, the C++ app needs to send the Java app 10MB of data; and the Java app needs to send the C++ app 10 MB of data.

Both apps are running on the same machine; the OS is either Linux or Mac OS X.

What is the most efficient way to do this? (At the moment, I'm considering TCPIP ports; but in C++, I can do memory mapping -- can I do something similar in Java?)

Thanks!

+2  A: 

Sounds like shared memory would be the way to go. I believe the NIO libraries support that.

The question is WHAT do you want your program to do?

Thorbjørn Ravn Andersen
+4  A: 

Yes, Java has memory-mapped files with the NIO framework.

If you're trying to avoid JNI because you didn't want to write stubs, you can also interface with C++ code (at least ones that are extern "C") using JNA. For best performance, use direct mapping (concrete classes with native methods, not a mapped interface)---see documentation for more details. :-)

Chris Jester-Young
JNA is just a wrapper on JNI.
bmargulies
Yes---I just edited my question to say that if the OP wanted to avoid JNI because of the need to avoid writing stubs, then JNA is a good alternative (as it handles the stubs for you).
Chris Jester-Young
I want to avoid JNI to avoid unstability. I don't trust the C++ library to not have bugs and do not want it to destabilize my Java VM.
anon
anon
+1  A: 

Not directly helpful and will be interesting at least to develop but you could throw in an SSD/RAM drive accessible to both the Java and C++ application and have a sort of juggle between data ops in there with file-based locking and all that odd stuff.

What would make this scheme sort of manageable from performance point is that for this purpose Java NIO has ByteBuffer which is a high level representation of low level byte mapping on disk.

Esko
+5  A: 

Using mapped files is a way of hand-rolling a highly optimized rpc. You might consider starting with a web service talking over local sockets, using MTOM for attaching the data, or just dropping it into a file. Then you could measure the performance. If the data was a problem, you could then use mapping.

Note that there are some odd restrictions on this that make your code sensitive to whether it is running on Windows or not. On Windows, you can't delete something that is open.

I should point out that I have done exactly what you are proposing here. It has a control channel on a socket, and the data is shared via a file that is mmapped in C++ (or the Windows equivalent) and NIO mapped in Java. It works. I've never measured maximum throughput, though.

bmargulies