2017-03-21 2 views
0

私のプロジェクトでAkkaを使用していて、MainActorクラスの設定値を取得しています。 Avroレスポンスを構築するために別のファイルの中にcommitversionauthortagを使用したいのですが、私は単にMainActorを私のAvro応答インターフェースの親クラスにすることはできません。回避策はありますか?他のクラスのMainActorクラスの設定値を使用

マイMainActorクラス

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 

} 

私は値を定義する別の形質を作成し、あなたのMainActorのとDefaultCustomerProfiles形質とそれを混ぜる

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 = { 


    } 
} 

答えて

2

に値を引っ張っするクラス。私はあなたが本当に必要なものだと思う

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") 
} 
0

は、カスタム設定のような、洗練された方法であなたのアッカシステムに、機能を追加することが可能にする、Akka Extensionです。これにより、アクターシステムのすべてのアクター内のこれらの設定値にアクセスできます。一例として、この素晴らしいblog postをチェックしてください。

例の他のクラスについては、パラメータとして渡す必要があります。これは、config自体の取得と解析に関係している必要があります。

関連する問題