-
Notifications
You must be signed in to change notification settings - Fork 214
/
AbstractHealthCheckingActor.java
96 lines (78 loc) · 2.96 KB
/
AbstractHealthCheckingActor.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
96
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.utils.health;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.ditto.internal.utils.pekko.logging.DittoLoggerFactory;
import org.apache.pekko.actor.AbstractActorWithTimers;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.event.DiagnosticLoggingAdapter;
import org.apache.pekko.japi.pf.ReceiveBuilder;
/**
* Abstract base implementations for actors which check the health of a subsystem.
*/
public abstract class AbstractHealthCheckingActor extends AbstractActorWithTimers {
/**
* The required actor name of the Actor to be created in the ActorSystem.
*/
public static final String ACTOR_NAME = "healthCheckingActor";
protected final DiagnosticLoggingAdapter log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
private StatusInfo health = StatusInfo.unknown();
private Set<ActorRef> waitingHealthReceivers = new HashSet<>();
@Override
public Receive createReceive() {
return matchRetrieveHealth()
.orElse(matchCustomMessages())
.orElse(matchAny());
}
/**
* Defines a {@link Receive} for the messages to be handled by the subclass actor.
*
* @return the {@link Receive}.
*/
protected abstract Receive matchCustomMessages();
/**
* Defines the action of the subclass actor which will be triggered when its health status is requested.
*/
protected abstract void triggerHealthRetrieval();
/**
* This method has to be used by subclass actors to update the health status. An update to health will notify all
* actors which have asked for the health status since the last update.
*
* @param newHealth the new health
*/
protected final void updateHealth(final StatusInfo newHealth) {
waitingHealthReceivers.forEach(receiver -> receiver.tell(newHealth, getSelf()));
waitingHealthReceivers.clear();
health = newHealth;
}
public final StatusInfo getHealth() {
return health;
}
private Receive matchRetrieveHealth() {
return ReceiveBuilder.create()
.match(RetrieveHealth.class, retrieveHealth -> retrieveHealth())
.build();
}
private Receive matchAny() {
return ReceiveBuilder.create()
.matchAny(m -> {
log.warning("Unknown message: {}", m);
unhandled(m);
}).build();
}
private void retrieveHealth() {
waitingHealthReceivers.add(getSender());
triggerHealthRetrieval();
}
}