首页 > 解决方案 > 从无类型的演员系统创建有类型的分片区域

问题描述

我有带有无类型actor系统的akka​​集群。现在我必须将一个节点更改为使用类型化的 Actor 问题是:如何从非类型化的 Actor 系统创建类型化的 shardregion?基本上我需要这样的东西:

 import akka.cluster.sharding.typed.scaladsl.ClusterSharding
    ClusterSharding(untypedActorSyste).sharding.spawn(
      behavior = entityId ⇒ counter(entityId, 0),
      props = Props.empty,
      typeKey = TypeKey,
      settings = ClusterShardingSettings(system),
      maxNumberOfShards = 10,
      handOffStopMessage = GoodByeCounter)

我发现的所有示例都显示了如何基于 typedActorSystem 提供类型化分片...

标签: akkaakka-clusterakka-typed

解决方案


您可以使用适配器导入将 typedActorSystem 转换为 untypedActorSystem。

// adds support for typed actors to an untyped actor system and context
import akka.actor.typed.scaladsl.adapter._

toUntyped为类型化系统添加了隐式扩展方法。

val typedSystem           : ActorSystem[Any]  = ActorSystem(Behavior.empty[Any], "typed-system")
implicit val untypedSystem: actor.ActorSystem = typedSystem.toUntyped

查看Coexisting文档以获取更多详细信息,另一种方法是将Untyped 转换为 typed系统。为您的情况选择更好的。


推荐阅读