-
Notifications
You must be signed in to change notification settings - Fork 5
/
DirectoryWatcherVerticle.java
82 lines (69 loc) · 2.04 KB
/
DirectoryWatcherVerticle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package org.genericsystem.watch;
import java.io.IOException;
import java.nio.file.ClosedWatchServiceException;
import java.nio.file.Path;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonObject;
public class DirectoryWatcherVerticle extends AbstractVerticle {
private final Path folder;
private final String address;
private WatchService watcher;
// Creates a verticle that publishes an event to the given address when a new file is created in the given folder.
public DirectoryWatcherVerticle(Path folder, String address) {
this.folder = folder;
this.address = address;
folder.toFile().mkdirs();
}
@Override
public void start() throws Exception {
try {
watcher = folder.getFileSystem().newWatchService();
folder.register(watcher, StandardWatchEventKinds.ENTRY_CREATE);
vertx.setPeriodic(1000, new Handler<Long>() {
@Override
public void handle(Long timerId) {
processEvents();
}
});
} catch (ClosedWatchServiceException | IOException e) {
throw new IllegalStateException(e);
}
}
public void processEvents() {
while (true) {
WatchKey key = watcher.poll();
if (key == null)
break;
for (WatchEvent<?> event : key.pollEvents()) {
@SuppressWarnings("unchecked")
WatchEvent<Path> ev = (WatchEvent<Path>) event;
WatchEvent.Kind<?> kind = ev.kind();
if (kind == StandardWatchEventKinds.OVERFLOW)
continue;
Path filename = ev.context();
JsonObject watchMsg = new JsonObject();
watchMsg.put("filename", filename.toString());
watchMsg.put("folder", folder.toString());
watchMsg.put("kind", kind.name());
vertx.eventBus().publish(address, watchMsg.encodePrettily());
}
boolean valid = key.reset();
if (!valid) {
break;
}
}
}
@Override
public void stop() {
try {
watcher.close();
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}