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.Props
import java.net.InetSocketAddress
import redis.RedisClient
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import 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")
}
}Hello Soumya,
You can use the scheduler the same way you’re already doing it from within an Actor too.
// inside an Actor (Subscribe Actor)
context.system.scheduler.schedule(30.seconds, 30.seconds) {
// your publishing here
}
I hope this helps, happy hakking!
--
>>>>>>>>>> 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 akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.
Hello Soumya,
You can use the scheduler the same way you’re already doing it from within an Actor too.
You received this message because you are subscribed to a topic in the Google Groups "Akka User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/akka-user/gIq3gq-8yu0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to akka-user+...@googlegroups.com.