如何在 Akka 中获取对现有 ActorSystem 的引用?
How to get a reference to an existing ActorSystem in Akka?
是否可以在 Akka (scala) 中获取对现有 ActorSystem 的引用?
我正在与另一个 Actor 一起为 DB 开发一个 Spray 应用程序。我还扩展了指令,使每个路径都有一个对象。指令本身不是参与者,但它们需要将消息传递给 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 需要成为一个 class (不是一个对象),所以不再是一个单例。
来自here:
there was a ticket for creating such a registry, but we were not
satisfied with what we got when trying to specify the semantics in
detail. One part is that we removed all global state so that different
parts of an application can use Akka without having to worry about
each other and a global feature would break this. Another is that it
would encourage get-or-create usage—my pet peeve—which would make the
semantics unclear: you give a name and a config, but if the name
already exists you potentially get back a differently configured
system (which is usually quite fatal).
There is nothing stopping you from putting a hashmap in some central
place of your application, (pre-)populate that with the actor systems
you need and be done, that's basically a one-liner (which is another
reason for not including it in Akka, because instead of a simple
solution to a very narrow problem we'd have to think of a solution to
a much more generic problem)
在您的情况下,最好将您的系统隐式传递给 route
函数:
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) = {...}
}
是否可以在 Akka (scala) 中获取对现有 ActorSystem 的引用?
我正在与另一个 Actor 一起为 DB 开发一个 Spray 应用程序。我还扩展了指令,使每个路径都有一个对象。指令本身不是参与者,但它们需要将消息传递给 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 需要成为一个 class (不是一个对象),所以不再是一个单例。
来自here:
there was a ticket for creating such a registry, but we were not satisfied with what we got when trying to specify the semantics in detail. One part is that we removed all global state so that different parts of an application can use Akka without having to worry about each other and a global feature would break this. Another is that it would encourage get-or-create usage—my pet peeve—which would make the semantics unclear: you give a name and a config, but if the name already exists you potentially get back a differently configured system (which is usually quite fatal).
There is nothing stopping you from putting a hashmap in some central place of your application, (pre-)populate that with the actor systems you need and be done, that's basically a one-liner (which is another reason for not including it in Akka, because instead of a simple solution to a very narrow problem we'd have to think of a solution to a much more generic problem)
在您的情况下,最好将您的系统隐式传递给 route
函数:
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) = {...}
}