我有一个远程角色Bar
和一个本地角色Foo
。我希望在每次调用CLI时使用Foo
将消息传递给Bar
。
Bar
可以成功传递消息,但Foo
在等待消息时挂起。为了解决这个问题,我在sys.exit(0)
的主要部分末尾添加了Foo
。这会导致Foo
系统的关联问题。
如何在连续的CLI发布之间关闭我的本地actor,而不会手动杀死我的本地actor?
build.sbt
name := "Foo"
version := "1.0"
scalaVersion := "2.11.8"
libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.4.11"
libraryDependencies += "com.typesafe.akka" %% "akka-remote" % "2.4.11"
libraryDependencies += "com.github.scopt" %% "scopt" % "3.5.0"
fork in run := true
Main.scala
import akka.actor._
import com.typesafe.config.ConfigFactory
case class Config(mode: String = "", greeting: String="")
class Foo extends Actor {
// create the remote actor
val BarActor = context.actorSelection("akka.tcp://BarSystem@127.0.0.1:2552/user/BarActor")
def receive = {
case method: String => BarActor ! method
}
}
object CommandLineInterface {
val config = ConfigFactory.load()
val system = ActorSystem("FooSystem", config.getConfig("FooApp"))
val FooActor = system.actorOf(Props[Foo], name = "FooActor")
val parser = new scopt.OptionParser[Config]("Foo") {
head("foo", "1.x")
help("help").text("prints usage text")
opt[String]('m', "method").action( (x, c) =>
c.copy(greeting = x) ).text("Bar will greet with <method>")
}
}
object Main extends App {
import CommandLineInterface.{parser, FooActor}
parser.parse(args, Config()) match {
case Some(config) => FooActor ! config.greeting
case None => sys.error("Bad news...")
}
/*
When sys.exit(0) commented, this hangs and Bar greet.
When sys.exit(0) uncommented, this doesn't hang, but also Bar doesn't greet.
*/
//sys.exit(0)
}
application.conf
FooApp {
akka {
loglevel = "INFO"
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
enabled-transports = ["akka.remote.netty.tcp"]
netty.tcp {
hostname = "127.0.0.1"
port = 0
}
log-sent-messages = on
log-received-messages = on
}
}
}
build.sbt
name := "Bar"
version := "1.0"
scalaVersion := "2.11.8"
libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.4.11"
libraryDependencies += "com.typesafe.akka" %% "akka-remote" % "2.4.11"
Main.scala
import akka.actor._
import com.typesafe.config.ConfigFactory
class Bar extends Actor {
def receive = {
case greeting: String => Bar.greet(greeting)
}
}
object Bar {
val config = ConfigFactory.load()
val system = ActorSystem("BarSystem", config.getConfig("BarApp"))
val BarActor = system.actorOf(Props[Bar], name = "BarActor")
def greet(greeting: String) = println(greeting)
def main(args: Array[String]): Unit = {
/* Intentionally empty */
}
}
application.conf
BarApp {
akka {
loglevel = "INFO"
actor {
provider = remote
}
remote {
enabled-transports = ["akka.remote.netty.tcp"]
netty.tcp {
hostname = "127.0.0.1"
port = 2552
}
log-sent-messages = on
log-received-messages = on
}
}
}
使用Foo
投放sbt 'run-main Main -m hello'
,并使用Bar
投放sbt 'run-main Main'
。
对于长代码很抱歉,但这是我的问题的MVCE。
如何实现我想要的行为 - CLI演员在连续的CLI调用与远程actor等待新消息之间死亡。
答案 0 :(得分:3)
这种情况正在发生,因为您在向sys.exit(0)
发送消息后立即致电FooActor
,因此在FooActor
有机会阅读消息之前,应用程序很可能会退出,更不用说将它转发给BarActor
。
似乎有many possible solutions,其中一个是:
class Foo extends Actor {
// create the remote actor
val BarActor = context.actorSelection("akka.tcp://BarSystem@127.0.0.1:2552/user/BarActor")
override def receive = {
case method: String => {
BarActor ! method
self ! PoisonPill
}
}
override def postStop = {
context.system.terminate
}
}
不幸的是,事实证明在将邮件发送到Bar
之前系统仍会关闭。
如果您想以“即发即忘”的方式发送消息,我找不到任何合理的解决方案。但是,在大多数情况下,最好从远程演员处获得某种响应,所以你可以这样做:
class Foo extends Actor {
// create the remote actor
val BarActor = context.actorSelection("akka.tcp://BarSystem@127.0.0.1:2552/user/BarActor")
override def receive = {
case method: String => {
BarActor ! method
context.become(waitingToKillMyself)
}
}
def waitingToKillMyself: Receive = {
case response: String => {
println(response)
self ! PoisonPill
}
}
override def postStop = {
context.system.terminate
}
}
// ...
object Main extends App {
import CommandLineInterface.{parser, FooActor, system}
import system.dispatcher
parser.parse(args, Config()) match {
case Some(config) => {
FooActor ! config.greeting
system.scheduler.scheduleOnce(10.seconds, FooActor, PoisonPill)
}
case None => sys.error("Bad news...")
}
}
栏:
class Bar extends Actor {
def receive = {
case greeting: String => {
Bar.greet(greeting)
sender() ! "OK"
}
}
}