2012-06-01 08:24:47 -04:00
|
|
|
/**
|
2013-01-09 01:47:48 +01:00
|
|
|
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
2012-06-01 08:24:47 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package docs.circuitbreaker
|
|
|
|
|
|
|
|
|
|
//#imports1
|
2012-09-21 14:50:06 +02:00
|
|
|
import scala.concurrent.duration._
|
2012-06-01 08:24:47 -04:00
|
|
|
import akka.pattern.CircuitBreaker
|
2012-10-03 13:30:00 +02:00
|
|
|
import akka.pattern.pipe
|
2012-06-01 08:24:47 -04:00
|
|
|
import akka.actor.Actor
|
2012-10-03 13:30:00 +02:00
|
|
|
import akka.actor.ActorLogging
|
2012-07-04 15:25:30 +02:00
|
|
|
import scala.concurrent.Future
|
2012-06-01 08:24:47 -04:00
|
|
|
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
|
2012-10-03 13:30:00 +02:00
|
|
|
class DangerousActor extends Actor with ActorLogging {
|
|
|
|
|
import context.dispatcher
|
2012-06-01 08:24:47 -04:00
|
|
|
|
|
|
|
|
val breaker =
|
2012-10-03 13:30:00 +02:00
|
|
|
new CircuitBreaker(context.system.scheduler,
|
|
|
|
|
maxFailures = 5,
|
|
|
|
|
callTimeout = 10.seconds,
|
2013-03-28 23:45:48 +01:00
|
|
|
resetTimeout = 1.minute).onOpen(notifyMeOnOpen())
|
2012-06-01 08:24:47 -04:00
|
|
|
|
2013-03-28 23:45:48 +01:00
|
|
|
def notifyMeOnOpen(): Unit =
|
2012-06-01 08:24:47 -04:00
|
|
|
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-10-03 13:30:00 +02:00
|
|
|
breaker.withCircuitBreaker(Future(dangerousCall)) pipeTo sender
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|