scala - How to use custom Router in Akka 2.3? -
update: source code whole akka project available here, problem filed issue.
i having trouble rewriting example of custom router akka concurrency book (section 10.6). here 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 => // routerconfig requires fill out these 2 // fields. know supervisorstrategy we're // aware of dispatcher, // discussed in detail later def routerdispatcher: string = dispatchers.defaultdispatcherid def supervisorstrategy: supervisorstrategy = supervisorstrategy.defaultstrategy // createroute method invokes decision // making code. instantiate actors need , // create routing code def createroute(routeeprovider: routeeprovider): route = { // create 5 flight attendants val attendants = (1 5) map { n => routeeprovider.context.actorof(props(newflightattendant), "attendant-" + n) } // register them provider - important. // if forget this, nobody's going // tell :) routeeprovider.registerroutees(attendants) // partial function calculates route. // going route based on name of // incoming sender. of course, cache or // slicker. { case (sender, message) => import passenger.seatassignment val seatassignment(_, row, _) = sender.path.name list(destination(sender, attendants(math.floor(row.toint / 11).toint))) } } } my questions:
- should extend
pool,routerconfigorcustomrouterconfig? - how can
senderreference in order calculate index flight attendant's path?
here broken beginnings:
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 5) map { n => system.actorof(props(newflightattendant()), "attendant-" + n) } new router(new specificroutinglogic()) } }
documentation not mention when inherit customrouterconfig, akka engine not create routees you. therefore nothing routes messages. 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()) } } i've changed sectionspecificattendantrouter extends pool not customrouterconfig due issue i've wrote above. when have implementation (i think thats all) create router have somethin sectionspecificattendantrouter(5).props(props[yourworker]) - code have invoked inside example system.actorof() create concrete router. remember, if want inherit directly customrouterconfig have implement creating routees yourself. take @ class akka.routing.routedactorcell particulary method start(). if use pool or group have initialization stuff yourself.
Comments
Post a Comment