0

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)

AndCode
  • 384
  • 1
  • 10

1 Answers1

2

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")
ignasi35
  • 885
  • 6
  • 14
  • Thank you for the helpful answer, I thought that I would have had to resort to using a message broker for such data interchange ... Direct communications between entities (of different types) in the shared cluster would help in my use case. – AndCode Jul 20 '20 at 10:35
  • I see that you are a new contributor. Could you kindly accept ignasi's answer? – Levi Ramsey Jul 25 '20 at 16:28