如何确保路由器演员采用"组模式"已经关联远程路由?

时间:2016-11-16 05:37:14

标签: scala akka

对于具有"组模式"的路由器角色,我们提前新建了一系列角色,然后我们将使用actorOf将一个路由器与远程路由相关联。

事实上,它内部将使用actorSelection,我的问题是:how to assure the association already finish?

对于actorSelection,我们可以使用resolveOne来确保选择成功,然后发送消息。但路由器演员怎么样?请参阅以下代码,如何确保line2在向其发送消息之前与远程路由关联,就像行line1一样?

package local
import akka.actor._
import akka.routing.RoundRobinGroup
import akka.util.Timeout
import scala.concurrent.duration._
import com.typesafe.config.ConfigFactory
import scala.util.{Failure, Success}

object Local extends App {
  val config = ConfigFactory.parseString(s"akka.remote.netty.tcp.port=2251")
  val system = ActorSystem("LocalSystem", config.withFallback(ConfigFactory.load()))
  system.actorOf(Props[LocalActor], "LocalActor")
  val config2 = ConfigFactory.parseString(s"akka.remote.netty.tcp.port=2252")
  val system2 = ActorSystem("LocalSystem2", config2.withFallback(ConfigFactory.load()))
  system2.actorOf(Props[LocalActor2], "LocalActor2")
}
class LocalActor extends Actor {
  def receive = {
    case _ => println("hi")
  }
}
class LocalActor2 extends Actor {
  import scala.concurrent.ExecutionContext.Implicits.global
  implicit val timeout = Timeout(5 seconds)
  val a = context.actorSelection("akka.tcp://LocalSystem@127.0.0.1:2251/user/LocalActor")
  a.resolveOne().onComplete { // line 1
    case Success(actor) => println("ready")
      a ! 1
    case Failure(ex) => println("not ready")
  }
  val paths = List("akka.tcp://LocalSystem@127.0.0.1:2251/user/LocalActor")
  val b = context.actorOf(RoundRobinGroup(paths).props(), "LocalActorRouter") // line 2
  b ! 1
  def receive = {
    case _ =>
  }
}

0 个答案:

没有答案