file watch
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
|
out/
|
||||||
build
|
build
|
||||||
classes
|
classes
|
||||||
.DS_Store
|
.DS_Store
|
||||||
@@ -7,4 +8,4 @@ classes
|
|||||||
.settings
|
.settings
|
||||||
*.iml
|
*.iml
|
||||||
*.ipr
|
*.ipr
|
||||||
*.iws
|
*.iws
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ package me.hatter.sample;
|
|||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
|
@EnableScheduling
|
||||||
@ComponentScan(basePackages = "me.hatter.sample")
|
@ComponentScan(basePackages = "me.hatter.sample")
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class Application {
|
public class Application {
|
||||||
|
|||||||
31
src/main/java/me/hatter/sample/common/misc/WatchFolder.java
Normal file
31
src/main/java/me/hatter/sample/common/misc/WatchFolder.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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")));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user