在Akka演员中堆叠多个特征 [英] stacking multiple traits in akka Actors
问题描述
我正在创建扩展Actor的多个特征。然后,我想创建一个使用其中一些特征的actor类。但是,我不确定如何在Actor类的receive方法中结合所有特征的接收方法。
I'm creating multiple traits which extend Actor. Then I want to create an actor class which uses some of these traits. However, I'm not sure how to combine the receive methods from all traits in the receive method of the Actor class.
特质:
trait ServerLocatorTrait extends Actor {
def receive() = {
case "s" => println("I'm server ")
}
}
trait ServiceRegistrationTrait extends Actor {
def receive() = {
case "r" => println("I'm registration ")
}
}
演员:
class FinalActor extends Actor with ServiceRegistrationTrait with ServerLocatorTrait {
override def receive = {
super.receive orElse ??? <--- what to put here
}
}
现在,如果我将 r
和 s
发送到 FinalActor
它仅在 ServerLocatorTrait
中使用-这是最后添加的特征。
因此,现在的工作方式是,它认为超级是最后添加的特征,因此在这种情况下, ServerLocatorTrait
Now if I send "r"
and "s"
to FinalActor
it goes only in ServerLocatorTrait
- which is the last trait added.
So the way this works right now is that it considers super the last trait added, so in this case ServerLocatorTrait
问题:
如何结合 FinalActor
中所有特征的接收方法?
Question:
How do I combine the receive methods from all the traits in FinalActor
?
PS-我见过具有反应
的演员,例如: http://www.kotancode.com/2011/07/19/traits-multiple-inheritance-and-actors-在scala /
中,但这不是我所需要的
PS - I've seen the actors with react
example: http://www.kotancode.com/2011/07/19/traits-multiple-inheritance-and-actors-in-scala/
but it's not what I need
推荐答案
我不确定您是否可以合并接收方法,因为这将涉及调用上级的上级以获得 ServiceRegistration
的 receive
方法。
I'm not sure if you can combine the receive methods, since that would involve calling the super's super to obtain the ServiceRegistration
's receive
method. It would also be very confusing.
另一种方法是在特征中的 receive
方法中使用不同的名称
Another way would be to give different names to the receive
method in the traits.
trait ServerLocatorTrait extends Actor {
def handleLocation: Receive = {
case "s" => println("I'm server ")
}
}
trait ServiceRegistrationTrait extends Actor {
def handleRegistration: Receive = {
case "r" => println("I'm registration ")
}
}
class FinalActor extends Actor with ServiceRegistrationTrait with ServerLocatorTrait {
def receive = handleLocation orElse handleRegistration
}
object Main extends App {
val sys = ActorSystem()
val actor = sys.actorOf(Props(new FinalActor))
actor ! "s"
actor ! "r"
sys.shutdown()
}
您仍然可以使用初始方法,但是必须为每个混合特征链接 super.receive
。
You can still use you initial approach, but you must chain the super.receive
for each mixed trait.
trait IgnoreAll extends Actor {
def receive: Receive = Map()
}
trait ServerLocatorTrait extends Actor {
abstract override def receive = ({
case "s" => println("I'm server ")
}: Receive) orElse super.receive
}
trait ServiceRegistrationTrait extends Actor {
abstract override def receive = ({
case "r" => println("I'm registration ")
}: Receive) orElse super.receive
}
class FinalActor extends IgnoreAll with ServiceRegistrationTrait with ServerLocatorTrait
后一种解决方案对我来说看起来很丑。
The latter solution looks pretty ugly to me.
请参见下面的链接,对该主题进行更详细的讨论:
Please see the below link for a more detailed discussion on the subject:
这篇关于在Akka演员中堆叠多个特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!