Created
April 1, 2014 18:34
-
-
Save fancellu/9920186 to your computer and use it in GitHub Desktop.
ThrottledProcessor.scala (little example for an intro to Scala talk)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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