2
2
3
3
import monitor .observer .DiagnosticDataPoint ;
4
4
import monitor .observer .ServiceObserver ;
5
- import monitor .observer .alpha .AlphaServiceObserver ;
6
- import monitor .observer .beta .BetaServiceObserver ;
7
5
import monitor .persistence .StatisticsRepository ;
8
6
import monitor .statistics .Statistician ;
9
7
import monitor .statistics .Statistics ;
10
8
11
9
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
10
17
11
import static java .util .Objects .requireNonNull ;
18
12
import static java .util .stream .Collectors .toList ;
19
13
20
14
public class Monitor {
21
15
22
- private final ScheduledExecutorService scheduler ;
23
16
private final List <ServiceObserver > serviceObservers ;
24
17
private final Statistician statistician ;
25
18
private final StatisticsRepository repository ;
26
19
27
20
private Statistics currentStatistics ;
28
21
29
- Monitor (
30
- ScheduledExecutorService scheduler ,
22
+ public Monitor (
31
23
List <ServiceObserver > serviceObservers ,
32
24
Statistician statistician ,
33
25
StatisticsRepository repository ,
34
26
Statistics initialStatistics ) {
35
- this .scheduler = requireNonNull (scheduler );
36
27
this .serviceObservers = requireNonNull (serviceObservers );
37
28
this .statistician = requireNonNull (statistician );
38
29
this .repository = requireNonNull (repository );
39
30
this .currentStatistics = requireNonNull (initialStatistics );
40
31
}
41
32
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 () {
51
34
List <DiagnosticDataPoint > newDataPoints = serviceObservers .stream ()
52
35
.map (ServiceObserver ::gatherDataFromService )
53
36
.collect (toList ());
@@ -56,27 +39,8 @@ private void updateStatistics() {
56
39
repository .store (newStatistics );
57
40
}
58
41
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 ;
80
44
}
81
45
82
46
}
0 commit comments