2012-06-01 08:24:47 -04:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package docs.circuitbreaker
|
|
|
|
|
|
|
|
|
|
//#imports1
|
2012-06-29 13:33:20 +02:00
|
|
|
import scala.concurrent.util.duration._ // small d is important here
|
2012-06-01 08:24:47 -04:00
|
|
|
import akka.pattern.CircuitBreaker
|
|
|
|
|
import akka.actor.Actor
|
|
|
|
|
import akka.dispatch.Future
|
|
|
|
|
import akka.event.Logging
|
|
|
|
|
|
|
|
|
|
//#imports1
|
|
|
|
|
|
2012-06-01 21:29:47 +02:00
|
|
|
class CircuitBreakerDocSpec {}
|
2012-06-01 08:24:47 -04:00
|
|
|
|
|
|
|
|
//#circuit-breaker-initialization
|
|
|
|
|
class DangerousActor extends Actor {
|
|
|
|
|
|
|
|
|
|
val log = Logging(context.system, this)
|
|
|
|
|
implicit val executionContext = context.dispatcher
|
|
|
|
|
val breaker =
|
|
|
|
|
new CircuitBreaker(context.system.scheduler, 5, 10.seconds, 1.minute)
|
|
|
|
|
.onOpen(notifyMeOnOpen)
|
|
|
|
|
|
|
|
|
|
def notifyMeOnOpen =
|
|
|
|
|
log.warning("My CircuitBreaker is now open, and will not close for one minute")
|
2012-06-01 21:29:47 +02:00
|
|
|
//#circuit-breaker-initialization
|
2012-06-01 08:24:47 -04:00
|
|
|
|
2012-06-01 21:29:47 +02:00
|
|
|
//#circuit-breaker-usage
|
2012-06-01 08:24:47 -04:00
|
|
|
def dangerousCall: String = "This really isn't that dangerous of a call after all"
|
|
|
|
|
|
|
|
|
|
def receive = {
|
2012-06-01 21:29:47 +02:00
|
|
|
case "is my middle name" ⇒
|
2012-06-01 08:24:47 -04:00
|
|
|
sender ! breaker.withCircuitBreaker(Future(dangerousCall))
|
2012-06-01 21:29:47 +02:00
|
|
|
case "block for me" ⇒
|
2012-06-01 08:24:47 -04:00
|
|
|
sender ! breaker.withSyncCircuitBreaker(dangerousCall)
|
|
|
|
|
}
|
2012-06-01 21:29:47 +02:00
|
|
|
//#circuit-breaker-usage
|
2012-06-01 08:24:47 -04:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|