Interplatform (interprocess, interlanguage) communication

S

Stefan Ram

»X« below is another language than Java, for example,
VBA, C#, or C.

When an X process and a Java process have to exchange
information on the same computer, what possibilites are
there? The Java process should act as a client, sending
commands to the X process and also wants to read answers
from the X process. So, the X process is a kind of server.

My criteria are: reliability and it should not be extremely
slow (say exchanging a string should not take more than
about 10 ms). The main criterion is reliability.

»Reliability« means little risk of creating problems, little
risk of failure at run-time. (It might help when the client
[=Java process] can reset the communication to a known and
sane start state in case of problems detected at run-time.)

The host OS is Windows, but a portable solution won't hurt.

A list of possibilities I am aware of now:

Pipes

I have no experience with this. I heard one can establish
a new process »proc« with »exec« and then use

BufferedWriter out = new BufferedWriter(new
OutputStreamWriter(proc.getOutputStream()));
BufferedReader in = new BufferedReader(new
InputStreamReader(proc.getInputStream()));

Files

One process writes to the end of a file, the other reads
from the end of the file? - I never tried this, don't know
if it is guaranteed to work that one process can detect and
read, whether the other has just appended something to a file.

What if the processes run very long and the files get too
large? But OTOH this is very transparent, which makes it easy
to debug, since one can open the files and directly inspect
them, or even append commands manually with »copy con file«.

Sockets

This is slightly less transparent than files, but has the
advantage that it becomes very easy to have the two
processes running on different computers later, if this
should ever be required. Debugging should be possible
by a man-in-the-middle proxy that prints all information
it sees or by connecting to the server with a terminal.

JNI

JNI might be used to access code written in C or
ABI-compatible languages. This should be fast, but I heard
that it is error prone to write JNI code and needs some
learning (code less maintainable)?
 
R

Robert Klemme

»X« below is another language than Java, for example,
VBA, C#, or C.

When an X process and a Java process have to exchange
information on the same computer, what possibilites are
there? The Java process should act as a client, sending
commands to the X process and also wants to read answers
from the X process. So, the X process is a kind of server.

My criteria are: reliability and it should not be extremely
slow (say exchanging a string should not take more than
about 10 ms). The main criterion is reliability.

»Reliability« means little risk of creating problems, little
risk of failure at run-time. (It might help when the client
[=Java process] can reset the communication to a known and
sane start state in case of problems detected at run-time.)

The host OS is Windows, but a portable solution won't hurt.

A list of possibilities I am aware of now:

Pipes

I have no experience with this. I heard one can establish
a new process »proc« with »exec« and then use

BufferedWriter out = new BufferedWriter(new
OutputStreamWriter(proc.getOutputStream()));
BufferedReader in = new BufferedReader(new
InputStreamReader(proc.getInputStream()));

A pipes is just 1:1 communication and only in 1 direction.
Files

One process writes to the end of a file, the other reads
from the end of the file? - I never tried this, don't know
if it is guaranteed to work that one process can detect and
read, whether the other has just appended something to a file.

You can, but what do you do with the ever increasing file? This is not
reliable since the filesystem will fill up at some point.
What if the processes run very long and the files get too
large? But OTOH this is very transparent, which makes it easy
to debug, since one can open the files and directly inspect
them, or even append commands manually with »copy con file«.

Sockets

This is slightly less transparent than files, but has the
advantage that it becomes very easy to have the two
processes running on different computers later, if this
should ever be required. Debugging should be possible
by a man-in-the-middle proxy that prints all information
it sees or by connecting to the server with a terminal.

You can as well use a packet sniffer (Wireshark for example). If you
use a standard protocol you'll typically have encoding functionality in
the tool.
JNI

JNI might be used to access code written in C or
ABI-compatible languages. This should be fast, but I heard
that it is error prone to write JNI code and needs some
learning (code less maintainable)?

That would be a clumsy approach IMHO.

I'd pick a higher level protocol such as

- SOAP (XML based, ubiquitous)
- CORBA (a little out of fashion but quite efficient in terms of network
transport)

Advantage: you can focus on definition of the API and need not take care
of all the nifty details. Choice should also depend on the availability
for language X, of course.

Kind regards

robert
 
A

Arne Vajhøj

»X« below is another language than Java, for example,
VBA, C#, or C.

When an X process and a Java process have to exchange
information on the same computer, what possibilites are
there? The Java process should act as a client, sending
commands to the X process and also wants to read answers
from the X process. So, the X process is a kind of server.

My criteria are: reliability and it should not be extremely
slow (say exchanging a string should not take more than
about 10 ms). The main criterion is reliability.

»Reliability« means little risk of creating problems, little
risk of failure at run-time. (It might help when the client
[=Java process] can reset the communication to a known and
sane start state in case of problems detected at run-time.)

The host OS is Windows, but a portable solution won't hurt.

A list of possibilities I am aware of now:

Pipes

I have no experience with this. I heard one can establish
a new process »proc« with »exec« and then use

BufferedWriter out = new BufferedWriter(new
OutputStreamWriter(proc.getOutputStream()));
BufferedReader in = new BufferedReader(new
InputStreamReader(proc.getInputStream()));

That would require the client to start the server.

Does not look as a good solution.
Files

One process writes to the end of a file, the other reads
from the end of the file? - I never tried this, don't know
if it is guaranteed to work that one process can detect and
read, whether the other has just appended something to a file.

What if the processes run very long and the files get too
large? But OTOH this is very transparent, which makes it easy
to debug, since one can open the files and directly inspect
them, or even append commands manually with »copy con file«.

It should work, but it will be slow.
Sockets

This is slightly less transparent than files, but has the
advantage that it becomes very easy to have the two
processes running on different computers later, if this
should ever be required. Debugging should be possible
by a man-in-the-middle proxy that prints all information
it sees or by connecting to the server with a terminal.

That would be my choice.

JNI

JNI might be used to access code written in C or
ABI-compatible languages. This should be fast, but I heard
that it is error prone to write JNI code and needs some
learning (code less maintainable)?


JNI would mean single process.

It does fit with your problem description.

JNI is a bit tricky, but it is not more difficult than
many other things. But since Java programmers very rarely
use JNI, then most Java programmers never learn JNI properly
with the expected result. You could learn JNI if you need to.

Arne
 
A

Arne Vajhøj

»X« below is another language than Java, for example,
VBA, C#, or C.

When an X process and a Java process have to exchange
information on the same computer, what possibilites are
there? The Java process should act as a client, sending
commands to the X process and also wants to read answers
from the X process. So, the X process is a kind of server.

My criteria are: reliability and it should not be extremely
slow (say exchanging a string should not take more than
about 10 ms). The main criterion is reliability.

»Reliability« means little risk of creating problems, little
risk of failure at run-time. (It might help when the client
[=Java process] can reset the communication to a known and
sane start state in case of problems detected at run-time.)

The host OS is Windows, but a portable solution won't hurt.

A list of possibilities I am aware of now:

Pipes

I have no experience with this. I heard one can establish
a new process »proc« with »exec« and then use

BufferedWriter out = new BufferedWriter(new
OutputStreamWriter(proc.getOutputStream()));
BufferedReader in = new BufferedReader(new
InputStreamReader(proc.getInputStream()));

A pipes is just 1:1 communication and only in 1 direction.

That type of pipe is bidirectional.

And Windows named pipes are bidirectional as well.
You can, but what do you do with the ever increasing file? This is not
reliable since the filesystem will fill up at some point.

It would be possible to switchover to a new file and
delete the old file if he really wanted to go this route.
I'd pick a higher level protocol such as

- SOAP (XML based, ubiquitous)
- CORBA (a little out of fashion but quite efficient in terms of network
transport)

Advantage: you can focus on definition of the API and need not take care
of all the nifty details. Choice should also depend on the availability
for language X, of course.

They will use socket as transport.

But if the X language has a good SOAP toolkit, then it would
certainly make things a lot easier.

Arne
 
A

Arved Sandstrom

»X« below is another language than Java, for example,
VBA, C#, or C.

When an X process and a Java process have to exchange
information on the same computer, what possibilites are
there? The Java process should act as a client, sending
commands to the X process and also wants to read answers
from the X process. So, the X process is a kind of server.

My criteria are: reliability and it should not be extremely
slow (say exchanging a string should not take more than
about 10 ms). The main criterion is reliability. [ SNIP ]

Files

One process writes to the end of a file, the other reads
from the end of the file? - I never tried this, don't know
if it is guaranteed to work that one process can detect and
read, whether the other has just appended something to a file.

What if the processes run very long and the files get too
large? But OTOH this is very transparent, which makes it easy
to debug, since one can open the files and directly inspect
them, or even append commands manually with »copy con file«.
[ SNIP ]

A logical subset of files for IPC is database tables.

AHS
 
J

Jeff Higgins

»X« below is another language than Java, for example,
VBA, C#, or C.

When an X process and a Java process have to exchange
information on the same computer, what possibilites are
there? The Java process should act as a client, sending
commands to the X process and also wants to read answers
from the X process. So, the X process is a kind of server.

My criteria are: reliability and it should not be extremely
slow (say exchanging a string should not take more than
about 10 ms). The main criterion is reliability.

»Reliability« means little risk of creating problems, little
risk of failure at run-time. (It might help when the client
[=Java process] can reset the communication to a known and
sane start state in case of problems detected at run-time.)

The host OS is Windows, but a portable solution won't hurt.
For Windows platform:
<http://msdn.microsoft.com/en-us/library/windows/desktop/aa365574(v=vs.85).aspx>
Prune for Java/X support, prune again for your choice of protocol.

snip
 
S

Stefan Ram


Thanks for the answers so far! So I might go for sockets,
because I like that fact that I do not need any additional
libraries under C (where one can use winsocks) and under
Java (where they are part of Java SE).
 
R

Robert Klemme

»X« below is another language than Java, for example,
VBA, C#, or C.

When an X process and a Java process have to exchange
information on the same computer, what possibilites are
there? The Java process should act as a client, sending
commands to the X process and also wants to read answers
from the X process. So, the X process is a kind of server.

My criteria are: reliability and it should not be extremely
slow (say exchanging a string should not take more than
about 10 ms). The main criterion is reliability.

»Reliability« means little risk of creating problems, little
risk of failure at run-time. (It might help when the client
[=Java process] can reset the communication to a known and
sane start state in case of problems detected at run-time.)

The host OS is Windows, but a portable solution won't hurt.

A list of possibilities I am aware of now:

Pipes

I have no experience with this. I heard one can establish
a new process »proc« with »exec« and then use

BufferedWriter out = new BufferedWriter(new
OutputStreamWriter(proc.getOutputStream()));
BufferedReader in = new BufferedReader(new
InputStreamReader(proc.getInputStream()));

A pipes is just 1:1 communication and only in 1 direction.

That type of pipe is bidirectional.

Well, that are actually two pipes aren't they? Or it's a socketpair,
depending on platform. Also, this approach only works if the Java
process always starts the other process. Alternatively the other
process would start the Java process this way and we can read from
System.in and write to System.out.
And Windows named pipes are bidirectional as well.

Oh, I didn't knew that. Learn something new every day. Thanks!
It would be possible to switchover to a new file and
delete the old file if he really wanted to go this route.

Well, yes, but that soon gets nasty because of file locking etc.
They will use socket as transport.

But if the X language has a good SOAP toolkit, then it would
certainly make things a lot easier.

Exactly.

Cheers

robert
 
J

Jeffrey H. Coffield

Sockets

This is slightly less transparent than files, but has the
advantage that it becomes very easy to have the two
processes running on different computers later, if this
should ever be required. Debugging should be possible
by a man-in-the-middle proxy that prints all information
it sees or by connecting to the server with a terminal.

SOAP has been mentioned, but I would also look at REST. An http post
with an XML response although less powerful, has a wider range of
support. Using port 80/443 to get to a server also greatly simplifies
firewall issues when the systems are remote.
JNI

JNI might be used to access code written in C or
ABI-compatible languages. This should be fast, but I heard
that it is error prone to write JNI code and needs some
learning (code less maintainable)?
The biggest drawback to JNI (I feel) is that it opens up all the
disadvantages of C in a Java environment. It is difficult (for me) at
times to determine exactly where an error actually is as I use C only
when forced to.

Jeff Coffield
www.digitalsynergyinc.com
 
J

Jan Burse

markspace said:
What Java API do you use for that?


One solution would be to port MemoryFiles from
Android to Java SE. The API of MemoryFiles is
seen here:

http://developer.android.com/reference/android/os/MemoryFile.html

You can also find the source code of the classes.
But suggesting the above has more to do with my
obsession for memory files (just joking).

But the following stack overflow entry lists 5 (five)
alternative ways do deal with shared memory in Java:

http://stackoverflow.com/questions/1491519/any-concept-of-shared-memory-in-java

Bye
 
R

Roedy Green

When an X process and a Java process have to exchange
information on the same computer, what possibilites are
there?

TCP/IP socket.

both talk to SQL database, presume data cached.

both read and write same file on SSD

JNI
 
R

Roedy Green

both read and write same file on SSD

Let's say you used a simple RandomAccessFile. How could you implement
a busy lock field in the file to indicate the file was busy being
updated? or busy being read? In RAM you have test and set locks to
check a value and set the value in one atomic operation. How could
you simulate that without test and set hardware on the SSD? You can't
very well share a RAM lock between separate jobs.
 
J

Jan Burse

Roedy said:
Let's say you used a simple RandomAccessFile. How could you implement
a busy lock field in the file to indicate the file was busy being
updated? or busy being read? In RAM you have test and set locks to
check a value and set the value in one atomic operation. How could
you simulate that without test and set hardware on the SSD? You can't
very well share a RAM lock between separate jobs.

What do you want, a write lock or a read lock?
Here is a write lock:

Obtain the lock:
raf = new RandomAccessFile(file, "rw");

fo = new FileOutputStream(raf.getFD());
fo.getChannel().lock(0, Long.MAX_VALUE, false);

Release the lock:
fo.close();

raf.close();

Maybe it can be done even simpler, but the above
works for me over process / jvm boundaries. Can
be also used to synchronize jvm with non-jvm code.

Similar code I use to obtain a read lock, via an
FileInputStream and the lock() methods third
argument =true. Currently seems also to work on
Android, but did not yet thoroughly test...

Bye

(*)
http://docs.oracle.com/javase/1.4.2...ls/FileChannel.html#lock(long, long, boolean)
 
A

Arne Vajhøj

SOAP has been mentioned, but I would also look at REST. An http post
with an XML response although less powerful, has a wider range of
support. Using port 80/443 to get to a server also greatly simplifies
firewall issues when the systems are remote.

That is given by the HTTP transport more than the RPC style SOAP
vs RESTful POX.

Arne
 
A

Arne Vajhøj

On 02/03/2012 08:52 PM, Stefan Ram wrote:
»X« below is another language than Java, for example,
VBA, C#, or C.

When an X process and a Java process have to exchange
information on the same computer, what possibilites are
there? The Java process should act as a client, sending
commands to the X process and also wants to read answers
from the X process. So, the X process is a kind of server.

My criteria are: reliability and it should not be extremely
slow (say exchanging a string should not take more than
about 10 ms). The main criterion is reliability.

»Reliability« means little risk of creating problems, little
risk of failure at run-time. (It might help when the client
[=Java process] can reset the communication to a known and
sane start state in case of problems detected at run-time.)

The host OS is Windows, but a portable solution won't hurt.

A list of possibilities I am aware of now:

Pipes

I have no experience with this. I heard one can establish
a new process »proc« with »exec« and then use

BufferedWriter out = new BufferedWriter(new
OutputStreamWriter(proc.getOutputStream()));
BufferedReader in = new BufferedReader(new
InputStreamReader(proc.getInputStream()));

A pipes is just 1:1 communication and only in 1 direction.

That type of pipe is bidirectional.

Well, that are actually two pipes aren't they? Or it's a socketpair,
depending on platform.

The Java Process supports in and out.

Whether the OS does it via single bidirectional or two unidirectional
does not change the Java code.

Thinking of it then two sounds more likely as Java also need to
separate err and out - that would be a lot easier with two.
Also, this approach only works if the Java
process always starts the other process.
Yep.


Well, yes, but that soon gets nasty because of file locking etc.

Some coding required.

Arne
 
A

Arne Vajhøj

TCP/IP socket.

both talk to SQL database, presume data cached.

both read and write same file on SSD

JNI

If you had bothered read the entire post, then you may have
been able to avoid repeating those already listed in the
post.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top