开发者

Simple way to talk to Java process

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 开发者_如何学Pythonwould 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?


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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜