Last active
August 29, 2015 14:07
-
-
Save aoiroaoino/4a636d99dddaa33f8427 to your computer and use it in GitHub Desktop.
This file contains 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
// http://doc.akka.io/docs/akka/2.3.6/contrib/throttle.html | |
name := "TimerBasedThrottler-Sample" | |
version := "0.0.1-SNAPSHOT" | |
scalaVersion := "2.10.4" | |
resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/" | |
libraryDependencies ++= Seq ( | |
"com.typesafe.akka" %% "akka-actor" % "2.3.6", | |
"com.typesafe.akka" %% "akka-contrib" % "2.3.6" | |
) |
This file contains 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
// $ sbt console | |
import akka.actor.{Actor, ActorSystem, Props} | |
import akka.contrib.throttle.Throttler._ | |
import akka.contrib.throttle.TimerBasedThrottler | |
import scala.concurrent.duration._ | |
class PrintActor extends Actor { | |
def receive = { | |
case x => println(x) | |
} | |
} | |
val system = ActorSystem("my-system") | |
val printer = system.actorOf(Props[PrintActor]) | |
val throttler = system.actorOf(Props(classOf[TimerBasedThrottler], 3 msgsPer 1.second)) | |
throttler ! SetTarget(Some(printer)) | |
(0 to 10) foreach {throttler ! _} | |
/* | |
scala> (0 to 10) foreach {throttler ! _} | |
0 | |
1 | |
2 | |
--- 1 sec | |
3 | |
4 | |
5 | |
--- 1 sec | |
6 | |
7 | |
8 | |
--- 1 sec | |
9 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment