-
Notifications
You must be signed in to change notification settings - Fork 0
/
WatchdogDemo.java
95 lines (68 loc) · 2.24 KB
/
WatchdogDemo.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
83
84
85
86
87
88
89
90
91
92
93
94
95
package xyz.downgoon.mydk.concurrent;
import java.util.concurrent.atomic.AtomicInteger;
import xyz.downgoon.mydk.testing.ConsoleCmder;
import xyz.downgoon.mydk.testing.FileTailAppender;
import xyz.downgoon.mydk.testing.MiniHttpc;
import xyz.downgoon.mydk.testing.MiniHttpd;
public class WatchdogDemo {
public static void main(String[] args) throws Exception {
MiniHttpd httpServer = new MiniHttpd();
final int port = httpServer.start().getListeningPort();
FileTailAppender httpLogger = FileTailAppender.tempFile();
System.out.println("tailLight -100f " + httpLogger.getFileName());
Watchdog.WatchJob watchJob = new Watchdog.WatchJob() {
private AtomicInteger requestCount = new AtomicInteger(0);
@Override
public void watch(long times, String name) {
MiniHttpc httpClient = new MiniHttpc(port);
httpClient.get("/", new MiniHttpc.ResponseHandler() {
@Override
public void onHeadLine(String head, int num) {
httpLogger.appendLine("<<< " + head);
}
@Override
public void onContentLength(int contentLength) {
}
@Override
public void onBodyTrunk(String trunk, int num) {
httpLogger.appendLine("<<< " + trunk);
}
@Override
public void onException(Exception e) {
httpLogger.appendLine("!!! exception occured: ");
httpLogger.appendLine(e.getMessage());
}
@Override
public void onBodyEnd() {
httpLogger.appendLine("");
httpLogger.appendLine("");
}
}, new MiniHttpc.RequestTracer() {
@Override
public void requestFlushed(String requestPath) {
httpLogger.appendLine(">>> request#" + requestCount.incrementAndGet());
httpLogger.appendLine(">>> GET " + requestPath);
}
});
} // end watch
};
// start watchdog
Watchdog watchdog = new Watchdog(watchJob, 1000L).start();
// control watchdog's action from console
new ConsoleCmder().on("s", "stop", () -> {
watchdog.stop();
}).on("p", "pause", () -> {
watchdog.pause();
}).on("r", "resume", () -> {
watchdog.resume();
}).on("v", "view", () -> {
System.out.println(watchdog.toString());
}).start();
// clean resources
if (watchdog.isAlive()) {
watchdog.stop();
}
httpLogger.closeAndDelete();
httpServer.stop();
}
}