WatchService
Əgər sizə java vasitəsilə qovluğu dinləmək lazım gəlirsə nəcə etməli? Web proqramçıların ağlına ilk gələn Listener-lərdir. Lakin desktop tətbiqində Listeneri əvəz edici bir termin var. WatchService-lər. WatchService-lərin əsas mühüm cəhəti onun yalnız jdk 7 və jdk 8-ci versiyalarında işləməsidir. Aşağıda WatchService-ə aid nümunə yerləşdirirəm. Siz WatchService vasitəsilə qovluqda baş verən dəyişiklikləri izləyə bilərsiniz. Fayl-in yaradılması, dəyişdirilməsi, silinməsi hadisələrini nəzarətdə saxlaya bilərsiniz və bu hadisələr baş verdikdə hər hansı bir əməliyyatı yerinə yetirə bilərsiniz.
ENTRY_CREATE-faylın yaradılması hadisəsi
ENTRY_DELETE- faylın silinməsi hadisəsi
ENTRY_MODIFY-faylın dəyişdirilməsi hadisəsi
//WatchService-in elan olunması
WatchService watcher = FileSystems.getDefault().newWatchService();
//İzlənən qovluğun elan olunması
Path dir = "C:\\folder";
dir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
while (true) {
WatchKey key;
try {
// wait for a key to be available
key = watcher.take();
} catch (InterruptedException ex) {
key = watcher.take();
}
for (WatchEvent<?> event : key.pollEvents()) {
// get event type
WatchEvent.Kind<?> kind = event.kind();
// get file name
@SuppressWarnings("unchecked")
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path fileName = ev.context();
if (kind == StandardWatchEventKinds.OVERFLOW) {
continue;
} else if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
} else if (kind == StandardWatchEventKinds.ENTRY_DELETE) {
// process delete event
} else if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
}
}
// IMPORTANT: The key must be reset after processed
boolean valid = key.reset();
if (!valid) {
break;
}
}
}catch (Exception e){
e.printStackTrace();
}
ENTRY_CREATE-faylın yaradılması hadisəsi
ENTRY_DELETE- faylın silinməsi hadisəsi
ENTRY_MODIFY-faylın dəyişdirilməsi hadisəsi
//WatchService-in elan olunması
WatchService watcher = FileSystems.getDefault().newWatchService();
//İzlənən qovluğun elan olunması
Path dir = "C:\\folder";
dir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
while (true) {
WatchKey key;
try {
// wait for a key to be available
key = watcher.take();
} catch (InterruptedException ex) {
key = watcher.take();
}
for (WatchEvent<?> event : key.pollEvents()) {
// get event type
WatchEvent.Kind<?> kind = event.kind();
// get file name
@SuppressWarnings("unchecked")
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path fileName = ev.context();
if (kind == StandardWatchEventKinds.OVERFLOW) {
continue;
} else if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
} else if (kind == StandardWatchEventKinds.ENTRY_DELETE) {
// process delete event
} else if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
}
}
// IMPORTANT: The key must be reset after processed
boolean valid = key.reset();
if (!valid) {
break;
}
}
}catch (Exception e){
e.printStackTrace();
}
Comments
Post a Comment