I have this Java code which I want to use to monitor file changes into directory:
import com.validation.database.entity.EntityImportRequestsTable;
import com.validation.database.service.EntityImportRequestsService;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.nio.file.*;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
private static String folderPath = "D:\\Import";
public static void main(final String[] args) throws IOException, InterruptedException {
System.out.println("Running file verifier");
System.out.println("monitoring folder " + folderPath);
EntityImportRequestsJob sql = new EntityImportRequestsJob();
WatchService watchService = FileSystems.getDefault().newWatchService();
Path path = Paths.get(folderPath);
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
WatchKey key;
while ((key = watchService.take()) != null) {
for (WatchEvent<?> event : key.pollEvents()) {
System.out.println("Event kind:" + event.kind() + ". File affected: " + event.context() + ".");
if(event.kind().equals(StandardWatchEventKinds.ENTRY_DELETE)){
Instant start = Instant.now();
boolean flag = true;
while(flag) {
while ((key = watchService.take()) != null) {
HashMap<String, List> map = sql.checkFileImport();
List values = map.get(event.context()); // get values by file name
if(values.contains("Completed")){
// exit the monitoring while loop
flag = false;
}
}
Thread.sleep(1000);
}
Instant end = Instant.now();
System.out.println(Duration.between(start,end));
long seconds = TimeUnit.MILLISECONDS.toSeconds(Duration.between(start,end).getSeconds());
long minutes = TimeUnit.MILLISECONDS.toMinutes(Duration.between(start,end).getSeconds());
System.out.format("Execution time %d minutes %d seconds", minutes, seconds);
}
}
key.reset();
}
watchService.close();
}
How I can improve this code in order properly to track file import time?