-
Notifications
You must be signed in to change notification settings - Fork 214
/
ShutdownBehaviour.java
125 lines (106 loc) · 4.47 KB
/
ShutdownBehaviour.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Copyright (c) 2019 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.base.service.actors;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import org.eclipse.ditto.base.api.common.Shutdown;
import org.eclipse.ditto.base.model.entity.id.EntityId;
import org.eclipse.ditto.base.model.entity.id.NamespacedEntityId;
import org.eclipse.ditto.internal.utils.cluster.DistPubSubAccess;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.PoisonPill;
import org.apache.pekko.cluster.pubsub.DistributedPubSubMediator;
import org.apache.pekko.japi.pf.ReceiveBuilder;
/**
* Responsible for shutting down the given actor in case a shutdown command contains a reason that is applicable for the
* information hold by this behaviour.
*/
public final class ShutdownBehaviour {
private static final Logger LOGGER = LoggerFactory.getLogger(ShutdownBehaviour.class);
private final String namespace;
private final EntityId entityId;
private final ActorRef self;
private ShutdownBehaviour(final String namespace, final EntityId entityId, final ActorRef self) {
this.namespace = checkNotNull(namespace, "namespace");
this.entityId = checkNotNull(entityId, "entityId");
this.self = checkNotNull(self, "self");
}
/**
* Create the actor behavior from its entity ID and reference.
*
* @param entityId entity ID to react to.
* @param pubSubMediator Pekko pub-sub mediator.
* @param self reference of the actor itself.
* @return the actor behavior.
*/
public static ShutdownBehaviour fromId(final NamespacedEntityId entityId,
final ActorRef pubSubMediator,
final ActorRef self) {
checkNotNull(entityId, "entityId");
return fromIdWithNamespace(entityId, pubSubMediator, self, entityId.getNamespace());
}
/**
* Create the actor behavior from its entity ID (without namespace) and reference.
*
* @param entityId entity ID to react to.
* @param pubSubMediator Pekko pub-sub mediator.
* @param self reference of the actor itself.
* @return the actor behavior.
*/
public static ShutdownBehaviour fromIdWithoutNamespace(final EntityId entityId,
final ActorRef pubSubMediator,
final ActorRef self) {
return fromIdWithNamespace(entityId, pubSubMediator, self, "");
}
/**
* Create the actor behavior from a namespace and ID.
*
* @param entityId the entity ID.
* @param pubSubMediator Pekko pub-sub mediator.
* @param self reference of the actor itself.
* @param namespace the namespace of the actor.
* @return the actor behavior.
*/
public static ShutdownBehaviour fromIdWithNamespace(final EntityId entityId,
final ActorRef pubSubMediator,
final ActorRef self,
final String namespace) {
final var shutdownBehaviour = new ShutdownBehaviour(namespace, entityId, self);
shutdownBehaviour.subscribePubSub(checkNotNull(pubSubMediator, "pubSubMediator"));
return shutdownBehaviour;
}
private void subscribePubSub(final ActorRef pubSubMediator) {
pubSubMediator.tell(DistPubSubAccess.subscribe(Shutdown.TYPE, self), self);
}
/**
* Create a new {@code ReceiveBuilder} matching on messages handled by this actor.
*
* @return new receive builder.
*/
public ReceiveBuilder createReceive() {
return ReceiveBuilder.create()
.match(Shutdown.class, this::shutdown)
.match(DistributedPubSubMediator.SubscribeAck.class, this::subscribeAck);
}
private void shutdown(final Shutdown shutdown) {
final var shutdownReason = shutdown.getReason();
if (shutdownReason.isRelevantFor(namespace) || shutdownReason.isRelevantFor(entityId)) {
LOGGER.info("Shutting down <{}> due to <{}>.", self, shutdown);
self.tell(PoisonPill.getInstance(), ActorRef.noSender());
}
}
private void subscribeAck(final DistributedPubSubMediator.SubscribeAck ack) {
// do nothing
}
}