-
Notifications
You must be signed in to change notification settings - Fork 5
/
FileCreateEventsHandlerVerticle.java
53 lines (47 loc) · 1.64 KB
/
FileCreateEventsHandlerVerticle.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
package org.genericsystem.watch;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.eventbus.MessageConsumer;
import io.vertx.core.json.JsonObject;
public abstract class FileCreateEventsHandlerVerticle extends AbstractVerticle {
private final String address;
public FileCreateEventsHandlerVerticle(String address) {
this.address = address;
}
@Override
public void start() throws Exception {
MessageConsumer<String> consumer = vertx.eventBus().consumer(address);
consumer.handler(message -> vertx.executeBlocking(future -> {
System.out.println();
System.out.println("Message, address: " + message.address());
System.out.println(" , body: " + message.body());
JsonObject json = new JsonObject(message.body());
String kind = json.getString("kind");
if (StandardWatchEventKinds.ENTRY_CREATE.name().equals(kind)) {
Path watchedDir = Paths.get(".", json.getString("folder").split("/"));
Path newFile = watchedDir.resolve(json.getString("filename"));
FileChannel channel = null;
try {
RandomAccessFile raf = new RandomAccessFile(newFile.toFile(), "rw");
channel = raf.getChannel();
channel.lock();
handle(newFile);
channel.close();
raf.close();
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
future.complete();
}, res -> {
if (res.failed())
throw new IllegalStateException(res.cause());
}));
}
public abstract void handle(Path newFile);
}