SDNotify implements the systemd notification protocol in Java.
The Notify protocol uses datagram unix sockets, which are not accessible via Java; Therefore SDNotify includes a JNA wrapper of the socket API.
SDNotify is available via maven: central.sonatype.com
<dependency>
<groupId>info.faljse</groupId>
<artifactId>SDNotify</artifactId>
<version>1.5</version>
</dependency>
SDNotify is initialized at the first call to any send* function.
If the systemd environment isn't available, or the initialization fails a warning message is logged. All further calls to SDNotify are ignored.
import info.faljse.SDNotify.SDNotify;
public class SDTest {
public static void main(String[] args) {
initMyServer();
SDNotify.sendNotify(); //notify: ready
}
}
systemctl status
will print this string
SDNotify.sendStatus("No space left on device");
If a watchdog is configured systemd will kill the process
when SDNotify.sendWatchdog()
isn't called every n seconds.
isWatchdogEnabled()
and getWatchdogFrequency()
may be used to
determine if and at what interval sendWatchdog()
should be called.
Also available:
sendReloading()
, sendStopping()
, sendErrno()
, sendBusError()
, sendMainPID()
- see sd_notify manpage for details.
There is also sendRaw()
for unsupported/unknown/future functions.
[Unit]
Description=My Java Server
After=syslog.target
[Service]
Type=notify
NotifyAccess=all
WorkingDirectory=/opt/myserver/
ExecStart=/usr/bin/java -server -jar myserver.jar
# WatchdogSec=30
# Restart=always
[Install]
WantedBy=multi-user.target