Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

akka.net is there a a way to get or create actor

Tags:

c#

akka.net

For my actor hierarchy, I do not know all the actors I need until I process the data through a few actors, so I'm looking for a way to either return an existing ActorRef or create a new action. This is what I would like the code below to either create an actor if one does not exist at "my-id-1" or return the one that already exists.

Context.ActorOf(MyActor.Props(message), "my-id-1");

The above code will (as documented) throw a InvalidActorNameException if the actor already exists. How can I accomplish this in Akka.net?

like image 891
Zeus82 Avatar asked Oct 06 '16 20:10

Zeus82


People also ask

How do you get an ActorRef?

To acquire an ActorRef that is bound to the life-cycle of a specific actor you need to send a message, such as the built-in Identify message, to the actor and use the sender() reference of a reply from the actor.

What is actor Akka net?

An actor is a container for State, Behavior, a Mailbox, Children and a Supervisor Strategy.

Is Akka net good?

Akka.Net is an excellent library to work with Actor Model, with it you will have amazing results. It is easy to use, flexible and has a great documentation and reference guide.

How do I create an actorsystem in Akka?

Creating an ActorSystem and actors in Akka .NET is one of the most basic necessities, and it is something you’ll be doing all the time. Once you have an ActorSystem, you can create actors using an IActorRefFactory implementation (i.e. the ActorSystem itself, or an actor’s Context). This requires you to use Props.

What is Akka net?

Akka.Net is an open source, distributed computing framework built by Petabridge. Akka.Net allows you to create scalable, resilient, concurrent, event-driven applications using the actor model.

What are Akka actors and messages?

When working in Akka.Net, you use actors and messages to model your problem. In Akka.Net, an actor is an object with some specific behavior. While actors do have internal state, they don’t have any shared mutable state. You can have many concurrent actors in your application with each of them processing operations independently on their own.

What is Akka object-oriented programming?

The object-oriented programming approach uses classes and objects to model the problem domain. When working in Akka.Net, you use actors and messages to model your problem. In Akka.Net, an actor is an object with some specific behavior. While actors do have internal state, they don’t have any shared mutable state.


1 Answers

You can check if current actor has a child with provided name by using Context.Child(actorName) method. It will return actor ref of the target actor if it exists or ActorRefs.Nobody if there is no such actor.

Code in your case could look like:

var child = Context.Child(actorName);
if (Equals(child, ActorRefs.Nobody))
    child = Context.ActorOf(MyActor.Props(message), actorName);
like image 144
Bartosz Sypytkowski Avatar answered Sep 21 '22 06:09

Bartosz Sypytkowski