-
Notifications
You must be signed in to change notification settings - Fork 214
/
ClusterUtil.java
145 lines (127 loc) · 5.58 KB
/
ClusterUtil.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* 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.cluster;
import javax.annotation.concurrent.Immutable;
import org.apache.pekko.actor.ActorContext;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorRefFactory;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.actor.PoisonPill;
import org.apache.pekko.actor.Props;
import org.apache.pekko.actor.SupervisorStrategy;
import org.apache.pekko.cluster.singleton.ClusterSingletonManager;
import org.apache.pekko.cluster.singleton.ClusterSingletonManagerSettings;
import org.apache.pekko.cluster.singleton.ClusterSingletonProxy;
import org.apache.pekko.cluster.singleton.ClusterSingletonProxySettings;
/**
* Convenience methods to operate an Pekko cluster.
*/
@Immutable
public final class ClusterUtil {
private ClusterUtil() {
throw new AssertionError();
}
/**
* Start a proxy to a singleton actor.
*
* @param context context to create the proxy actor in.
* @param role role of the singleton actor.
* @param singleton actor reference of the singleton manager.
* @return actor reference of the proxy to the singleton actor.
*/
public static ActorRef startSingletonProxy(final ActorContext context, final String role,
final ActorRef singleton) {
final ClusterSingletonProxySettings settings =
ClusterSingletonProxySettings.create(context.system()).withRole(role);
final Props proxyProps = ClusterSingletonProxy.props(singleton.path().toStringWithoutAddress(), settings);
return context.actorOf(proxyProps, singleton.path().name() + "Proxy");
}
/**
* Start a cluster singleton actor.
*
* @param context context of the actor whose child the cluster singleton manager shall be.
* @param role role of this cluster member.
* @param actorName name of the singleton actor.
* @param props Props of the singleton actor.
* @return reference of the singleton actor.
*/
public static ActorRef startSingleton(final ActorContext context,
final String role,
final String actorName,
final Props props) {
return startSingleton(context.system(), context, role, actorName, props);
}
/**
* Start a cluster singleton actor.
*
* @param context context of the actor whose child the cluster singleton manager shall be.
* @param role role of this cluster member.
* @param actorName name of the singleton actor.
* @param props Props of the singleton actor.
* @param supervisorStrategy the {@link SupervisorStrategy} for the singleton actor.
* @return reference of the singleton actor.
*/
public static ActorRef startSingleton(final ActorContext context,
final String role,
final String actorName,
final Props props,
final SupervisorStrategy supervisorStrategy) {
return startSingleton(context.system(), context, role, actorName, props, supervisorStrategy);
}
/**
* Start a cluster singleton actor.
*
* @param system the actor system.
* @param actorRefFactory where the cluster singleton should be created.
* @param role role of this cluster member.
* @param actorName name of the singleton actor.
* @param props Props of the singleton actor.
* @return reference of the singleton actor.
*/
public static ActorRef startSingleton(final ActorSystem system,
final ActorRefFactory actorRefFactory,
final String role,
final String actorName,
final Props props) {
final ClusterSingletonManagerSettings settings =
ClusterSingletonManagerSettings.create(system).withRole(role);
final Props supervisorProps = ClusterSingletonSupervisorActor.props(props);
final Props singletonManagerProps =
ClusterSingletonManager.props(supervisorProps, PoisonPill.getInstance(), settings);
return actorRefFactory.actorOf(singletonManagerProps, actorName);
}
/**
* Start a cluster singleton actor.
*
* @param system the actor system.
* @param actorRefFactory where the cluster singleton should be created.
* @param role role of this cluster member.
* @param actorName name of the singleton actor.
* @param props Props of the singleton actor.
* @param supervisorStrategy the {@link SupervisorStrategy} for the singleton actor.
* @return reference of the singleton actor.
*/
public static ActorRef startSingleton(final ActorSystem system,
final ActorRefFactory actorRefFactory,
final String role,
final String actorName,
final Props props,
final SupervisorStrategy supervisorStrategy) {
final ClusterSingletonManagerSettings settings =
ClusterSingletonManagerSettings.create(system).withRole(role);
final Props supervisorProps = ClusterSingletonSupervisorActor.props(props, supervisorStrategy);
final Props singletonManagerProps =
ClusterSingletonManager.props(supervisorProps, PoisonPill.getInstance(), settings);
return actorRefFactory.actorOf(singletonManagerProps, actorName);
}
}