-
Notifications
You must be signed in to change notification settings - Fork 214
/
StatusSupplierActor.java
executable file
·110 lines (97 loc) · 4.81 KB
/
StatusSupplierActor.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* 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.status;
import java.time.Duration;
import org.eclipse.ditto.internal.utils.pekko.SimpleCommand;
import org.eclipse.ditto.internal.utils.pekko.SimpleCommandResponse;
import org.eclipse.ditto.internal.utils.pekko.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.health.AbstractHealthCheckingActor;
import org.eclipse.ditto.internal.utils.health.RetrieveHealth;
import org.eclipse.ditto.internal.utils.health.StatusInfo;
import org.apache.pekko.actor.AbstractActor;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.Props;
import org.apache.pekko.event.DiagnosticLoggingAdapter;
import org.apache.pekko.japi.pf.ReceiveBuilder;
import org.apache.pekko.pattern.Patterns;
/**
* Actor supplying "status" and "health" information of an ActorSystem. Has to be started as "root" actor so that
* address is the same for all microservices in Things-Cluster.
*/
public final class StatusSupplierActor extends AbstractActor {
/**
* The name of this Actor in the ActorSystem.
*/
public static final String ACTOR_NAME = "statusSupplier";
/**
* Command name for retrieving the "status" of this instance.
*/
public static final String SIMPLE_COMMAND_RETRIEVE_STATUS = "retrieveStatus";
/**
* Command name for retrieving the "health" of this instance.
*/
public static final String SIMPLE_COMMAND_RETRIEVE_HEALTH = "retrieveHealth";
private final DiagnosticLoggingAdapter log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
private final String rootActorName;
@SuppressWarnings("unused")
private StatusSupplierActor(final String rootActorName) {
this.rootActorName = rootActorName;
}
/**
* Creates Pekko configuration object Props for this StatusSupplierActor.
*
* @param rootActorName sets the name of the root actor (e.g. "thingsRoot") which is used as the parent of
* {@link org.eclipse.ditto.internal.utils.health.AbstractHealthCheckingActor#ACTOR_NAME}.
* @return the Pekko configuration Props object
*/
public static Props props(final String rootActorName) {
return Props.create(StatusSupplierActor.class, rootActorName);
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(SimpleCommand.class, command -> SIMPLE_COMMAND_RETRIEVE_STATUS.equals(command.getCommandName()),
command -> {
log.info("Sending the status of this system as requested..");
final SimpleCommandResponse response = SimpleCommandResponse.of(
command.getCorrelationId().orElse("?"), Status.provideStaticStatus());
getSender().tell(response, getSelf());
})
.match(SimpleCommand.class, command -> SIMPLE_COMMAND_RETRIEVE_HEALTH.equals(command.getCommandName()),
command -> {
final ActorRef sender = getSender();
final ActorRef self = getSelf();
Patterns.ask(getContext().system().actorSelection("/user/" + rootActorName + "/" +
AbstractHealthCheckingActor.ACTOR_NAME),
RetrieveHealth.newInstance(), Duration.ofSeconds(2))
.thenAccept(health -> {
log.info("Sending the health of this system as requested: {}", health);
sender.tell(health, self);
})
.exceptionally(throwable -> {
sender.tell(
StatusInfo.fromStatus(StatusInfo.Status.DOWN, throwable.getMessage()),
self);
return null;
});
})
.match(SimpleCommand.class, command ->
log.warning("Unsupported SimpleCommand with name '{}': {}", command.getCommandName(),
command)
)
.matchAny(m -> {
log.warning("Unknown message: {}", m);
unhandled(m);
}).build();
}
}