《經過C#學Proto.Actor模型》之Spawning

Props是配置Actor和實例化Actor,那實例化後,就應該訪問了,Props.Actor提供了Actor.Spawn(),Actor.SpawnPrefix(),Actor.SpawnNamed()三個方法,來獲取Actor實例,須要注意的是,這些方法返回的並非真正的Actor對象,而是一個ProgressID,一個表明Actor對象的進程ID,縮寫PID。spa

 上代碼:3d

 1 using Proto;
 2 using System;
 3 using System.Threading;
 4 using System.Threading.Tasks;
 5 
 6 namespace P003_SpawningActors
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             var props = Actor.FromProducer(() => new MyActor());
13 
14             //產生一個自定義名稱的PID
15             var pid1 = Actor.Spawn(props);
16             pid1.Tell(new MyEntity { ID = 1 });
17             Thread.Sleep(1000);
18             Console.WriteLine("------------------------------------------");
19             //產生一個有gsw前綴,跟自動生成的名稱的PID
20             var pid2 = Actor.SpawnPrefix(props, "gsw");
21             pid2.Tell(new MyEntity { ID = 2 });
22             Thread.Sleep(1000);
23             Console.WriteLine("------------------------------------------");
24             //產生一個名稱爲gswpid的PID
25             var pid3 = Actor.SpawnNamed(props, "gswpid");
26             pid3.Tell(new MyEntity { ID = 3 });
27             Console.ReadLine();
28         }
29     }
30 
31     public class MyActor : IActor
32     {
33         public Task ReceiveAsync(IContext context)
34         {
35             if (context.Message is MyEntity myEntity)
36             {
37                 Console.WriteLine($"父 SelfID={context.Self.Id}   myEntity.ID={myEntity.ID}");           
38 
39                 var cldProps = Actor.FromProducer(() => new MyChildActor());
40                 //第一個子Actor
41                 var pidCld1 = context.Spawn(cldProps);
42                 pidCld1.Tell(new MyChildEntity { Message = "1 message,myEntity.ID=" + myEntity.ID });
43                 //第二個子Actor
44                 var pidCld2 = context.SpawnPrefix(cldProps, "gswCld");
45                 pidCld2.Tell(new MyChildEntity { Message = "2 message,myEntity.ID=" + myEntity.ID });
46                 //第三個子Actor
47                 var pidCld3 = context.SpawnNamed(cldProps, "gswCldPid");
48                 pidCld3.Tell(new MyChildEntity { ID = 3, Message = "3 message,myEntity.ID=" + myEntity.ID });
49             }
50             return Actor.Done;
51         }
52     }
53     public class MyChildActor : IActor
54     {
55         public Task ReceiveAsync(IContext context)
56         {
57             if (context.Message is MyChildEntity myChildEntity)
58             {              
59                 Console.WriteLine($"子    SelfID={context.Self.Id}     Message={myChildEntity.Message}");          
60             }
61             return Actor.Done;
62         }
63     }
64     public class MyEntity
65     {
66         public int ID { get; set; }
67     }
68     public class MyChildEntity
69     {
70         public string Message { get; set; }
71         public int ID { get; set; }
72     }
73 }

這個例子很簡單,說明了三個Spawn的使用方式和Self.Id的特徵,包括產生子Actor後,子Actor的Self.Id會帶有父ID,結果以下:code

相關文章
相關標籤/搜索