How should I schedule some simple delayed tasks in Scala?

早过忘川 提交于 2019-12-22 10:03:08

问题


I'm making a Chaos Monkey program and I want it to clean up after itself after a certain period of time. I'd like a simple way to queue up cleanup tasks to be called a set amount of time in the future. I think I could do something with actors and a lot of hand-waving but that seems like the wrong approach. Is there a better tool for this task in the Scala standard library?


回答1:


I have written a scheduling DSL in Scala called foil, which is freely available on Github. It will work with either of Java Calendar/Date, or the Joda library. The syntax looks like this:

schedule(f) now
schedule(f) onceAfter 5.minutes
schedule(f) onceAt inst
schedule(f) onceAtNext time
schedule(f) todayNoEarlierThan time

Where f is a closure (i.e. () => Unit). There's many more examples on the Wiki and an example REPL session with foil, showing how to use it (with both Java Date/Calendar and JODA).




回答2:


There's not so much hand waving involved, reactWithin combined with the TIMEOUT message will let you do this.

You can also use Futures.alarm to create a waitable Future that will resolve after the specified time limit.



来源:https://stackoverflow.com/questions/5797666/how-should-i-schedule-some-simple-delayed-tasks-in-scala

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!