如何在Akka 2.3中使用自定义路由器? [英] How to use custom Router in Akka 2.3?
问题描述
更新:整个 akka项目的源代码现在在这里可用,存在此问题。归档为问题。
Update: The source code for the whole akka project is now available here, with this problem filed as an issue.
我无法从 Akka并发性一书(第10.6节)。这是有问题的代码段:
I am having trouble rewriting an example of a custom router from Akka Concurrency book (section 10.6). Here is the piece of code in question:
package zzz.akka.avionics
import akka.actor.{Props, SupervisorStrategy}
import akka.dispatch.Dispatchers
import akka.routing.{RouterConfig, RouteeProvider, Route, Destination}
class SectionSpecificAttendantRouter extends RouterConfig {
this: FlightAttendantProvider =>
// The RouterConfig requires us to fill out these two
// fields. We know what the supervisorStrategy is but we're
// only slightly aware of the Dispatcher, which will be
// discussed in detail later
def routerDispatcher: String = Dispatchers.DefaultDispatcherId
def supervisorStrategy: SupervisorStrategy =
SupervisorStrategy.defaultStrategy
// The createRoute method is what invokes the decision
// making code. We instantiate the Actors we need and then
// create the routing code
def createRoute(routeeProvider: RouteeProvider): Route = {
// Create 5 flight attendants
val attendants = (1 to 5) map { n =>
routeeProvider.context.actorOf(Props(newFlightAttendant), "Attendant-" + n)
}
// Register them with the provider - This is important.
// If you forget to do this, nobody's really going to
// tell you about it :)
routeeProvider.registerRoutees(attendants)
// Now the partial function that calculates the route.
// We are going to route based on the name of the
// incoming sender. Of course, you would cache this or
// do something slicker.
{
case (sender, message) =>
import Passenger.SeatAssignment
val SeatAssignment(_, row, _) = sender.path.name
List(Destination(sender,
attendants(math.floor(row.toInt / 11).toInt)))
}
}
}
我的问题:
- 我应该扩展
Pool
,RouterConfig
或CustomRouterConfig
? - 如何获取
发件人
参考以便从空姐的路径计算索引?
- Should I extend
Pool
,RouterConfig
orCustomRouterConfig
? - How can I get a
sender
reference in order to calculate the index from the flight attendant's path?
这是我破碎的起点:
class SpecificRoutingLogic extends RoutingLogic {
override def select(message: Any, routees: IndexedSeq[Routee]): Routee = {
??? no sender here!
}
}
class SectionSpecificAttendantRouter extends CustomRouterConfig {
this: FlightAttendantProvider =>
override def routerDispatcher: String = Dispatchers.DefaultDispatcherId
//override def supervisorStrategy: SupervisorStrategy = SupervisorStrategy.defaultStrategy
override def createRouter(system: ActorSystem): Router = {
// Create 5 flight attendants
val attendants = (1 to 5) map { n =>
system.actorOf(Props(newFlightAttendant()), "Attendant-" + n)
}
new Router(new SpecificRoutingLogic())
}
}
推荐答案
文档没有提及何时从<$ c继承$ c> CustomRouterConfig ,Akka引擎不会为您创建路由。因此,没有路由您的消息。我会这样做:
Documentation does not mention about when you inherit from CustomRouterConfig
, Akka engine does not create routees for you. Therefore nothing routes your messages. I would do that way:
class SpecificRoutingLogic extends RoutingLogic {
override def select(message: Any, routees: IndexedSeq[Routee]): Routee = {
val selected = routees.take(5)
if(selected.isEmpty) NoRoutee
else selected
}
}
class SectionSpecificAttendantRouter(nrOfInstances: Int) extends Pool {
this: FlightAttendantProvider =>
override def routerDispatcher: String = Dispatchers.DefaultDispatcherId
override def createRouter(system: ActorSystem): Router = {
new Router(new SpecificRoutingLogic())
}
}
我已更改 SectionSpecificAttendantRouter
现在扩展了 Pool
而不是 CustomRouterConfig
,这是我上面写的问题。当您具有此实现(我认为就是这样)时,您需要创建Router,诸如此类 SectionSpecificAttendantRouter(5).props(Props [YourWorker])
例如在 system.actorOf()
内部调用以创建具体的路由器。请记住,如果要直接从CustomRouterConfig继承,则必须自己实现创建Routees。看看类 akka.routing.RoutedActorCell 特别方法 start()
。如果您使用的不是Pool或Group,则必须自己进行一些初始化工作。
I've changed SectionSpecificAttendantRouter
extends now Pool
not CustomRouterConfig
due to issue I've wrote above. When you have this implementation (I think thats all) you create Router you have to do somethin like this SectionSpecificAttendantRouter(5).props(Props[YourWorker])
- this code have to be invoked inside for example system.actorOf()
to create concrete router. Remember, if you want to inherit directly from CustomRouterConfig you have to implement creating Routees by yourself. Take a look at class akka.routing.RoutedActorCell particulary method start()
. If you use something than Pool or Group you have to do some initialization stuff by yourself.
这篇关于如何在Akka 2.3中使用自定义路由器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!