Akka.net – Part 2 (Create Actor)

Facebook
Twitter
LinkedIn

Never miss a post!

Sign up for our newsletter and get FREE Development Trends delivered directly to your inbox.

You can unsubscribe any time. Terms & Conditions.
Categories

In the first post of this series we covered the principles of akka.net. Now its time, to look at the code involved in creating your first actor and sending messages to it.

Our journey starts by installation Akka.Net from NuGet.

Install-Package Akka

Next thing we need to accomplish is the definition the actor. In Akka.Net all actors derive from the UntypedActor class, which extends the ActorBase class.

We will build an simple alert system that will process alerts generated by our environment. (i.e. a HelloWorld sample).

[code language=”csharp”]
public class AlertHandler : UntypedActor
{
protected override void PreStart()
    {
    //You can write any initialisation code here
Console.WriteLine("PreRestart");        
}
    protected override void PreRestart(Exception reason, object message)
    {
Console.WriteLine("PreRestart");
    }
   protected override void PostStop()
    {
Console.WriteLine("PostStop");
  }
    protected override void PostRestart(Exception reason)
    {               
Console.WriteLine("PostRestart");
    }
protected override void OnReceive(object message)
    {
         if (message is string)
         {
          var msg = message as string;
             Console.WriteLine(msg);
         }
    }
}
[/code]

The Actor has events that you can wire to perform initialisation (PreStart/PreReStart) and cleanup (PostRestart/PostStop). These were covered in the first post. The main entry point for our messages is OnReceive. This function will receive messages from the Actor mailbox. Its were all the magic happens.

Any POCO can be a message. A message can be a string, a value like int, a type or  an object.

The recommended approach is to make your own custom messages into semantically named classes, and to encapsulate any state you want inside those classes.

Actions to be typically implemented in the OnReceive may include , processing the message, send the reply to the parent or forward a message to another actor. Also important to note that exceptions in the OnReceive will result in a notification to the parent. This will be covered in a future post.

Now we need to create an instance of our Actor and send messages to it.

We will now introduce the following principles ActorSystem ,  Props   and IActorRef .

TheActorSystem is a reference to the Akka.NET framework. All actors live within the context of this actor system. You’ll need to create your first actors from the context of this ActorSystem.

[code language=”csharp”]

var eventActorSystem = ActorSystem.Create("EventActorSystem");

[/code]

Props objects are shareable recipes for creating an instance of an actor. Props get passed to the ActorSystem to generate an actor for your use.

The following is the suggested syntax to create an instance of Props

[code language=”csharp”]

Props props = Props.Create(() => new AlertHandler());

[/code]

Now we create an IActorRef.

The IActorRef is a reference or handle to an actor and we will use it to send messages to the
Actor through the ActorSystem.

There is one important rule everything is managed by the ActorSystem you should never create actors or props with the new operator or send messages directly to Actors. Everything has to flow through the ActorSystem. (If not puppies die).

Its sounds an overkill but don’t forget that an actor might not reside on the same box !!!.

The following is the process of creating an IActorRef.

[code language=”csharp”]

var eventHandlerRef =  eventActorSystem.ActorOf(props);

[/code]

 

Last peace of the puzzle we need to send a message to the actor through the ActorSystem. This is achieved by using the function call .Tell().

 

[code langage=”csharp”]

eventHandlerRef.Tell("Event 1");

[/code]

 

[code language=”csharp”]
void Main(string[] args)
{
var actorSystem = ActorSystem.Create("EventSystem");
IActorRef actorRef = actorSystem.ActorOf(Props.Create()
= new EventHandler());
actorRef.Tell("Event 1");
}
[/code]

Wooohoooo. Great Success !!!

Facebook
Twitter
LinkedIn

Our website uses cookies that help it to function, allow us to analyze how you interact with it, and help us to improve its performance. By using our website you agree by our Terms and Conditions and Privacy Policy.