在Akka演员中堆叠多个特征

编程入门 行业动态 更新时间:2024-10-22 10:40:31
本文介绍了在Akka演员中堆叠多个特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在创建扩展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-我见过具有反应的演员,例如: www.kotancode/2011/07/19/traits-multiple-inheritance-and-actors-在scala / 中,但这不是我所需要的

PS - I've seen the actors with react example: www.kotancode/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:

使用PartialFunction链扩展Actor

更多推荐

在Akka演员中堆叠多个特征

本文发布于:2023-11-25 02:57:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628036.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   特征   演员   Akka

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!