只是想知道取消时间表的最佳方法是什么。
目前我使用链接到调度程序任务的var
,并将其取消,如下所示:
private var scheduterLink: akka.actor.Cancellable = null
def receive = {
case Test => {
if(...){scheduterLink.cancel}
}
如果没有 var 链接,如何做更多示例?
谢谢!
答案 0 :(得分:1)
如果您更喜欢不可变的解决方案,您可以像这样更改Actor的状态:
def receive: Receive = withoutCancel
def withoutCancel:Receive = {
case x =>
if(???){
context.become(withCancel(context.system.scheduler.schedule(....)))
}
???
}
def withCancel(cancellable:Cancellable):Receive = {
case Test =>
if(???){
cancellable.cancel()
context.unbecome()
}
???
}
答案 1 :(得分:0)
我想你应该试试这个:
ActorRef tickActor = system.actorOf(Props.create(Ticker.class, this));
//This will schedule to send the Tick-message
//to the tickActor after 0ms repeating every 50ms
Cancellable cancellable = system.scheduler().schedule(Duration.Zero(),
Duration.create(50, TimeUnit.MILLISECONDS), tickActor, "Tick",
system.dispatcher(), null);
//This cancels further Ticks to be sent
cancellable.cancel();
获取