As akka docs described, akka send messages fire-and-forget.
But in ask pattern, if a typekey not exists, I still need waiting for response util timeout.
Is there an approach to failure immediately on sending message to non-existing typekey?
CodePudding user response:
By non-existing type key, I presume you mean a typekey which hasn't been passed to init
.
If using typed sharding, you can ask the local ActorRef
returned from ClusterSharding(actorSystem).shardState
with the GetShardRegionState(typeKey, _)
query. The response CurrentShardRegionState
's shards
will be empty if the typekey hasn't been initialized (note that since this relies on classic cluster sharding under the hood, this will technically only verify that there's a typekey with that name initialized) on this node.
In classic sharding, the set of typekey names which have been initialized on the node can be obtained synchronously via ClusterSharding(actorSystem).shardTypeNames
.
Note that it is generally recommended to initialize sharding for all type keys very early in the startup process and to sequence the startup such that functionality which depends on sharding is not started until all the type keys have been initialized. For example, one can trigger sharding initialization on cluster formation and then (in the same callback) start serving HTTP requests or consuming from a message broker. If one is running in Kubernetes, one can bind HTTP immediately, but define a readiness check which doesn't turn green until the sharding typekeys have been initialized: k8s will then not send HTTP traffic to the instance until sharding is ready.