Skip to main content

Posts

Showing posts from October, 2012

Akka: Printing list of actors

To print the list of actors that are running in the actor system, you can add following scheduler that will print actor path every 10 seconds: import akka.actor.{Actor, ActorIdentity, ActorLogging, ActorRef, Cancellable, Props} class TestActor extends Actor with ActorLogging { private var scheduler: Cancellable = _ @scala.throws[Exception](classOf[Exception]) override def preStart(): Unit = { import scala.concurrent.duration._ scheduler = context.system.scheduler.schedule( initialDelay = 0 seconds, interval = 10 seconds, receiver = self, message = ActorIdentity("", Some(self)) ) } @scala.throws[IOException](classOf[IOException]) override def postStop() = { scheduler.cancel } override def receive = { case ActorIdentity(_, Some(ref)) => log.info(ref.path.toString) } }

Scala simple concepts

A simple iteration over Array in Scala can be done as: val xs:Array[String] = Array[String]("a", "b", "c", "d", "e") for((x,i) <- xs.iterator.zipWithIndex) println(i + "th element is " + x) for((x,i) <- xs.view.zipWithIndex) println(i + "th element is " + x) for (i <- xs.indices) println(i + "th element is " + xs(i)) for (i <- 0 until xs.length) println(i + "th element is " + xs(i)) for (i <- xs.length - 1 to 0 by -1) println(i + "th element is " + xs(i))