원격 액터가 Bar
이고 로컬 액터가 Foo
입니다. CLI를 실행할 때마다 Foo
을 사용하여 Bar
에 메시지를 전달하고 싶습니다.Akka 리모팅을 사용하여 CLI를 통해 원격 액터에게 메시지를 보낼 수 있습니까?
Bar
은 메시지를 성공적으로 전달할 수 있지만 메시지를 기다리는 동안 Foo
은 중단됩니다. 이 문제를 해결하기 위해 Foo
의 메인 끝에 sys.exit(0)
을 추가했습니다. 이로 인해 Foo
의 시스템과 연결 문제가 발생합니다.
로컬 액터를 수동으로 종료하지 않고 연속 CLI 발급간에 로컬 액터를 종료하려면 어떻게해야합니까?
코드를 종료하고 알려주세요!
푸 :
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://[email protected]: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)
}
012,351,
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
}
}
}
바 :
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"
build.sbtMain.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
}
}
}
sbt 'run-main Main -m hello'
와 실행 Foo
및 sbt 'run-main Main'
와 Bar
를 실행합니다.
죄송합니다. 긴 코드는 사용하지만 내 문제는 MVCE입니다.
원하는 동작을 얻으려면 어떻게해야합니까? 원격 액터가 새 메시지를 기다리는 동안 CLI 액터가 연속 CLI 호출 사이에 없어집니다.
가 왜'Bar'가 죽었다는 것을 생각합니까? 이것을 나타내는 로그에 뭔가가 있습니까? –
@ PawełBartkiewicz 나는 내 의미를 정리하려고 노력했다. 미스 피크를 유감스럽게 생각합니다. :) 바라건대 그것은 더 분명하다. – erip