https://github.com/vincent4vx/singleinstance
Java library for prevent running multiple instances of an application, and offer a communication with the first instance.
https://github.com/vincent4vx/singleinstance
ipc java single-instance single-instance-app
Last synced: 11 months ago
JSON representation
Java library for prevent running multiple instances of an application, and offer a communication with the first instance.
- Host: GitHub
- URL: https://github.com/vincent4vx/singleinstance
- Owner: vincent4vx
- License: lgpl-3.0
- Created: 2020-08-28T12:43:24.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-08-29T12:34:59.000Z (over 5 years ago)
- Last Synced: 2025-02-05T13:14:20.666Z (about 1 year ago)
- Topics: ipc, java, single-instance, single-instance-app
- Language: Java
- Homepage:
- Size: 61.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: COPYING
Awesome Lists containing this project
README
# SingleInstance
[](https://scrutinizer-ci.com/g/vincent4vx/SingleInstance/build-status/master) [](https://scrutinizer-ci.com/g/vincent4vx/SingleInstance/?branch=master) [](https://scrutinizer-ci.com/g/vincent4vx/SingleInstance/?branch=master) [](https://javadoc.io/doc/fr.quatrevieux/single-instance) [](https://search.maven.org/artifact/fr.quatrevieux/single-instance)
Java library for prevent running multiple instances of an application, and offer a communication with the first instance.
It's based on the use of a lock file with exclusive lock to ensure that only the first process can write into this file.
For the IPC communication, a simple single thread socket server is start by the first process, and the port number is written
on the lock file.
## Installation
For installing using maven, add this dependency into the `pom.xml` :
```xml
fr.quatrevieux
single-instance
1.0
```
## Usage
### Basic usage
Use [SingleInstance](src/main/java/fr/quatrevieux/singleinstance/SingleInstance.java)`#onAlreadyRunning()` to check if an instance is running, and send a message.
Then start the IPC server using `SingleInstance#onMessages()`.
```java
/**
* Forward the arguments to the first running application
*/
public class MyApp {
public static void main(String[] args) {
// Check if there is a running instance
SingleInstance.onAlreadyRunning(instance -> {
try {
// Forward arguments to the instance
for (String arg : args) {
instance.send("Open", arg.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
// Quit the process
System.exit(0);
});
// We are on the first running instance here : initialize application
MyApp app = new MyApp();
// Start the IPC server and consume messages
SingleInstance.onMessage(message -> {
// Handle the "Open" message
if (message.name().equals("Open")) {
app.open(new String(message.data()));
}
});
// Continue execution
for (String arg : args) {
app.open(arg);
}
}
public void open(String argument) {
// ...
}
}
```
## Licence
This project is licensed under the LGPLv3 licence. See [COPYING](./COPYING) and [COPYING.LESSER](./COPYING.LESSER) files for details.