2009-08-17 18:42:41 +02:00
|
|
|
package se.scalablesolutions.akka.kernel.util
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent._
|
|
|
|
|
import kernel.actor.{OneForOneStrategy, Actor}
|
|
|
|
|
|
|
|
|
|
import org.scala_tools.javautils.Imports._
|
|
|
|
|
|
|
|
|
|
case object UnSchedule
|
|
|
|
|
case class SchedulerException(msg: String, e: Throwable) extends RuntimeException(msg, e)
|
|
|
|
|
|
|
|
|
|
/**
|
2009-08-17 20:39:14 +02:00
|
|
|
* Rework of David Pollak's ActorPing class in the Lift Project
|
|
|
|
|
* which is licensed under the Apache 2 License.
|
2009-08-17 18:42:41 +02:00
|
|
|
*/
|
|
|
|
|
class ScheduleActor(val receiver: Actor, val future: ScheduledFuture[AnyRef]) extends Actor with Logging {
|
|
|
|
|
def receive: PartialFunction[Any, Unit] = {
|
|
|
|
|
case UnSchedule =>
|
|
|
|
|
Scheduler.stopSupervising(this)
|
|
|
|
|
future.cancel(true)
|
|
|
|
|
stop
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object Scheduler extends Actor {
|
|
|
|
|
private var service = Executors.newSingleThreadScheduledExecutor(SchedulerThreadFactory)
|
|
|
|
|
private val schedulers = new ConcurrentHashMap[Actor, Actor]
|
|
|
|
|
faultHandler = Some(OneForOneStrategy(5, 5000))
|
|
|
|
|
trapExit = true
|
2009-08-17 20:39:14 +02:00
|
|
|
start
|
2009-08-17 18:42:41 +02:00
|
|
|
|
|
|
|
|
def schedule(receiver: Actor, message: AnyRef, initialDelay: Long, delay: Long, timeUnit: TimeUnit) = {
|
|
|
|
|
try {
|
|
|
|
|
startLink(new ScheduleActor(
|
|
|
|
|
receiver,
|
|
|
|
|
service.scheduleAtFixedRate(new java.lang.Runnable {
|
|
|
|
|
def run = receiver ! message;
|
|
|
|
|
}, initialDelay, delay, timeUnit).asInstanceOf[ScheduledFuture[AnyRef]]))
|
|
|
|
|
} catch {
|
|
|
|
|
case e => throw SchedulerException(message + " could not be scheduled on " + receiver, e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def restart = service = Executors.newSingleThreadScheduledExecutor(SchedulerThreadFactory)
|
|
|
|
|
|
|
|
|
|
def stopSupervising(actor: Actor) = {
|
|
|
|
|
unlink(actor)
|
|
|
|
|
schedulers.remove(actor)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def shutdown = {
|
|
|
|
|
schedulers.values.asScala.foreach(_ ! UnSchedule)
|
|
|
|
|
service.shutdown
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def receive: PartialFunction[Any, Unit] = {
|
|
|
|
|
case _ => {} // ignore all messages
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private object SchedulerThreadFactory extends ThreadFactory {
|
|
|
|
|
private var count = 0
|
|
|
|
|
val threadFactory = Executors.defaultThreadFactory()
|
|
|
|
|
def newThread(r: Runnable): Thread = {
|
|
|
|
|
val thread = threadFactory.newThread(r)
|
|
|
|
|
thread.setName("Scheduler-" + count)
|
|
|
|
|
thread.setDaemon(true)
|
|
|
|
|
thread
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-08-17 20:39:14 +02:00
|
|
|
|
|
|
|
|
|