Skip to content

Instantly share code, notes, and snippets.

@fancellu
Created April 1, 2014 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fancellu/9920186 to your computer and use it in GitHub Desktop.
Save fancellu/9920186 to your computer and use it in GitHub Desktop.
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