当前位置: 移动技术网 > IT编程>开发语言>.net > 《通过C#学Proto.Actor模型》之Mailbox

《通过C#学Proto.Actor模型》之Mailbox

2018年08月30日  | 移动技术网IT编程  | 我要评论

詹天佑资料,ayomi博客,实验室用冰箱

邮箱是actor模型的一个重要组成部分,负责接收发过来的消息,并保存起来,等待actor处理。邮箱中维护着两种队列,一种是存系统消息,另一个是存用户消息,系统省是指started,stoping,stoped之类的,用户当然指我们自定义的actor。

另外,我们可以通过实现imailboxstatistics接口,来获取邮箱的状态变更,并且可以有多个imailboxstatistics实现。

 

码友看代码:

 1 using proto;
 2 using proto.mailbox;
 3 using system;
 4 using system.threading.tasks;
 5 
 6 namespace p005_mailboxes
 7 {
 8     class program
 9     {
10         static void main(string[] args)
11         {
12             var props = new props()
13                 // 用道具代理返回一个iactor实例
14                 .withproducer(() => new myactor())
15                 //默认邮箱使用无界队列
16                 .withmailbox(() => unboundedmailbox.create(new mymailboxstatistics()))
17                 // 默认的 spawner 构造  actor, context 和 process
18                 .withspawner(props.defaultspawner);
19 
20             //从props衍生pid,pid代理一个actor的地址
21             var pid = actor.spawn(props);
22             //把hello对象交给helloactor处理
23             pid.tell(new myentity
24             {
25                 message = "this is message"
26             });
27             console.readline();
28         }
29     }
30     public class myactor : iactor
31     {
32         public task receiveasync(icontext context)
33         {
34             if (context.message is myentity myentity)
35             {
36                 console.writeline(myentity.message);
37             }
38             return actor.done;
39         }
40     }
41     public class myentity
42     {
43         public string message { get; set; }
44     }
45     public class mymailboxstatistics : imailboxstatistics
46     {
47         public void mailboxempty()
48         {            
49             console.writeline("邮箱mailboxempty");
50         }
51 
52         public void mailboxstarted()
53         {
54             console.writeline("邮箱mailboxstarted");
55         }
56 
57         public void messageposted(object message)
58         {
59             console.writeline("邮箱messageposted:"+message);
60         }
61 
62         public void messagereceived(object message)
63         {
64             console.writeline("邮箱messagereceived:"+message);
65         }
66     }
67 }

当消息posted时,started时,received时,邮箱为空时,这些方法会被先后调用,这里可对消息作处理。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网