简体   繁体   English

Akka ActorSystem控制

[英]Akka ActorSystem Control

my environment is scala akka and the play! 我的环境是scala akka和戏剧! framework. 框架。 I was wondering if there is anyway to control the creation of an actor system or any other ideas that can . 我想知道是否有控制演员系统的创建或任何其他可以的想法。

My idea is to create remote actors that will handle authorization when a user hits buy. 我的想法是创建远程演员,在用户点击购买时处理授权。 And so, I create the remote actor system and actors in an action method, when a user does a post: 因此,当用户执行帖子时,我在动作方法中创建远程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()
}

Here is the definition of remotecreation in the application.conf 这是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
    }
}

The problem I am having is that, when I submit twice in a row, I get an error because I am trying to create an actor system on an ip address that already has an actor system on it. 我遇到的问题是,当我连续两次提交时,我得到一个错误,因为我试图在已经有一个actor系统的ip地址上创建一个actor系统。

I definitely think I need to move it, but I'm not sure where because since this is going to be a wide, multi user play! 我当然认为我需要移动它,但我不确定在哪里,因为这将是一个广泛的多用户游戏! application, I'm not sure where I can put the creation of the actor system without being conflicted when hundreds of users use the application. 应用程序,我不知道在数百名用户使用该应用程序时,我可以在何处创建actor系统而不会发生冲突。

Any thoughts, suggestions, or help is appreciated. 任何想法,建议或帮助表示赞赏。

Don't start a (remote) ActorSystem per call. 不要在每次调用时启动(远程)ActorSystem。 Instead, start up an application-wide actor system (or use the default one, see integrating Play with Akka ). 相反,启动应用程序范围的actor系统(或使用默认系统,请参阅将Play与Akka集成 )。

Add to your application.conf : 添加到您的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 ?
    }
  }
}

Then use the default Play actorsystem for example to get a reference to the remote actor in your contorller: 然后使用默认的Play actorsystem来获取对控制器中远程actor的引用:

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

You might even convert an Akka Future to a Scala Promise if you want to render the actor response. 如果要渲染actor响应,甚至可以将Akka Future转换为Scala Promise。 I avice to keep using Akka Futures for composability, then at the very last stem convert the Future[Result] to a Promise. 我建议继续使用Akka Futures的可组合性,然后在最后一个词干将Future[Result]转换为Promise。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM