-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(FileWatcher): close WatchService synchronously
- Loading branch information
1 parent
b4da625
commit 2e28596
Showing
3 changed files
with
35 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
@@ -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); | ||
} | ||
}); | ||
} | ||
} |