-
Notifications
You must be signed in to change notification settings - Fork 214
/
ShardRegionProxyActorFactory.java
75 lines (62 loc) · 2.74 KB
/
ShardRegionProxyActorFactory.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
/*
* Copyright (c) 2021 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 java.util.Optional;
import javax.annotation.concurrent.NotThreadSafe;
import org.eclipse.ditto.base.model.common.ConditionChecker;
import org.eclipse.ditto.internal.utils.cluster.config.ClusterConfig;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.cluster.sharding.ClusterSharding;
/**
* Factory for creating shard region proxy actors.
*
* @since 2.3.0
*/
@NotThreadSafe
public final class ShardRegionProxyActorFactory {
private final ShardRegionExtractor extractor;
private final ClusterSharding clusterSharding;
private ShardRegionProxyActorFactory(final ShardRegionExtractor extractor, final ClusterSharding clusterSharding) {
this.extractor = extractor;
this.clusterSharding = clusterSharding;
}
/**
* Returns a new instance of {@code ShardRegionFactory}.
*
* @return the instance.
* @throws NullPointerException if any argument is {@code null}.
*/
public static ShardRegionProxyActorFactory newInstance(final ActorSystem actorSystem,
final ClusterConfig clusterConfig) {
ConditionChecker.checkNotNull(actorSystem, "actorSystem");
ConditionChecker.checkNotNull(clusterConfig, "clusterConfig");
return new ShardRegionProxyActorFactory(ShardRegionExtractor.of(clusterConfig.getNumberOfShards(), actorSystem),
ClusterSharding.get(actorSystem));
}
/**
* Starts a proxy of a shard region specified by the cluster role and shard region name arguments.
* The actor reference is not being cached.
*
* @param shardRegionName name of the shard region.
* @param clusterRole role of cluster members where the shard region resides.
* @return reference of the shard region proxy.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if any argument is empty.
*/
public ActorRef getShardRegionProxyActor(final CharSequence clusterRole, final CharSequence shardRegionName) {
ConditionChecker.argumentNotEmpty(clusterRole, "clusterRole");
ConditionChecker.argumentNotEmpty(shardRegionName, "shardRegionName");
return clusterSharding.startProxy(shardRegionName.toString(), Optional.of(clusterRole.toString()), extractor);
}
}