- var lock = fileChannel.tryLock();
- if (lock == null) {
- if (!waitForLock) {
- logger.debug("Config file is in use by another instance.");
- throw new IOException("Config file is in use by another instance.");
+ try {
+ var lock = fileChannel.tryLock();
+ if (lock == null) {
+ if (!waitForLock) {
+ logger.debug("Config file is in use by another instance.");
+ throw new IOException("Config file is in use by another instance.");
+ }
+ logger.info("Config file is in use by another instance, waiting…");
+ lock = fileChannel.lock();
+ logger.info("Config file lock acquired.");
+ }
+ final var result = new Pair<>(fileChannel, lock);
+ fileChannel = null;
+ return result;
+ } finally {
+ if (fileChannel != null) {
+ fileChannel.close();