'How to create an actor in Akka cluster dynamically

How can i create an actor in akka cluster Sharding dynamically with Cluster Client which will pass message from out side the cluster with akka remoting.



Solution 1:[1]

So you don't provide a lot of details about what you using (Typed vs Untyped, JVM or .NET). But, regardless, the short answer is just "send a message to the ShardRegion". The whole gist of sharding is that the actor is dynamically created automatically and transparently whenever a message is received.

e.g. If you have a Customer actor, and you send a "get the account balance of account 1001" the ShardRegion will automatically extract the customer number from the message, figure out which shard that actor belongs in, and route the message to the node that owns that shard. The ShardRegion on that node will then automatically create the Actor (if it doesn't already exist) and then forward the message to the actor.

So, the even shorter answer is "it just happens automatically as needed". But it sounds like you might want to read through all of the sharding documentation just so you understand what is going on.

EDIT (Responding to a comments):

I was trying to reply to the following comment. But it was too long to respond in a comment so I'm adding this section.

Can we spawn actors by user defined actor when we pass some message.But can it be done distributed across other nodes?means if i create 10 actors in 5 nodes will it create 2 actor in each node? In sharding it can be done by sharding each actor

Part of the challenge of answering this question is that there is some ambiguity in what you mean by "create an actor".

Fundamentally the only direct way that an actor is created is an actor spawning a child. There is a root level actor associated with the actor system. That can spawn children. And then the children can spawn children, and so forth. All of those children and sub children are, by definition, on the same node. So in this "direct" interpretation, all actor creation is ALWAYS local. No matter what. Full stop.

Furthermore, you specifically mention cluster client. Which means that you are a client and not an actor. Which means, in the strictest sense of the term, you cannot create actors. At all. Remotely or locally, it doesn't matter, you can't create actors AT ALL if you are a cluster client. Only actors can create actors.

But, in practical terms, this isn't true. Because a very common case is that actors will allow you to spawn actors indirectly in response to a message. This is what cluster sharding does: it essentially runs a "proxy" on every node that will automatically spawn actors as needed. The proxies will even stop and restart an actor on a different node to "rebalance" as needed.

Which goes back to my original point, that there is nothing magic about how cluster sharding does this. You could run your own proxy on each node. Either directly. Or via a cluster based router. Or via an event bus. Or any of a bunch of other mechanisms.

So, getting back to your specific question:

if i create 10 actors in 5 nodes will it create 2 actor in each node?*

To a certain extent, the question doesn't really make sense. As noted above, as a cluster client, you can't actually create actors at all.

But once you start talking about indirect creation, it's basically whatever you want, it just depends on how you write it.

In Akka Sharding, which was your original question, the actors will be created on whatever node their hash dictates. Which hopefully is fairly uniform but might not be exactly even. But the sharding API doesn't have a "create actor" API, it just creates actors as needed.

On the other hand, if you using some sort of cluster aware router to spawn actors, the functionality can be whatever you desire.

But I feel like this question is getting too vague to answer meaningfully.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1