Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ThrottledProcessor.scala (little example for an intro to Scala talk)
1
2
222
Dump Vector(1, 2, 222)
Tue Apr 01 19:32:26 BST 2014 process 1
Tue Apr 01 19:32:27 BST 2014 process 2
Tue Apr 01 19:32:28 BST 2014 process 222
Dump Vector()
import akka.actor.ActorSystem
import scala.concurrent.duration._
import akka.actor.ActorDSL._
object ThrottledProcessor extends App {
implicit val system = ActorSystem("ThrottledProcessor")
import system.dispatcher
case object Dump
case object Process
val a = actor("myactor")(new Act {
var numbers = IndexedSeq[Int]()
become {
case Dump => println(s"Dump $numbers")
case Process => numbers =
numbers match {
case head +: tail => println(s"${new java.util.Date} process $head"); tail
case _ => numbers
}
case x: Int => println(x); numbers = numbers :+ x
}
system.scheduler.schedule(0.seconds, 1.second, self, Process)
})
Seq(1, 2, 222).foreach(a!_)
a ! Dump
Thread.sleep(3500)
a ! Dump
Thread.sleep(100)
system.shutdown
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment