NOTE: Cross posting it here for better coverage. http://stackoverflow.com/questions/22952336/sending-a-message-from-an-akka-actor-at-fixed-interval
I want to publish messages from an Actor at a fixed rate. I extended this example<https://github.com/etaty/rediscala-demo/blob/master/src/main/scala/ExamplePubSub.scala> below where the current time is published a Redis channel every 2 seconds. In the SubscribeActor I'm keeping track of some state (in this case the aggregate). I want to publish the value of this aggregate to another Redis channel at fixed intervals (say every 30 seconds) import akka.actor.Propsimport java.net.InetSocketAddressimport redis.RedisClientimport scala.concurrent.duration._import scala.concurrent.ExecutionContext.Implicits.globalimport redis.api.pubsub.{PMessage, Message} import redis.actors.RedisSubscriberActor object AkkaScheduler extends App { implicit val akkaSystem = akka.actor.ActorSystem() val redis = RedisClient() //publish the current time every 2 seconds on Redis channel "time" akkaSystem.scheduler.schedule(2 seconds, 2 seconds)(redis.publish("time", System.currentTimeMillis())) //channel and patterns to subscribe to val channels = Seq("time") val patterns = Seq("pattern.*") // create SubscribeActor instance akkaSystem.actorOf(Props(classOf[SubscribeActor], "input", channels, patterns).withDispatcher("rediscala.rediscala-client-worker-dispatcher")) } class SubscribeActor(name: String, channels: Seq[String] = Nil, patterns: Seq[String] = Nil) extends RedisSubscriberActor(new InetSocketAddress("localhost", 6379), channels, patterns) { //hold state till it's time to publish it again var aggregate: BigInt = 0 def onMessage(message: Message) { println(s" $name message received: $message") aggregate = aggregate + BigInt(message.data.toInt) //Publish this aggregate to another Redis channel at a fixed interval (e.g., every 30 seconds) } def onPMessage(pmessage: PMessage) { println(s"pattern message received: $pmessage") }} -- >>>>>>>>>> Read the docs: http://akka.io/docs/ >>>>>>>>>> Check the FAQ: >>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user --- You received this message because you are subscribed to the Google Groups "Akka User List" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/akka-user. For more options, visit https://groups.google.com/d/optout.
