Skip to content

Instantly share code, notes, and snippets.

@aoiroaoino
Last active August 29, 2015 14:07
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 aoiroaoino/4a636d99dddaa33f8427 to your computer and use it in GitHub Desktop.
Save aoiroaoino/4a636d99dddaa33f8427 to your computer and use it in GitHub Desktop.
// 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"
)
// $ 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