Skip to content

Commit

Permalink
If the monitored folder does not exist, create it. (#748)
Browse files Browse the repository at this point in the history
Co-authored-by: wtt <[email protected]>
  • Loading branch information
goodjava and wtt40122 authored Oct 23, 2023
1 parent 72911f6 commit c537b0b
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,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 @@ -62,6 +65,7 @@ public HeraFileMonitor() {
}, 5, 10, TimeUnit.SECONDS);
}


public HeraFileMonitor(EventListener listener) {
this();
this.listener = listener;
Expand All @@ -71,6 +75,11 @@ public void reg(String path, Predicate<String> predicate) throws IOException, In
Path directory = Paths.get(path);
File f = directory.toFile();

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

Arrays.stream(Objects.requireNonNull(f.listFiles())).filter(it -> predicate.test(it.getPath())).forEach(this::initFile);

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

0 comments on commit c537b0b

Please sign in to comment.