为我的代码设计实例化 Actor System

Instantiate Actor System for my code design

我正在重构 akka 代码,结果遇到无法在 actor 系统中实例化 actor 的情况

我有一个 BaseActor,它具有我的其他演员使用的所有常用功能

trait baseActor extends Actor with timeout {
  implicit val system = context.system
  implicit val dispatcher = system.dispatchers.lookup("myCustomDispatcher")
  implicit val ldapService = ldapSerice
}

我还有一个个人服务来对 Ldap 进行主要的 CRUD 操作

trait PersonService {
  def addUser(user:User)(implicit ldapSerice:Ldapservice) = ???
  def getUser(uuid:String)(implicit ldapSerice:Ldapservice): User = ???
}

但隐式使用 ldap 服务,假设我需要此 ldap 服务来进行 ldap 连接和调用,因为其他一些开发人员已经实现了它,所以我必须遵循相同的模式


当我尝试从 Main class 实例化 actor 系统时,我不确定如何将该 actor 系统传递给基本 actor。所以所有创建的儿童演员都可以使用那个

object Main extends App {
   val system = ActorSystem(config.settings.getString("systemName"))
   val myActor = system.actorOf(Props[MyActor],"MyActor")
}

我的演员

class myactor extends baseActor with PersonService{
  overide def receive: Unit = {
    case "create" => addUser
    case "delete" => //my business logic
  }
}

虽然它在 运行 期间没有抛出任何编译错误,但它抛出异常无法创建 actor,因为未配置调度程序,但我可以在配置文件中看到它在这里。


hostName = "localhost"

//get the data from environment variable
//hostName = {?HOST_NAME} //If environment variable exist pick that value or override

akka {
    #loggers = ["akka.event.Logging$DefaultLogger"]
    loggers = ["akka.event.slf4j.Slf4jLogger"]
    loglevel = "DEBUG"
    logging-filter="akka.event.slf4j.Slf4jLoggingFilter"

    debug {
        receive = on
        lifecycle = on
        event-stream = on
    }
}

clusterLdap {
    version = 10
    name = "MyApplication"
    ldap {
        hostName = ""
        port = 20681
        bindDN = ""
        password = ""
        name = ""
        permissions = ["read","write","bind"]
    }
}
akka {
    provider = "cluster"
    wfm-dispatcher = {
        type = dispatcher
        executor = "fork-join-executor"
        fork-join-executor {
            parallelism-min = 4
            parallelism-factor = 2.0
            parallelism-max = 8
        }
    }
    remote {
        netty.tcp {
            hostName = "localhost"
            port = 2022
        }
    }
    
    cluster {
     seedNodes = ["akka.tcp://MyCluster@localhost:2541"]
     roles = ["service"]
    }
}

Akka Documentation on Classic Dispatchers 中,它指出:

The dispatcher you specify in withDispatcher and the dispatcher property in the deployment configuration is in fact a path into your configuration. So in this example it’s a top-level section, but you could for instance put it as a sub-section, where you’d use periods to denote sub-sections, like this: "foo.bar.my-dispatcher"

在上面的配置中,调度程序路径是 "akka.wfm-dispatcher",所以这是您需要用来查找它的字符串。或者,您可以将调度程序配置放在顶层而不是 akka 部分内,并查找为 "wfm-dispatcher".

另请注意,上面的代码实际上不会使用您的自定义调度程序来 运行 这些演员。要使用您的调度程序将 actor 配置为 运行(假设配置文件未更改),您可以使用:

system.actorOf(Props[MyActor].withDispatcher("akka.wfm-dispatcher"),"MyActor")