I have a bunch of actors running which need to enqueue tasks to be processed sequentially, one at a time. I will need an actor to process the tasks on the queue. Is it ok to create one actor and pass a reference to that actor as a parameter to each job (an implicit queue)? Ex.
Actor:
class QActor extends Actor{
def receive = {
case input => sender ! doSomething(input)
}
}
Sender
val future = myQActor ? msg
Await.result(future)
使用另一个actor及其邮箱作为队列很好,只是不要阻止发件人。
Your approach looks fine but with one caveat. The ask pattern ( ?
) requires an implicit timeout, after which if no reply is received, the Future fails with a timeout error. If you have multiple actors all simultaneously asking your QActor
which processes each request sequentially, it may be difficult to know what is a reasonable timeout to set. It may be better to use myQActor ! msg
myQActor ! msg
and implement a handler in Receive
of your sender to handler the expected response.
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.