📜 ⬆️ ⬇️

Lesson 1.1 - Actors and ActorSystem

image

And here we are! Welcome to the first lesson.

In this tutorial, you will create your first actors and you will be introduced to the basics of Akka.NET .
')

Basic concepts / background




In this first lesson, you will learn the basics of creating a console application with your first system of actors and simple actors inside. We will create two actors, one for reading from the console, and one for writing after some simple processing.

What is an actor?


"Actor" can actually be presented as an analogue of a person participating in the system. This is an object that can do something and communicate.

We will assume that you are already familiar with object-oriented programming (OOP). The model of actors is very similar to object-oriented programming (OOP), in the model of actors all of these are actors .


Repeat this constantly for yourself: all are actors (everything is an actor) . All this actors. All this actors! When developing a system, present it as a hierarchy of people where tasks are divided and delegated between them until the task is so small that one actor can complete it.

At the moment, we propose to think like this: in OOP, you are trying to give each object one, well-defined goal, right? Well, the model of actors is no different, only now the objects to which you give a specific goal are called actors.

You can also read: What is an Akka.NET Actor ?

How do the actors communicate?


Actors communicate with each other in the same way people do it by exchanging messages. These messages are just old C # classes.

//this is a message! public class SomeMessage { public int SomeValue {get; set} } 


We'll talk more about the message in the next lesson, so don't worry about it now. All you need to know is that you send messages using the Tell () method to another actor.

 //send a string to an actor someActorRef.Tell("this is a message too!"); 


What can actors do?


Everything you can write. Realistically.

You program actors to handle the messages they receive and they can do everything they need with them to work. Access the database, write to a file, change internal variables or something else you need.

But in addition to processing the messages that it receives, actors can also do other actions:



Actors are inherently asynchronous (more about this in future lessons), and the actor model says nothing about which actor should do or in what order. It is up to you.

What types of actors exist?


All actors are inherited from UntypedActor , but don't worry about it now. We will cover different types of actors later.

In the first part, all your actors will be inherited from UntypedActor .

How can you create an actor?


There are two main things you need to know when creating an actor:



We will get to know Props more deeply in the third lesson, so don't worry about it now. We have provided Props for you in source code, so you just have to figure out how to use Props to create an actor.

What is ActorSystem?


ActorSystem is a reference to the underlying system and the Akka.NET framework. All actors live in the context of this system of actors. You will need to create your first actors from the context of this ActorSystem.

By the way, ActorSystem is a very heavy object: create it only one per application.

Iiiiii ... let's go! You have enough conceptual material now to dive in and make your first actors.

Exercise




Let's dive.

Note: In the presented code there are sections marked “YOU NEED TO FILL IN HERE” - find these regions of the code and start filling them with the appropriate functionality to achieve the goal.


Running an empty project


Go to the DoThis folder and open WinTail in Visual Studio. Solution consists of a simple console application.

You will use this solution throughout the first part.

Install the latest Akka.NET package from NuGet


In the NuGet package manager console, type the following command:

 Install-Package Akka 


This will install the latest Akka.NET libraries that you will need to compile this example.
Then you must add using namespace to the very top of the Program.cs file.

 // in Program.cs using Akka.Actor; 


Create your first ActorSystem


Go to Program.cs and add the following line:

 MyActorSystem = ActorSystem.Create("MyActorSystem"); 


NOTE : When you create a Props , ActorSystem or ActorRef you will very rarely see the new keyword. These objects must be created through the method factory built into Akka.NET. If you use the new keyword, you may be making an error.


Create ConsoleReaderActor and ConsoleWriterActor


The classes of actors are already there, but you have to create the objects themselves.

Again, in Program.cs, add just below where you created your ActorSystem :

 var consoleWriterActor = MyActorSystem.ActorOf(Props.Create(() => new ConsoleWriterActor())); var consoleReaderActor = MyActorSystem.ActorOf(Props.Create(() => new ConsoleReaderActor(consoleWriterActor))); 


Let ConsoleReaderActor send a message to ConsoleWriterActor actor


It's time to get your actors to work.

To do this, you need to do:

1. Let the ConsoleReaderActor send a message to the ConsoleWriterActor actor containing the content we just read from the console.
 // in ConsoleReaderActor.cs _consoleWriterActor.Tell(read); 


2. Let the ConsoleReaderActor send the message to itself after sending the message to the ConsoleWriterActor. This will create a loop reading console commands.

 // in ConsoleReaderActor.cs Self.Tell("continue"); 


3. Send the initial message to ConsoleReaderActor to start reading from the console.
 // in Program.cs consoleReaderActor.Tell("start"); 


Step 5: Build and Run!


When you finish your changes, press F5 to compile and run the sample in Visual Studio.
You will need to see something like this if everything is done correctly:



When you finish


Compare your code with the code in the Completed folder to see the instructions included in this example.

Source: https://habr.com/ru/post/253575/


All Articles