Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If the monitored folder does not exist, create it. (issue #747) #748

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading