java - Scheduling a block of code to run every few seconds inside Akka actor in Play 2.4.2 -


i have actor class, manager extends untypedactor contains list of messages: listofmessages, list of client websocket connections: clientconnections, , method sendmessagestoclient sends every message in listofmessages every client, , resets listofmessages empty list. want method sendmessagestoclient executed every 2 seconds.

is there way schedule method run on interval within actor? docs show how schedule task different actor, shown here:

system.scheduler().schedule(     duration.create(0, timeunit.milliseconds), //initial delay 0 milliseconds     duration.create(30, timeunit.minutes),     //frequency 30 minutes     testactor,     "tick",     system.dispatcher(),     null ); 

but want inside manager class:

system.scheduler().schedule(     duration.create(0, milliseconds),     durant.create(2, "seconds"),     sendmessagestoclient() ); 

is possible?

yes possible. method prefer have actor send message itself, , within receive, call method want have executed. example (in scala since how use akka):

class example extends actor {    override def prestart() {     system.scheduler().schedule(      duration.create(0, timeunit.milliseconds), //initial delay 0 milliseconds      duration.create(30, timeunit.minutes),     //frequency 30 minutes      self, //send message      "tick",      system.dispatcher(),      null    );   }    def receive = {    case "tick" => sendmessagestoclient()   }    def sendmessagestoclient() {     //do work   } } 

this actor will, starts, schedule regular sending of message "tick" itself.

one of advantages see in handling scheduling way queues work done "tick" along other work done actor, helps ensure actor continues perform 1 task @ once, keeping thread safety within actor.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -