在其他 class 中使用 MainActor class 的配置值

Use config value from MainActor class in other class

我在我的项目中使用 Akka 并在我的 MainActor class 中提取配置值。我希望能够在另一个文件中使用 commitversionauthor tag 来构建 avro 响应,但我不能简单地制作 MainActor我的 Avro 响应接口的父级 class。有解决方法吗?

我的MainActorclass

class MainActor extends Actor with ActorLogging with ConfigComponent with ExecutionContextComponent with DatabaseComponent with DefaultCustomerProfiles {

  override lazy val config: Config = context.system.settings.config

  override implicit lazy val executionContext: ExecutionContext = context.dispatcher

  override val db: Database = Database.fromConfig(config.getConfig("com.ojolabs.customer-profile.database"))

  private val avroServer = context.watch {
    val binding = ReflectiveBinding[CustomerService.Async](customerProfileManager)

    val host = config.getString("com.ojolabs.customer-profile.avro.bindAddress")
    val port = config.getInt("com.ojolabs.customer-profile.avro.port")

    context.actorOf(AvroServer.socketServer(binding, host, port))
  }

  val commit = config.getString("com.ojolabs.customer-profile.version.commit")
  val author = config.getString("com.ojolabs.customer-profile.version.author")
  val tag =  config.getString("com.ojolabs.customer-profile.version.tag")
  val buildId = config.getString("com.ojolabs.customer-profile.version.buildId")

  override def postStop(): Unit = {
    db.close()
    super.postStop()
  }

  //This toplevel actor does nothing by default
  override def receive: Receive = Actor.emptyBehavior

}

class我要拉值到

trait DefaultCustomerProfiles extends CustomerProfilesComponent {
  self: DatabaseComponent with ExecutionContextComponent =>

  lazy val customerProfileManager = new CustomerService.Async {

    import db.api._

    override def customerById(id: String): Future[AvroCustomer] = {
      db.run(Customers.byId(UUID.fromString(id)).result.headOption)
        .map(_.map(AvroConverters.toAvroCustomer).orNull)
    }

    override def customerByPhone(phoneNumber: String): Future[AvroCustomer] = {
      db.run(Customers.byPhoneNumber(phoneNumber).result.headOption)
        .map(_.map(AvroConverters.toAvroCustomer).orNull)
    }

    override def findOrCreate(phoneNumber: String, creationReason: String): Future[AvroCustomer] =  {
      db.run(Customers.findOrCreate(phoneNumber, creationReason)).map(AvroConverters.toAvroCustomer)
    }

    override def createEvent(customerId: String, eventType: String, version: Double, data: String, metadata: String): Future[AvroCustomerEvent] = {

      val action = CustomerEvents.create(
        UUID.fromString(customerId),
        eventType,
        Json.parse(data),
        version,
        Json.parse(metadata)
      )

      db.run(action).map(AvroConverters.toAvroEvent)
    }

    override def getVersion() : Version = {


    }
}

创建另一个定义值的特征,并将其与您的 MainActor 和 DefaultCustomerProfiles 特征混合。

trait AnvroConfig {
   self: ConfigComponent

      val commit = config.getString("com.ojolabs.customer-profile.version.commit")
      val author = config.getString("com.ojolabs.customer-profile.version.author")
      val tag =  config.getString("com.ojolabs.customer-profile.version.tag")
      val buildId = config.getString("com.ojolabs.customer-profile.version.buildId")
}

我认为您真正需要的是 Akka Extension, which enables you to add features, like custom config, to your Akka system in an elegant way. This way, you would have access to those config values within all your actors from the actor system. As an example, check out this nice blog post

至于您示例中的另一个 class,您应该将它们作为参数传递 - 它应该关注配置本身的检索和解析。