SDNotify implements the systemd notification protocol in Java.
Java
Switch branches/tags
Clone or download
Latest commit 7f17421 Apr 14, 2018
Permalink
Failed to load latest commit information.
src remove dead code Apr 14, 2018
.gitignore import Feb 14, 2016
.travis.yml Rename travis.yml to .travis.yml Feb 15, 2016
LICENSE Create LICENSE Nov 15, 2016
README.md Update README.md Mar 6, 2018
pom.xml Update Dependencies Apr 10, 2018

README.md

SDNotify

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:

<dependency>
    <groupId>info.faljse</groupId>
    <artifactId>SDNotify</artifactId>
    <version>1.1</version>
</dependency>

Basic usage

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
    }
}

Status text

systemctl status will print this string

SDNotify.sendStatus("No space left on device");

Watchdog, etc.

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.

Sample .service file

systemd service documentation

[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

Build Status