如何使用Akka Remoting通过CLI将消息发送给远程演员?

问题描述:

我有一个远程演员,Bar和本地演员Foo。我希望使用Foo在每次调用CLI时将消息传递给Bar如何使用Akka Remoting通过CLI将消息发送给远程演员?

Bar可以成功传递消息,但Foo在等待消息时挂起。为了解决这个问题,我在Foo的最后添加了一个sys.exit(0)。这会导致与Foo的系统有关联问题。

如何在连续CLI发行之间关闭本地演员而不手动杀死本地演员?

闭嘴,给我代码!


的Foo:

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) 
} 

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 
    } 
    } 
} 

sbt 'run-main Main -m hello'运行Foo,并与sbt 'run-main Main'运行Bar

对不起,长的代码,但它是我的问题MVCE。

我该如何实现自己想要的行为 - CLI角色会在连续的CLI调用与远程参与者等待新消息之间死亡。

+1

为什么你认为'Bar'死了?日志中是否有指示这一点的内容? –

+0

@PawełBartkiewicz我试图澄清我的意思。对不起,这个错误。 :)希望它更清楚。 – erip

发生这种情况,因为你将消息发送到FooActor后立即拨打sys.exit(0),所以有显著的机会,FooActor之前退出应用程序获取到甚至阅读信息的机会,更不用说将其转发给BarActor

似乎有成为many possible solutions,其中之一是:

class Foo extends Actor { 
    // create the remote actor 
    val BarActor = context.actorSelection("akka.tcp://[email protected]: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://[email protected]: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" 
    } 
    } 
} 
+0

这似乎是一个固定的解决方案,如果它是一次性消息,但我希望'酒吧'基本上总是等待消息。这会达到这个目的吗? – erip

+1

BarSystem不是一个单独的演员系统吗? –

+0

对不起,编辑后没有看到您的评论。是的,我相信这应该可以正常工作(现在不能自己尝试,如果你想的话,我可以稍后再做)。由于'FooSystem'和'BarSystem'是独立的actor系统,'shutdown'或'terminate'只能停止'FooSystem'([documentation](http://doc.akka.io/api/akka/2.4/index.html #[email protected]():scala.concurrent.Future [akka.actor.Terminated]))。 –