1
我有一个Akka Actor,负责管理一些消息,如Find
,Query
,它将一条新消息转发给其他类型的演员。这些演员将以新类型的消息作出回应。在Akka Actor中撰写多个接收函数
class Actorbase extends Actor {
override def receive = {
/* The two message below are related to each other */
case Find(coll, id) => // Do something
case QueryAck(key, value, u) => // Do something
/* The three message below are related to each other */
case Upsert(collection, id, value) => // Do something
case Response.UpsertNAck(key, msg, u) => // Do something
case Response.UpsertAck(key, u) => // Do something
/* And so on... */
}
}
在上述例子中,是QueryAck
Find
类型的消息的前向的响应消息。 UpsertNAck
和UpsertAck
是对Upsert
消息的可能响应。
为了使代码更具可读性和可维护性,我希望将两组case
分为两个专用方法,即manageQueries
和manageUpserts
,获得类似于以下代码的内容。
class Actorbase extends Actor {
override def receive = {
manageQueries.andThen(manageUpserts)
}
}
可能吗?我是否需要声明多个方法返回Receive
对象,然后以某种方式组合它们?
非常感谢。
感谢您的答复。你认为这是我在问题中描述的方法还是在[this]中暴露的方法更好(https://stackoverflow.com/questions/8681444/composing-trait-behavior-in-scala-in-an-akka- receive-method?rq = 1)SO答案? –
这比我认为的另一个更好(作为项目的维护者) –