How can I have an Akka actor executed every 5 min?

前端 未结 4 1785
离开以前
离开以前 2020-12-23 11:01

I\'d like to know if there are any mechanism in Akka that can have an actor executed periodically?

4条回答
  •  庸人自扰
    2020-12-23 11:53

    The approach using schedule is one good approach, although there is a potential for the messages to queue up if the work done on schedule is so great that it might take longer than the scheduled interval. If you want the interval to occur between the end of one iteration and the beginning of the next, then use scheduleOnce with the following pattern:

    import akka.actor.Actor
    import scala.concurrent.duration._
    
    class SchedulingActor extends Actor {
    
      override def preStart(): Unit = {
        self ! "Do Some Work"
      }
    
      def receive = {
        case "Do Some Work" => 
          doWork
          context.system.scheduler.scheduleOnce(5 minutes, self, "Do Some Work")
      }
    
      def doWork = ???
    }
    

提交回复
热议问题