diff --git a/cluster-sharding-typed/src/test/java/jdocs/org/apache/pekko/cluster/sharding/typed/ExternalShardAllocationCompileOnlyTest.java b/cluster-sharding-typed/src/test/java/jdocs/org/apache/pekko/cluster/sharding/typed/ExternalShardAllocationCompileOnlyTest.java index eba492a821..2ae915835a 100644 --- a/cluster-sharding-typed/src/test/java/jdocs/org/apache/pekko/cluster/sharding/typed/ExternalShardAllocationCompileOnlyTest.java +++ b/cluster-sharding-typed/src/test/java/jdocs/org/apache/pekko/cluster/sharding/typed/ExternalShardAllocationCompileOnlyTest.java @@ -18,12 +18,14 @@ import org.apache.pekko.actor.Address; import org.apache.pekko.actor.typed.ActorRef; import org.apache.pekko.actor.typed.ActorSystem; import org.apache.pekko.cluster.sharding.external.ExternalShardAllocation; +import org.apache.pekko.cluster.sharding.external.ExternalShardAllocationStrategy; import org.apache.pekko.cluster.sharding.external.javadsl.ExternalShardAllocationClient; import org.apache.pekko.cluster.sharding.typed.ShardingEnvelope; import org.apache.pekko.cluster.sharding.typed.javadsl.ClusterSharding; import org.apache.pekko.cluster.sharding.typed.javadsl.Entity; import org.apache.pekko.cluster.sharding.typed.javadsl.EntityTypeKey; +import java.time.Duration; import java.util.concurrent.CompletionStage; import static jdocs.org.apache.pekko.cluster.sharding.typed.ShardingCompileOnlyTest.Counter; @@ -39,7 +41,11 @@ public class ExternalShardAllocationCompileOnlyTest { EntityTypeKey typeKey = EntityTypeKey.create(Counter.Command.class, "Counter"); ActorRef> shardRegion = - sharding.init(Entity.of(typeKey, ctx -> Counter.create(ctx.getEntityId()))); + sharding.init( + Entity.of(typeKey, ctx -> Counter.create(ctx.getEntityId())) + .withAllocationStrategy( + ExternalShardAllocationStrategy.create( + system, typeKey.name(), Duration.ofSeconds(5)))); // #entity // #client diff --git a/cluster-sharding-typed/src/test/scala/docs/org/apache/pekko/cluster/sharding/typed/ExternalShardAllocationCompileOnlySpec.scala b/cluster-sharding-typed/src/test/scala/docs/org/apache/pekko/cluster/sharding/typed/ExternalShardAllocationCompileOnlySpec.scala index d858752310..36d109a657 100644 --- a/cluster-sharding-typed/src/test/scala/docs/org/apache/pekko/cluster/sharding/typed/ExternalShardAllocationCompileOnlySpec.scala +++ b/cluster-sharding-typed/src/test/scala/docs/org/apache/pekko/cluster/sharding/typed/ExternalShardAllocationCompileOnlySpec.scala @@ -38,7 +38,7 @@ class ExternalShardAllocationCompileOnlySpec { val TypeKey = EntityTypeKey[Counter.Command]("Counter") val entity = Entity(TypeKey)(createBehavior = entityContext => Counter(entityContext.entityId)) - .withAllocationStrategy(new ExternalShardAllocationStrategy(system, TypeKey.name)) + .withAllocationStrategy(ExternalShardAllocationStrategy(system, TypeKey.name)) // #entity val shardRegion: ActorRef[ShardingEnvelope[Counter.Command]] = diff --git a/cluster-sharding/src/main/scala/org/apache/pekko/cluster/sharding/external/ExternalShardAllocationStrategy.scala b/cluster-sharding/src/main/scala/org/apache/pekko/cluster/sharding/external/ExternalShardAllocationStrategy.scala index 906f1f0c5c..a4a6cfa5ec 100644 --- a/cluster-sharding/src/main/scala/org/apache/pekko/cluster/sharding/external/ExternalShardAllocationStrategy.scala +++ b/cluster-sharding/src/main/scala/org/apache/pekko/cluster/sharding/external/ExternalShardAllocationStrategy.scala @@ -39,11 +39,35 @@ import pekko.cluster.sharding.ShardRegion.ShardId import pekko.event.Logging import pekko.pattern.AskTimeoutException import pekko.util.Timeout +import pekko.util.JavaDurationConverters._ object ExternalShardAllocationStrategy { type ShardRegion = ActorRef + /** + * Create an [[ExternalShardAllocationStrategy]] + */ + def apply(systemProvider: ClassicActorSystemProvider, typeName: String): ExternalShardAllocationStrategy = { + implicit val timeout: Timeout = 5.seconds + new ExternalShardAllocationStrategy(systemProvider, typeName) + } + + /** + * Scala API: Create an [[ExternalShardAllocationStrategy]] + */ + def apply(systemProvider: ClassicActorSystemProvider, typeName: String, timeout: FiniteDuration) + : ExternalShardAllocationStrategy = { + new ExternalShardAllocationStrategy(systemProvider, typeName)(timeout) + } + + /** + * Java API: Create an [[ExternalShardAllocationStrategy]] + */ + def create(systemProvider: ClassicActorSystemProvider, typeName: String, timeout: java.time.Duration) + : ExternalShardAllocationStrategy = + this.apply(systemProvider, typeName, timeout.asScala) + // local only messages private[pekko] final case class GetShardLocation(shard: ShardId) private[pekko] case object GetShardLocations diff --git a/cluster-sharding/src/multi-jvm/scala/org/apache/pekko/cluster/sharding/ExternalShardAllocationSpec.scala b/cluster-sharding/src/multi-jvm/scala/org/apache/pekko/cluster/sharding/ExternalShardAllocationSpec.scala index 969c52c97a..571edb5f36 100644 --- a/cluster-sharding/src/multi-jvm/scala/org/apache/pekko/cluster/sharding/ExternalShardAllocationSpec.scala +++ b/cluster-sharding/src/multi-jvm/scala/org/apache/pekko/cluster/sharding/ExternalShardAllocationSpec.scala @@ -105,7 +105,7 @@ abstract class ExternalShardAllocationSpec entityProps = Props[GiveMeYourHome](), extractEntityId = extractEntityId, extractShardId = extractShardId, - allocationStrategy = new ExternalShardAllocationStrategy(system, typeName)) + allocationStrategy = ExternalShardAllocationStrategy(system, typeName)) "start cluster sharding" in { shardRegion