简体   繁体   English

如何在Akka.net中杀死一名演员

[英]How to kill just one Actor in Akka.net

I have an Actor and a Instantiate then more then once. 我有一个演员和一个实例化对象,然后再有一次。 When a try to kill a specified ActorRef, all my actors is killed. 尝试杀死指定的ActorRef时,我的所有演员都被杀死了。

Example: This is my actor: 示例:这是我的演员:

public class MyActor: ReceiveActor {
    public MyActor() {
         Receive<long>(id => Handler(id));
    }
    public void Handler(long id) {
        Console.WriteLine(id)
    }
}

And I can 'n' MyActor running, each binded with an Id . 而且我无法运行MyActor,每个MyIdor都绑定有一个Id But, when I try to kill a specified actor, it's killing all. 但是,当我尝试杀死指定的演员时,就是在杀死所有人。

Example: 例:

  • I have MyActor running. 我正在运行MyActor。
  • Binded with ids: [1, 2, 3 , 4 ,5]. 与ID绑定:[1、2、3、4、5]。
  • And I have to kill the Actor with id 3. 而且我必须杀死ID为3的演员。

So, I'm try to use this code in MyActor: 因此,我尝试在MyActor中使用以下代码:

public void Handler(long id) {
    Console.WriteLine(id)
    if (id == 3)
        Self.Tell(Kill.Instance);
}

But, it's killing all MyActor. 但是,这杀死了所有MyActor。 What's wrong? 怎么了?

-- EDIT -编辑

public IActorRef ScheduleTellRepeatedly<T>(TimeSpan initialDelay, TimeSpan interval, object message) { 
    var actorRef = ActorSystem.ActorOf(ActorSystem.DI().Props(typeof(T))); 
    ActorSystem.ScheduleTellRepeatedly(initialDelay, interval, actorRef, message, ActorRefs.NoSender)
    return actorRef;
}

var actor1 = ScheduleTellRepeatedly<MyActor>(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30), 1);
var actor2 = ScheduleTellRepeatedly<MyActor>(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30), 2);
var actor3 = ScheduleTellRepeatedly<MyActor>(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30), 3);
var actor4 = ScheduleTellRepeatedly<MyActor>(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30), 4);
var actor5 = ScheduleTellRepeatedly<MyActor>(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30), 5);


actor3.Tell(Kill.Instance);

Without seeing your code where you instantiate your actors, it is difficult to tell exactly what is happening. 如果没有在实例化角色的地方看到代码,就很难准确地知道正在发生什么。 But with your command handler set up the way it is, I'm guessing it is because you only have one actor spun up. 但是,按照您的命令处理程序的方式进行设置,我想这是因为您只有一个参与者参与其中。 You can spin up multiple instances of the same actor as long as they all have a unique name/reference. 您可以启动同一角色的多个实例,只要它们都有唯一的名称/引用即可。 The following code will kill one of the actors, but keep the rest of them alive. 以下代码将杀死其中一个参与者,但让其他参与者活着。

// Spin up five actors, each with a unique name/reference
// Fill in Props as needed
var actor1 = Context.ActorOf(MyActor.Props(), "MyActor_1");
var actor2 = Context.ActorOf(MyActor.Props(), "MyActor_2");
var actor3 = Context.ActorOf(MyActor.Props(), "MyActor_3");
var actor4 = Context.ActorOf(MyActor.Props(), "MyActor_4");
var actor5 = Context.ActorOf(MyActor.Props(), "MyActor_5");

// Kill the third actor by sending it a poison pill
actor3.Tell(PoisonPill.Instance);

// Send a message to any of the other actors
actor4.Tell((long)123); // Will write 123 to the console

NOTE: In the code above, the second (optional) parameter of the ActorOf method is simply a user-friendly name to give to the actors you create. 注意:在上面的代码中, ActorOf方法的第二个(可选)参数只是给您创建的actor的用户友好名称。 If you choose to not specify an actor's name, Akka will automatically generate a unique name for the actor. 如果您选择不指定演员名称,则Akka会自动为该演员生成一个唯一的名称。

Update 更新资料

Your latest code is set up correctly to kill just the actor you specify ( actor3 ). 您最新的代码已正确设置为仅杀死您指定的actor( actor3 )。 I copied/pasted your code, ran it, and verified the other four actors are still running. 我复制/粘贴了您的代码,运行了该代码,并验证了其他四个参与者仍在运行。 I did notice your MyActor class is listening for a long , but you are passing it an int . 我确实注意到您的MyActor类正在听很long ,但是您正在将它传递给int This may make it appear as though the other actors are shut down when they really aren't. 这可能会使其他参与者实际上没有被关闭时,好像它们被关闭了一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM