Props是配置Actor和实例化Actor,那实例化后,就应该访问了,Props.Actor提供了Actor.Spawn(),Actor.SpawnPrefix(),Actor.SpawnNamed()三个方法,来获取Actor实例,需要注意的是,这些方法返回的并不是真正的Actor对象,而是一个ProgressID,一个代表Actor对象的进程ID,缩写PID。
上代码:
using Proto;
using System;
using System.Threading;
using System.Threading.Tasks; namespace P003_SpawningActors
{
class Program
{
static void Main(string[] args)
{
var props = Actor.FromProducer(() => new MyActor()); //产生一个自定义名称的PID
var pid1 = Actor.Spawn(props);
pid1.Tell(new MyEntity { ID = });
Thread.Sleep();
Console.WriteLine("------------------------------------------");
//产生一个有gsw前缀,跟自动生成的名称的PID
var pid2 = Actor.SpawnPrefix(props, "gsw");
pid2.Tell(new MyEntity { ID = });
Thread.Sleep();
Console.WriteLine("------------------------------------------");
//产生一个名称为gswpid的PID
var pid3 = Actor.SpawnNamed(props, "gswpid");
pid3.Tell(new MyEntity { ID = });
Console.ReadLine();
}
} public class MyActor : IActor
{
public Task ReceiveAsync(IContext context)
{
if (context.Message is MyEntity myEntity)
{
Console.WriteLine($"父 SelfID={context.Self.Id} myEntity.ID={myEntity.ID}"); var cldProps = Actor.FromProducer(() => new MyChildActor());
//第一个子Actor
var pidCld1 = context.Spawn(cldProps);
pidCld1.Tell(new MyChildEntity { Message = "1 message,myEntity.ID=" + myEntity.ID });
//第二个子Actor
var pidCld2 = context.SpawnPrefix(cldProps, "gswCld");
pidCld2.Tell(new MyChildEntity { Message = "2 message,myEntity.ID=" + myEntity.ID });
//第三个子Actor
var pidCld3 = context.SpawnNamed(cldProps, "gswCldPid");
pidCld3.Tell(new MyChildEntity { ID = , Message = "3 message,myEntity.ID=" + myEntity.ID });
}
return Actor.Done;
}
}
public class MyChildActor : IActor
{
public Task ReceiveAsync(IContext context)
{
if (context.Message is MyChildEntity myChildEntity)
{
Console.WriteLine($"子 SelfID={context.Self.Id} Message={myChildEntity.Message}");
}
return Actor.Done;
}
}
public class MyEntity
{
public int ID { get; set; }
}
public class MyChildEntity
{
public string Message { get; set; }
public int ID { get; set; }
}
}
这个例子很简单,说明了三个Spawn的使用方式和Self.Id的特征,包括产生子Actor后,子Actor的Self.Id会带有父ID,结果如下: