AndCode
AndCode

Reputation: 488

Akka Cluster Sharding - can different entities within the cluster communicate with each other?

All materials on Cluster Sharding with Akka imply sending messages from outside the cluster to entities in the cluster. However, can entities (actors) in different sharding regions/shards of the same cluster communicate between each other? Is there some sample code available for this? (on how we send a message from one entity to another within a cluster)

Upvotes: 0

Views: 267

Answers (1)

ignasi35
ignasi35

Reputation: 925

the short answer is "yes".

Let's elaborate:

You can view an EntiryRef is an ActorRef that's known to be sharded, so what you need, in any case, is a mechanism to obtain that entityRef. That mechanism is the ClusterSharding extension. So using:

val sharding = ClusterSharding(system)

you obtain the sharding extension which you can then use:

val counterOne: EntityRef[Counter.Command] = sharding.entityRefFor(TypeKey, "counter-1")

Upvotes: 2

Related Questions