I need to test file import status using this Java code:
@EventListener
public void handleContextStart(ContextRefreshedEvent eventd) throws IOException, InterruptedException {
System.out.println("Handling context started event.!!!");
System.out.println("Running file verifier");
System.out.println("monitoring folder " + folderPath);
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();
AtomicBoolean flag = new AtomicBoolean(true);
while(flag.get()) {
while ((key = watchService.take()) != null) {
List<EntityImportRequestsTable> list = entityImportRequestsService.findAll();
HashMap<String, String> map = new HashMap<>();
for(EntityImportRequestsTable item : list){
map.put(item.getRequestXmlSourceFile(), item.getStatusCode());
}
map.entrySet().forEach(entry -> {
System.out.println(entry.getKey() + " " + entry.getValue());
// compare file name from list and file name from the delete event by file name
if(entry.getKey().contains(event.context().toString())){
// exit the monitoring while loop
flag.set(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();
}
When file is processed from the tested app the file is deleted and imported into database. If the file import is successfully imported the status is completed.
When I run the above program noting happens. Do you know how I can properly track the file import?