Skip to content

Commit

Permalink
If the monitored folder does not exist, create it.
Browse files Browse the repository at this point in the history
  • Loading branch information
goodjava committed Oct 23, 2023
1 parent 5af96d4 commit b064706
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ public class HeraFileMonitor {
@Setter
private EventListener listener;


public HeraFileMonitor() {
this(TimeUnit.SECONDS.toMillis(30));
}

public HeraFileMonitor(long removeTime) {
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> {
try {
List<Pair<String, Object>> remList = Lists.newArrayList();
long now = System.currentTimeMillis();
fileMap.values().forEach(it -> {
if (now - it.getUtime().get() >= TimeUnit.SECONDS.toMillis(5)) {
if (now - it.getUtime().get() >= removeTime) {
remList.add(Pair.of(it.getFileName(), it.getFileKey()));
}
});
Expand All @@ -60,6 +63,7 @@ public HeraFileMonitor() {
}, 5, 10, TimeUnit.SECONDS);
}


public HeraFileMonitor(EventListener listener) {
this();
this.listener = listener;
Expand All @@ -69,6 +73,11 @@ public void reg(String path) throws IOException, InterruptedException {
Path directory = Paths.get(path);
File f = directory.toFile();

if (!f.exists()) {
log.info("create directory:{}", directory);
Files.createDirectories(directory);
}

Arrays.stream(f.listFiles()).forEach(it -> initFile(it));

WatchService watchService = FileSystems.getDefault().newWatchService();
Expand Down

0 comments on commit b064706

Please sign in to comment.