我想定义一个可以与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