与 Java 进程对话的简单方法
我正在尝试找到从 Java 进程请求信息的最简单方法。在 shell 脚本中,我需要传递 2 个字符串作为参数并返回一个字符串。完全同步和阻塞。
接近它的最简单方法是什么? Http 有点重,但是如果没有其他东西的话也可以。 Pipe/unix 套接字通信很简单,但需要更多的维护代码(关于超时、额外的本机绑定库等)。自己的 TCP 通信可能没问题......但很难从 bash 脚本处理。
是否还有其他简单、轻量级的选项,不需要从脚本启动另一个 JVM?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 bash 的
/dev/tcp
支持来读取和写入 TCP 套接字:在一个终端中:
在另一个终端中:
如果您编写 Java 程序来侦听本地套接字以替换 nc -l 侦听器,则可以使用 TCP,但会遇到一些麻烦。
You can use bash's
/dev/tcp
support to read and write to TCP sockets:In one terminal:
In another terminal:
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.您已经用 Linux 标记了它。如果这仅适用于 Linux,那么您可以使用 mkfifo 制作一对命名管道,在从一个(或 Java 程序中的一个线程,如果需要)读取时让 Java 程序块,然后通过写入发送命令到它; Java 程序可以发回对方的答复。
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.
我肯定会使用http。如果您已经在应用程序服务器中运行,那就很容易了。如果没有,您可能会在这里找到答案:
http:// /blogs.operationaldynamics.com/andrew/software/free-java/sun-secret-webserver.html
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
对于简单的通信,只需使用文件和目录观察器 - 蹩脚但有效。对于 HTTP 服务器,我会坚持使用 HTTP,否则你可以随时尝试 JMX 或 write-您自己的套接字连接。
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.