On Wed, Apr 9, 2014 at 10:14 AM, Konrad Malawski <
[email protected]> wrote:

> Hello Soumya,
> You can use the scheduler the same way you're already doing it from within
> an Actor too.
>
Inside an actor you should always schedule a message to self instead of
running a block of code. The reason is to avoid closing over things in the
actor and thereby access things in the actor from another thread.

More information about scheduling messages can be found here:
http://doc.akka.io/docs/akka/2.3.1/scala/howto.html#scheduling-periodic-messages

Cheers,
Patrik



>  // inside an Actor (Subscribe Actor)
> context.system.scheduler.schedule(30.seconds, 30.seconds) {
>   // your publishing here
> }
>
> I hope this helps, happy hakking!
>
> --
> Cheers,
> Konrad Malawski
> blog.project13.pl | java.pl | geecon.org  | gdgkrakow.pl | krakowscala.pl
>
>
> 2014-04-09 5:49 GMT+02:00 Soumya Simanta <[email protected]>:
>
> 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.
>>
>
>  --
> >>>>>>>>>> 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.
>



-- 

Patrik Nordwall
Typesafe <http://typesafe.com/> -  Reactive apps on the JVM
Twitter: @patriknw
JOIN US. REGISTER TODAY! <http://www.scaladays.org/>
Scala <http://www.scaladays.org/>
Days <http://www.scaladays.org/>
June 16th-18th, <http://www.scaladays.org/>
Berlin <http://www.scaladays.org/>

-- 
>>>>>>>>>>      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.

Reply via email to