Skip to content

Commit

Permalink
perf(FileWatcher): close WatchService synchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Oct 26, 2020
1 parent b4da625 commit 2e28596
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.nio.file.Path;
import java.util.Optional;

/**
* Description: MediaStreamingBootstrap, change description here.
Expand Down Expand Up @@ -85,6 +86,6 @@ public void run(String... args) {
@PreDestroy
private void preDestroy() {
log.debug("Destroying {}, fileWatcher: {}", this.getClass().getSimpleName(), fileWatcher);
fileWatcher.destroy();
Optional.ofNullable(fileWatcher).ifPresent(FileWatcher::destroy);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ private void monitor() {

@SneakyThrows
public void destroy() {
WatchServiceSingleton.close();
THREAD_POOL.awaitTermination(5, TimeUnit.SECONDS);
log.debug("THREAD_POOL for FileWatcher was terminated.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
package com.github.johnnymillergh.boot.mediastreamingspringbootautoconfigure.filewatch;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.WatchService;
import java.util.Optional;

/**
* Description: WatchServiceSingleton, change description here.
*
* @author Johnny Miller (锺俊), email: [email protected], date: 10/20/2020 4:52 PM
**/
@Slf4j
public class WatchServiceSingleton {
private static volatile WatchService singletonInstance;

/**
* Private constructors are required for Singleton classes so that objects of such classes cannot be instantiated
* from outside the class. If Singleton classes do not have private constructors, objects of such classes can be
* created from outside too and hence the class will no longer be Singleton class.
*
* @author Johnny Miller (锺俊), email: [email protected], date: 10/26/2020 1:57 PM
*/
private WatchServiceSingleton() {
}

/**
* Gets WatchService instance. Minimize synchronized range, with double null check.
*
* @return the WatchService instance
* @author Johnny Miller (锺俊), email: [email protected], date: 10/26/2020 1:51 PM
*/
@SneakyThrows
public static WatchService getInstance() {
if (singletonInstance == null) {
Expand All @@ -27,4 +44,19 @@ public static WatchService getInstance() {
}
return singletonInstance;
}

/**
* Close WatchService synchronously.
*
* @author Johnny Miller (锺俊), email: [email protected], date: 10/26/2020 1:49 PM
*/
public static synchronized void close() {
Optional.ofNullable(singletonInstance).ifPresent(watchService -> {
try {
watchService.close();
} catch (IOException e) {
log.error("Exception occurred when closing WatchService.", e);
}
});
}
}

0 comments on commit 2e28596

Please sign in to comment.