问题描述
我知道不建议您阻塞演员的 receive
方法,但是我相信可以做到(只要也不要这样做)
I know that it is not recommended to block in the receive
method of an actor, but I believe it can be done (as long as it is not done in too many actors at once).
建议阻止 preStart
作为解决问题的一种方法,因此大概阻止 preStart
是安全的。
This post suggests blocking in preStart
as one way to solve a problem, so presumably blocking in preStart
is safe.
但是,我尝试阻止 preRestart
(不是 preStart
),一切似乎都死机了-不再记录接收到的消息。
However, I tried to block in preRestart
(not preStart
) and everything seemed to just hang - no more messages were logged as received.
此外,在无法安全阻止的情况下,是一个安全的选择吗?
Also, in cases where it is not safe to block, what is a safe alternative?
推荐答案
从不阻止演员。
如果您的actor是actor层次结构的一部分(应该如此),那么actor系统将无法停止它。
演员的生命周期(监督,监视等)是通过消息传递完成的。
停止阻止儿童的父演员是行不通的。
If your actor is part of an actor hierarchy (and it should be), the actor system is not able to stop it.The actor's life-cycle (supervision, watchig etc.) is done by messaging.Stopping a parent actor of a blocking child will not work.
也许有多种方法可以将阻止条件与演员的生命周期结合起来。
但这会导致复杂性和风格失调。
Maybe there are ways to couple the blocking condition with the actor's lifecycle.But this would lead to overload of complications and bad-style.
因此,最好的方法是在该演员外部进行遮挡。
例如您可以通过执行程序服务在单独的线程中运行阻止代码。
So, the best way is to do the blocking part outside of that actor.E.g. you could run the blocking code via an executor service in a separate thread.
这篇关于什么时候可以安全地阻止Akka 2演员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!