file watch

This commit is contained in:
2021-11-14 13:18:29 +08:00
parent c99bad22cb
commit 9f5654e75d
4 changed files with 51 additions and 1 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
out/
build
classes
.DS_Store

View File

@@ -3,7 +3,9 @@ package me.hatter.sample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling
@ComponentScan(basePackages = "me.hatter.sample")
@SpringBootApplication
public class Application {

View File

@@ -0,0 +1,31 @@
package me.hatter.sample.common.misc;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
public class WatchFolder {
public static void main(String[] args) throws IOException {
WatchService watchService = FileSystems.getDefault().newWatchService();
Path directory = new File(".").toPath();
WatchKey watchKey = directory.register(watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_MODIFY,
StandardWatchEventKinds.ENTRY_DELETE
);
while (true) {
for (WatchEvent<?> event : watchKey.pollEvents()) {
WatchEvent<Path> pathEvent = (WatchEvent<Path>) event;
Path fileName = pathEvent.context();
WatchEvent.Kind<?> kind = event.kind();
System.out.println("[WatchFolder] " + kind + ", " + fileName);
}
boolean valid = watchKey.reset();
if (!valid) {
break;
}
}
}
}

View File

@@ -0,0 +1,16 @@
package me.hatter.sample.common.schedule;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
@Component
public class CustomScheduler {
@Scheduled(cron = "0/5 * * * * *")
public void run() {
System.out.println("[CustomScheduler] Time now is : "
+ LocalTime.now().format(DateTimeFormatter.ofPattern("hh mm ss")));
}
}