|
| 1 | +# libdaemon-jvm |
| 2 | + |
| 3 | +*libdaemon-jvm* is a [libdaemon](http://0pointer.de/lennart/projects/libdaemon)-inspired |
| 4 | +library for the JVM written in Scala. |
| 5 | + |
| 6 | +It aims at making it easier for JVM-based daemon processes to |
| 7 | +- ensure that a single instance of it is running at a time |
| 8 | +- rely on Unix domain sockets (or Windows named pipes) to listen to incoming connections |
| 9 | + |
| 10 | +## Single process |
| 11 | + |
| 12 | +*libdaemon-jvm* relies on Java file lock mechanism to ensure only a single instance |
| 13 | +of a process is running at a time. |
| 14 | + |
| 15 | +More concretely, it is passed a directory, where it writes or creates: |
| 16 | +- a lock file |
| 17 | +- a PID file |
| 18 | +- a domain socket (except when named pipes are used on Windows) |
| 19 | + |
| 20 | +It ensures that no-two processes relying on the same directory can run at a time, relying |
| 21 | +on both the PID file and the domain socket to check for another running process. |
| 22 | + |
| 23 | +## Domain sockets |
| 24 | + |
| 25 | +*libdaemon-jvm* creates Unix domain sockets or Windows named pipes using either |
| 26 | +- the JNI Unix domain socket and Windows named pipe support in the [ipcsocket](https://github.com/sbt/ipcsocket) library |
| 27 | +- Unix domain socket support in Java >= 16 |
| 28 | + |
| 29 | +The ipcsocket library JNI support is only available on Linux / macOS / Windows for the |
| 30 | +x86_64 architecture, and macOS for the ARM64 architecture (untested). For other OSes and |
| 31 | +architectures, Java >= 16 is required. |
| 32 | + |
| 33 | +On Windows on x86_64, *libdaemon-jvm* defaults to using ipcsocket JNI-based Windows named pipes. |
| 34 | +On Windows but on a different architecture, it defaults to the Unix domain socket support of |
| 35 | +Java >= 16, that happens to also work on Windows (requires a not-too-dated Windows 10 version), |
| 36 | +but is incompatible with Windows named pipes. |
| 37 | + |
| 38 | +On other OSes, when using Java >= 16, *libdaemon-jvm* defaults to Java's own Unix domain socket |
| 39 | +support. On Java < 16, it only supports Linux on x86_64, or macOS on x86_64 or ARM64. Java >= 16 |
| 40 | +and ipcsocket JNI-based sockets can talk to each other on the same machine (no hard requirement |
| 41 | +to use Java >= 16 for both clients and servers). |
| 42 | + |
| 43 | +In all cases, when Java < 16 is supported, both Java >= 16 and Java < 16 clients and servers |
| 44 | +can talk to each other. |
| 45 | + |
| 46 | +## Usage |
| 47 | + |
| 48 | +Add the following dependency to your build |
| 49 | +```text |
| 50 | +io.github.alexarchambault.libdaemon::libdaemon:0.0.3 |
| 51 | +``` |
| 52 | +The latest version is [](https://maven-badges.herokuapp.com/maven-central/io.github.alexarchambault.libdaemon/libdaemon). |
| 53 | + |
| 54 | +From the server, call `Lock.tryAcquire`, and start accepting connections on the server socket in the thunk passed to it: |
| 55 | +```scala |
| 56 | +import libdaemonjvm.server._ |
| 57 | +import java.nio.file._ |
| 58 | + |
| 59 | +val daemonDirectory: Path = ??? // pass a directory under the user home dir, computed with directories-jvm for example |
| 60 | +val lockFiles = LockFiles.under(daemonDirectory, "my-app-name\\daemon") // second argument is the Windows named pipe path (that doesn't live in the file system) |
| 61 | +Lock.tryAcquire(lockFiles) { serverSocket: Either[ServerSocket, ServerSocketChannel] => |
| 62 | + // serverSocket is a Right(…) when Java >= 16 Unix domain socket support is used, |
| 63 | + // it's Left(…) when ipcsocket JNI support is used |
| 64 | + |
| 65 | + // you should start listening on serverSocket here, and as much as possible, |
| 66 | + // only exit this block when you are actually accepting incoming connections |
| 67 | +} |
| 68 | +``` |
0 commit comments