Skip to content

Commit 7397adb

Browse files
author
nipa
committed
Launch REST endpoint
1 parent 869dd3a commit 7397adb

File tree

4 files changed

+61
-41
lines changed

4 files changed

+61
-41
lines changed

compile.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,5 @@ javac9 \
7272
$(find monitor -name '*.java')
7373
jar9 --create \
7474
--file mods/monitor.jar \
75-
--main-class monitor.Monitor \
75+
--main-class monitor.Main \
7676
-C classes/monitor .

monitor/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
requires monitor.observer.beta;
55
requires monitor.statistics;
66
requires monitor.persistence;
7+
requires monitor.rest;
78
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package monitor;
2+
3+
import monitor.observer.ServiceObserver;
4+
import monitor.observer.alpha.AlphaServiceObserver;
5+
import monitor.observer.beta.BetaServiceObserver;
6+
import monitor.persistence.StatisticsRepository;
7+
import monitor.rest.MonitorServer;
8+
import monitor.statistics.Statistician;
9+
import monitor.statistics.Statistics;
10+
11+
import java.util.List;
12+
import java.util.concurrent.Executors;
13+
import java.util.concurrent.ScheduledExecutorService;
14+
import java.util.concurrent.TimeUnit;
15+
import java.util.stream.Stream;
16+
17+
import static java.util.stream.Collectors.toList;
18+
19+
public class Main {
20+
21+
public static void main(String[] args) {
22+
Monitor monitor = createMonitor();
23+
24+
MonitorServer server = MonitorServer
25+
.create(monitor::currentStatistics)
26+
.start();
27+
28+
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
29+
scheduler.scheduleAtFixedRate(monitor::updateStatistics, 1, 1, TimeUnit.SECONDS);
30+
scheduler.schedule(() -> {
31+
scheduler.shutdown();
32+
server.shutdown();
33+
},
34+
10,
35+
TimeUnit.SECONDS);
36+
}
37+
38+
private static Monitor createMonitor() {
39+
List<ServiceObserver> observers = Stream.of("alpha-1", "alpha-2", "alpha-3", "beta-1")
40+
.map(Main::createObserver)
41+
.collect(toList());
42+
Statistician statistician = new Statistician();
43+
StatisticsRepository repository = new StatisticsRepository();
44+
Statistics initialStatistics = repository.load().orElseGet(statistician::emptyStatistics);
45+
46+
return new Monitor(observers, statistician, repository, initialStatistics);
47+
}
48+
49+
private static ServiceObserver createObserver(String serviceName) {
50+
return AlphaServiceObserver.createIfAlphaService(serviceName)
51+
.or(() -> BetaServiceObserver.createIfBetaService(serviceName))
52+
.orElseThrow(IllegalArgumentException::new);
53+
}
54+
55+
}

monitor/src/main/java/monitor/Monitor.java

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,35 @@
22

33
import monitor.observer.DiagnosticDataPoint;
44
import monitor.observer.ServiceObserver;
5-
import monitor.observer.alpha.AlphaServiceObserver;
6-
import monitor.observer.beta.BetaServiceObserver;
75
import monitor.persistence.StatisticsRepository;
86
import monitor.statistics.Statistician;
97
import monitor.statistics.Statistics;
108

119
import java.util.List;
12-
import java.util.concurrent.Executors;
13-
import java.util.concurrent.ScheduledExecutorService;
14-
import java.util.concurrent.TimeUnit;
15-
import java.util.stream.Stream;
1610

1711
import static java.util.Objects.requireNonNull;
1812
import static java.util.stream.Collectors.toList;
1913

2014
public class Monitor {
2115

22-
private final ScheduledExecutorService scheduler;
2316
private final List<ServiceObserver> serviceObservers;
2417
private final Statistician statistician;
2518
private final StatisticsRepository repository;
2619

2720
private Statistics currentStatistics;
2821

29-
Monitor(
30-
ScheduledExecutorService scheduler,
22+
public Monitor(
3123
List<ServiceObserver> serviceObservers,
3224
Statistician statistician,
3325
StatisticsRepository repository,
3426
Statistics initialStatistics) {
35-
this.scheduler = requireNonNull(scheduler);
3627
this.serviceObservers = requireNonNull(serviceObservers);
3728
this.statistician = requireNonNull(statistician);
3829
this.repository = requireNonNull(repository);
3930
this.currentStatistics = requireNonNull(initialStatistics);
4031
}
4132

42-
public void startGatheringStatisticsEvery(long period, TimeUnit unit) {
43-
scheduler.scheduleAtFixedRate(this::updateStatistics, period, period, unit);
44-
}
45-
46-
public void stopGatheringStatistics() {
47-
scheduler.shutdown();
48-
}
49-
50-
private void updateStatistics() {
33+
public void updateStatistics() {
5134
List<DiagnosticDataPoint> newDataPoints = serviceObservers.stream()
5235
.map(ServiceObserver::gatherDataFromService)
5336
.collect(toList());
@@ -56,27 +39,8 @@ private void updateStatistics() {
5639
repository.store(newStatistics);
5740
}
5841

59-
// CREATE AND RUN
60-
61-
public static void main(String[] args) {
62-
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
63-
List<ServiceObserver> observers = Stream.of("alpha-1", "alpha-2", "alpha-3", "beta-1")
64-
.map(Monitor::createObserver)
65-
.collect(toList());
66-
Statistician statistician = new Statistician();
67-
StatisticsRepository repository = new StatisticsRepository();
68-
Statistics initialStatistics = repository.load().orElseGet(statistician::emptyStatistics);
69-
70-
Monitor monitor = new Monitor(scheduler, observers, statistician, repository, initialStatistics);
71-
monitor.startGatheringStatisticsEvery(1, TimeUnit.SECONDS);
72-
73-
scheduler.schedule(monitor::stopGatheringStatistics, 10, TimeUnit.SECONDS);
74-
}
75-
76-
private static ServiceObserver createObserver(String serviceName) {
77-
return AlphaServiceObserver.createIfAlphaService(serviceName)
78-
.or(() -> BetaServiceObserver.createIfBetaService(serviceName))
79-
.orElseThrow(IllegalArgumentException::new);
42+
public Statistics currentStatistics() {
43+
return currentStatistics;
8044
}
8145

8246
}

0 commit comments

Comments
 (0)