简体   繁体   中英

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.

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 . But, when I try to kill a specified actor, it's killing all.

Example:

  • I have MyActor running.
  • Binded with ids: [1, 2, 3 , 4 ,5].
  • And I have to kill the Actor with id 3.

So, I'm try to use this code in MyActor:

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

But, it's killing all 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. If you choose to not specify an actor's name, Akka will automatically generate a unique name for the actor.

Update

Your latest code is set up correctly to kill just the actor you specify ( 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 . This may make it appear as though the other actors are shut down when they really aren't.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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