scala – 如何在Akka中获取对现有ActorSystem的引用?
是否可以在Akka(
scala)中获取对现有ActorSystem的引用?
我正在与另一个DB的Actor一起开发Spray应用程序.我也在扩展Directives以获得每个路径的对象.指令本身不是actor,但是它们需要将消息传递给DBActor.这里: class HttpActor extends Actor with HttpService { val actorRefFactory = context def receive = runRoute( IndexService.route ~ HostsService.route ) } object HostsService extends Directives{ def route(implicit dm: DetachMagnet2) = { path("hosts") { get { detach() { **dbActor ! CreateHost** complete("get me hosts!") } } ~ post { detach() { entity(as[String]) { payload => complete(s"post hosts $payload") } } } } } } 有没有办法让HostsService发现ActorSystem本身,这样他就可以找到DBActor,或者HttpActor是否必须传递它?后者似乎不太优雅,因为HostsService需要成为一个类(不是对象),所以不再是单例. 解决方法
从
here开始:
在您的情况下,最好将您的系统隐式传递给路由功能: class HttpActor extends Actor with HttpService { implicit val actorRefFactory = context def receive = runRoute( IndexService.route ~ HostsService.route ) } object HostsService extends Directives { def route(implicit dm: DetachMagnet2,as: ActorContext) = {...} } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |