This is a common scenario I run into, where I have two (or more) actors getting some data asynchronously, and then I need to do an operation when they're all finished.
What is the common pattern to do this?
Here's a simplified example.
public MasterActor : ReceiveActor
{
public MasterActor()
{
Initialize();
}
public void Initiaize()
{
Receive<DoSomeWork>(_ =>
{
var actor1 = Context.ActorOf(Props.Create(() => new Actor1());
var actor2 = Context.ActorOf(Props.Create(() => new Actor2());
// pretend these actors send responses to their senders
// for sake of example each of these methods take between 1 and 3 seconds
actor1.Tell(new GetActor1Data());
actor2.Tell(new GetActor2Data());
});
Receive<Actor1Response>(m =>
{
//actor 1 has finished it's work
});
Receive<Actor2Response>(m =>
{
//actor 2 has finished it's work
});
}
}
To kick this off I send the MasterActor
a DoSomeWork
message.
What is the common way to do an operation when I have both a Actor1Response
and an Actor2Response
.
I don't really want to have logic in each receive handler checking if the other one is finished or anything like that. I guess what i'm thinking of something similar to a Task.WaitAll()
method.
Am I just attacking the problem the wrong way? Do I need to re-write the actors in a different way?
Any common patterns or solutions would be awesome.