2012-06-01 08:24:47 -04:00
|
|
|
/**
|
2017-01-04 17:37:10 +01:00
|
|
|
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
|
2012-06-01 08:24:47 -04:00
|
|
|
*/
|
|
|
|
|
package docs.circuitbreaker;
|
|
|
|
|
|
|
|
|
|
//#imports1
|
|
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
import akka.actor.AbstractActor;
|
2012-07-04 15:25:30 +02:00
|
|
|
import scala.concurrent.Future;
|
2012-06-01 08:24:47 -04:00
|
|
|
import akka.event.LoggingAdapter;
|
2012-10-15 16:18:52 +02:00
|
|
|
import scala.concurrent.duration.Duration;
|
2012-06-01 08:24:47 -04:00
|
|
|
import akka.pattern.CircuitBreaker;
|
|
|
|
|
import akka.event.Logging;
|
|
|
|
|
|
2012-10-03 13:30:00 +02:00
|
|
|
import static akka.pattern.Patterns.pipe;
|
2012-06-01 08:24:47 -04:00
|
|
|
import static akka.dispatch.Futures.future;
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
|
|
|
|
|
|
//#imports1
|
|
|
|
|
|
|
|
|
|
//#circuit-breaker-initialization
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
public class DangerousJavaActor extends AbstractActor {
|
2012-06-01 08:24:47 -04:00
|
|
|
|
2012-09-26 10:56:25 +02:00
|
|
|
private final CircuitBreaker breaker;
|
|
|
|
|
private final LoggingAdapter log = Logging.getLogger(getContext().system(), this);
|
2012-06-01 08:24:47 -04:00
|
|
|
|
2012-09-26 10:56:25 +02:00
|
|
|
public DangerousJavaActor() {
|
|
|
|
|
this.breaker = new CircuitBreaker(
|
|
|
|
|
getContext().dispatcher(), getContext().system().scheduler(),
|
|
|
|
|
5, Duration.create(10, "s"), Duration.create(1, "m"))
|
2012-10-16 14:38:23 +02:00
|
|
|
.onOpen(new Runnable() {
|
|
|
|
|
public void run() {
|
2012-09-26 10:56:25 +02:00
|
|
|
notifyMeOnOpen();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-06-01 08:24:47 -04:00
|
|
|
|
2012-09-26 10:56:25 +02:00
|
|
|
public void notifyMeOnOpen() {
|
|
|
|
|
log.warning("My CircuitBreaker is now open, and will not close for one minute");
|
|
|
|
|
}
|
2012-06-01 08:24:47 -04:00
|
|
|
//#circuit-breaker-initialization
|
|
|
|
|
|
2012-09-26 10:56:25 +02:00
|
|
|
//#circuit-breaker-usage
|
|
|
|
|
public String dangerousCall() {
|
|
|
|
|
return "This really isn't that dangerous of a call after all";
|
|
|
|
|
}
|
2012-06-01 08:24:47 -04:00
|
|
|
|
2012-09-26 10:56:25 +02:00
|
|
|
@Override
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder().
|
|
|
|
|
match(String.class, m -> "is my middle name".equals(m), m -> {
|
2016-07-14 14:03:04 +02:00
|
|
|
pipe(
|
|
|
|
|
breaker.callWithCircuitBreaker(() ->
|
|
|
|
|
future(() -> dangerousCall(), getContext().dispatcher())
|
|
|
|
|
), getContext().dispatcher()
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
).to(sender());
|
|
|
|
|
})
|
|
|
|
|
.match(String.class, m -> "block for me".equals(m), m -> {
|
|
|
|
|
sender().tell(breaker
|
2012-09-26 10:56:25 +02:00
|
|
|
.callWithSyncCircuitBreaker(
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
() -> dangerousCall()), self());
|
|
|
|
|
})
|
|
|
|
|
.build();
|
2012-09-26 10:56:25 +02:00
|
|
|
}
|
2012-06-01 08:24:47 -04:00
|
|
|
//#circuit-breaker-usage
|
|
|
|
|
|
2013-01-09 01:47:48 +01:00
|
|
|
}
|