Reputation: 34205
I'm trying to find the easiest way to request information from a Java process. From a shell script I need to pass 2 strings as arguments and get one string back. Completely synchronous and blocking.
What's the easiest way to approach it? Http is a bit heavy, but could do if nothing else is there. Pipe / unix socket communication would be simple, but requires much more maintenance code (regarding timeouts, additional native binding libraries, etc.). Own tcp communication could be ok... but it's hard to handle from a bash script.
Are there any other simple, lightweight options that don't require launching another JVM from the script?
Upvotes: 1
Views: 1064
Reputation: 8287
For simple communications, simply use files and a dirwatcher - lame but effective. With an HTTP server, I'd stick with HTTP, otherwise you can always try JMX or write-your-own socket connection.
Upvotes: 0
Reputation: 15809
I'd definitely use http. If you're already running in an app server, it's easy. If not, you might find an answer here:
http://blogs.operationaldynamics.com/andrew/software/free-java/sun-secret-webserver.html
Upvotes: 1
Reputation: 104110
You can use bash's /dev/tcp
support to read and write to TCP sockets:
In one terminal:
$ nc -l 8888
hello
why hello!
^D
In another terminal:
$ exec 6<>/dev/tcp/localhost/8888
$ echo hello >&6
$ cat <&6
why hello!
$
If you write your Java program to listen on a local socket to replace the nc -l
listener, you can use TCP with a little bit of hassle.
Upvotes: 3
Reputation: 81734
You've tagged this with Linux. If this is only supposed to work on Linux, then you could make a pair of named pipes with mkfifo, have the Java program block while reading from one (or just one thread in the Java program, if desired) and then send commands by writing to it; the Java program can send back the reply on the other one.
Upvotes: 1