我想定義一個可以與Akka actor混合的特性,該特性可以在一定的持續時間之後調度接收超時。這裏是什麼,我想要做一個素描......訪問mixin中的Akka Actor上下文
trait BidderInActivityClearingSchedule[T <: Tradable, A <: Auction[T, A]]
extends ClearingSchedule[T, A] {
this: AuctionActor[T, A] =>
context.setReceiveTimeout(timeout) // can I call this here?
def timeout: FiniteDuration
override def receive: Receive = {
case ReceiveTimeout =>
val (clearedAuction, contracts) = auction.clear
contracts.foreach(contract => settlementService ! contract)
auction = clearedAuction
case message => this.receive(message)
}
}
class FancyAuctionActor[T <: Tradable](val timeout: FiniteDuration, ...)
extends AuctionActor[T, FancyAuctionActor[T]]
with BidderInActivityClearingSchedule[T, FancyAuctionActor[T]]
...但我不明白,當context.setReceiveTimeout
將被調用。當MyFancyAuctionActor
被調用時,它會作爲構造函數的一部分被調用嗎?或者它會早些時候被調用,並因此拋出某種錯誤,因爲事實上timeout
尚未定義。
使用actor生命週期鉤子並不是一個壞主意,但我不想擴展Actor我想保持特性爲混合。 – davidrpugh
據我所知,可堆疊mixin的最佳實踐方法是讓所有mixin擴展基本特徵。你實際上只是在你的情況下使用自我類型註釋來做同樣的事情,而你卻剝奪了重載方法的優勢。還有另一個技巧來實現可堆疊的mixin,我將其描述爲更新回答 – shayan
我熟悉可堆疊的actor模式,並在庫中使用它。也許我很迂腐,但我不喜歡有明顯的mixin從Actor延伸出來的特質。 – davidrpugh