Created
March 3, 2012 05:21
-
-
Save coderplay/1964539 to your computer and use it in GitHub Desktop.
HDFS WatchService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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