Akka ActorSystem控制

时间:2012-09-21 18:33:54

标签: scala network-programming playframework-2.0 akka actor

我的环境是scala akka和戏剧!框架。我想知道是否有控制演员系统的创建或任何其他想法。

我的想法是创建远程演员,在用户点击购买时处理授权。因此,当用户发布帖子时,我在动作方法中创建远程actor系统和actor:

def payment = Action { implicit request =>
  var actorObject: Array[String] = new Array[String](23)

  val system = ActorSystem("RemoteSystem", ConfigFactory.load.getConfig("remotecreation")

  val worker = system.actorOf(Props[authNetActor.AuthNetActorMain].withRouter(FromConfig()), name = "remoteActor")
  ...
  system.shutdown()
}

这是application.conf

中remotecreation的定义
remotecreation {        #user defined name for the configuration
    include "common"
    akka {
            actor {
                    serialize-messages = on
                    serialize-creators = on

                    serializers {
                            proto = "akka.serialization.ProtobufSerializer"
                            java = "akka.serialization.JavaSerializer"
                            arr = "models.ArraySerializer"
                    }

                    serialization-bindings {
                            "com.google.protobuf.Message" = proto
                            "java.lang.String" = java
                            "java.util.Arrays" = java
                            "scala.Array" = arr
                            "akka.actor.ActorRef" = java
                    }

                    deployment {
                            /remoteActor {  #Specifically has to be the name of the remote actor
                                    remote = "akka://ActorApplication@172.17.100.232:2552"
                                    router = "round-robin"
                                    nr-of-instances = 1
                            }
                    }
            }
            remote.netty.port = 2554
    }
}

我遇到的问题是,当我连续两次提交时,我收到一个错误,因为我试图在已经有一个actor系统的ip地址上创建一个actor系统。

我绝对认为我需要移动它,但我不确定在哪里,因为这将是一个广泛的多用户游戏!应用程序,当数百名用户使用该应用程序时,我不确定在何处可以创建actor系统而不会发生冲突。

感谢任何想法,建议或帮助。

1 个答案:

答案 0 :(得分:3)

每次通话都不要启动(远程)ActorSystem。相反,启动应用程序范围的actor系统(或使用默认系统,请参阅integrating Play with Akka)。

添加到您的application.conf

akka {

  actor {
    provider = "akka.remote.RemoteActorRefProvider"
  }

  remote {
    transport = "akka.remote.netty.NettyRemoteTransport"
    netty {
      hostname = "127.0.0.1"
      port = 0 # 2552 seems to be bound with play 2.0.2 ?
    }
  }
}

然后使用默认的Play actorsystem来获取对控制器中远程actor的引用:

private val interpreters = Akka.system.actorFor(
  "akka://interpreter_system@127.0.0.1:2552/user/interpreters")

如果要渲染actor响应,甚至可以将Akka Future转换为Scala Promise。我建议继续使用Akka Futures的可组合性,然后在最后一个词干将Future[Result]转换为Promise。

new AkkaPromise(
  interpreters.ask(InterpretersComm.Request(sid, line)).mapTo[String]) map (Ok(_))