Friday, March 27, 2009

Simple cron like scheduler in Scala

Today in my Scala explorations I ran into the problem that I wanted some scheduler like Quarz or java.util.Timer -- that should be easy in scala I thought an came up with the following:

private val timedActor = actor {
//once a day
while (true) {
val c = Calendar.getInstance
c.set(Calendar.HOUR_OF_DAY, 0) //midnight
c.add(Calendar.DAY_OF_YEAR, 1) //next day
val sleepAmount = c.getTimeInMillis - Calendar.getInstance.getTimeInMillis

Pretty slick heavy weight (check the comment for a better implementation) -- they should include that in one of their libraries...


Anonymous Anonymous said...

You should be aware that this will cause the [heavyweight] thread to block, not just the lightweight process. If all threads in the concurrency threadpool are simultaneously blocked, none of the other lightweight processes can execute on them, and Scala starts spawning more threads. What you might do instead if you want this in lightweight threads is have a reactWithin(time) that only reacts to the TIMEOUT message.

3:03 PM


Post a Comment

<< Home