问题描述
我有一个Java服务(使用实现),我将从。到现在为止还挺好。现在我想使用systemd的让它了解服务状态(以或但最终两种方法都与环境变量引用的套接字进行通信。我可以在服务启动时运行任意代码,但我不确定如何最好地从Java中通知systemd。
I have a Java service (implemented using Dropwizard) which I'm launching from a user instance of systemd. So far so good. Now I would like to use systemd's notification features to make it aware of the service state (available as a C library function or shell script but eventually both methods talk to a socket referenced by an environment variable). I can run arbitrary code when the service has finished starting up, but I'm not sure how best to notify systemd from within Java.
其他开发人员在Windows上运行此服务,因此对于奖励积分,如果我的通知代码是谨慎的,并且如果没有使用systemd运行则不会导致任何输出,那将非常有用。
Other developers run this service on Windows, so for bonus points it would be really useful if my notification code were discreet and didn't cause any output if not running with systemd.
推荐答案
我发现这个实现使用系统挂钩,但至少不需要JNA / JNI。
I found this implementation which does use system hooks, but at least doesn't require JNA/JNI.
正如你所说,systemd使用环境变量引用的套接字(代码中的System.getenv(NOTIFY_SOCKET)),所以我想也可能有一种使用Java Socket的方法,虽然我想象很多研究和/或需要反复试验。
As you say, systemd uses sockets referenced by an environment variable (System.getenv("NOTIFY_SOCKET") in the code), so I'd imagine there may also be a way to use a Java Socket, although I'd imagine a lot of research and/or trial-and-error would be required.
这篇关于来自Java的sd_notify()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!