Skip to content

Instantly share code, notes, and snippets.

@coderplay
Created March 3, 2012 05:21
Show Gist options
  • Save coderplay/1964539 to your computer and use it in GitHub Desktop.
Save coderplay/1964539 to your computer and use it in GitHub Desktop.
HDFS WatchService
package com.taobao.dw.hdfs.file;
import org.apache.hadoop.fs.Path;
public class TestWatchService {
public static void main(String[] args) throws IOException {
AbstractWatchService service = WatchServiceFactory.newWatchService();
service.register(new Path("/user/min/"),
StandardWatchEventKinds.ENTRY_CREATE);
while (true) {
try {
WatchKey key = service.take();
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path eventPath = ev.context();
String realPath = eventPath.getName();
System.out.println(realPath);
}
boolean valid = key.reset();
if (!valid) {
break;
}
} catch (InterruptedException x) {
x.printStackTrace();
continue;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment