rename akka-docs dir to docs (#62)
This commit is contained in:
parent
13dce0ec69
commit
708da8caec
1029 changed files with 2033 additions and 2039 deletions
|
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.circuitbreaker;
|
||||
|
||||
// #imports1
|
||||
|
||||
import org.apache.pekko.actor.AbstractActor;
|
||||
import org.apache.pekko.event.LoggingAdapter;
|
||||
import java.time.Duration;
|
||||
import org.apache.pekko.pattern.CircuitBreaker;
|
||||
import org.apache.pekko.event.Logging;
|
||||
|
||||
import static org.apache.pekko.pattern.Patterns.pipe;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
// #imports1
|
||||
|
||||
// #circuit-breaker-initialization
|
||||
public class DangerousJavaActor extends AbstractActor {
|
||||
|
||||
private final CircuitBreaker breaker;
|
||||
private final LoggingAdapter log = Logging.getLogger(getContext().system(), this);
|
||||
|
||||
public DangerousJavaActor() {
|
||||
this.breaker =
|
||||
new CircuitBreaker(
|
||||
getContext().getDispatcher(),
|
||||
getContext().getSystem().getScheduler(),
|
||||
5,
|
||||
Duration.ofSeconds(10),
|
||||
Duration.ofMinutes(1))
|
||||
.addOnOpenListener(this::notifyMeOnOpen);
|
||||
}
|
||||
|
||||
public void notifyMeOnOpen() {
|
||||
log.warning("My CircuitBreaker is now open, and will not close for one minute");
|
||||
}
|
||||
// #circuit-breaker-initialization
|
||||
|
||||
// #circuit-breaker-usage
|
||||
public String dangerousCall() {
|
||||
return "This really isn't that dangerous of a call after all";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(
|
||||
String.class,
|
||||
"is my middle name"::equals,
|
||||
m ->
|
||||
pipe(
|
||||
breaker.callWithCircuitBreakerCS(
|
||||
() -> CompletableFuture.supplyAsync(this::dangerousCall)),
|
||||
getContext().getDispatcher())
|
||||
.to(sender()))
|
||||
.match(
|
||||
String.class,
|
||||
"block for me"::equals,
|
||||
m -> {
|
||||
sender().tell(breaker.callWithSyncCircuitBreaker(this::dangerousCall), self());
|
||||
})
|
||||
.build();
|
||||
}
|
||||
// #circuit-breaker-usage
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright (C) 2018-2022 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.circuitbreaker;
|
||||
|
||||
import org.apache.pekko.actor.AbstractActor;
|
||||
import org.apache.pekko.pattern.CircuitBreaker;
|
||||
import java.time.Duration;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class EvenNoFailureJavaExample extends AbstractActor {
|
||||
// #even-no-as-failure
|
||||
private final CircuitBreaker breaker;
|
||||
|
||||
public EvenNoFailureJavaExample() {
|
||||
this.breaker =
|
||||
new CircuitBreaker(
|
||||
getContext().getDispatcher(),
|
||||
getContext().getSystem().getScheduler(),
|
||||
5,
|
||||
Duration.ofSeconds(10),
|
||||
Duration.ofMinutes(1));
|
||||
}
|
||||
|
||||
public int luckyNumber() {
|
||||
BiFunction<Optional<Integer>, Optional<Throwable>, Boolean> evenNoAsFailure =
|
||||
(result, err) -> (result.isPresent() && result.get() % 2 == 0);
|
||||
|
||||
// this will return 8888 and increase failure count at the same time
|
||||
return this.breaker.callWithSyncCircuitBreaker(() -> 8888, evenNoAsFailure);
|
||||
}
|
||||
|
||||
// #even-no-as-failure
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.circuitbreaker;
|
||||
|
||||
import org.apache.pekko.actor.ActorRef;
|
||||
import org.apache.pekko.actor.ReceiveTimeout;
|
||||
import org.apache.pekko.actor.AbstractActor;
|
||||
import org.apache.pekko.event.Logging;
|
||||
import org.apache.pekko.event.LoggingAdapter;
|
||||
import org.apache.pekko.pattern.CircuitBreaker;
|
||||
import java.time.Duration;
|
||||
|
||||
public class TellPatternJavaActor extends AbstractActor {
|
||||
|
||||
private final ActorRef target;
|
||||
private final CircuitBreaker breaker;
|
||||
private final LoggingAdapter log = Logging.getLogger(getContext().system(), this);
|
||||
|
||||
public TellPatternJavaActor(ActorRef targetActor) {
|
||||
this.target = targetActor;
|
||||
this.breaker =
|
||||
new CircuitBreaker(
|
||||
getContext().getDispatcher(),
|
||||
getContext().getSystem().getScheduler(),
|
||||
5,
|
||||
Duration.ofSeconds(10),
|
||||
Duration.ofMinutes(1))
|
||||
.addOnOpenListener(this::notifyMeOnOpen);
|
||||
}
|
||||
|
||||
public void notifyMeOnOpen() {
|
||||
log.warning("My CircuitBreaker is now open, and will not close for one minute");
|
||||
}
|
||||
|
||||
// #circuit-breaker-tell-pattern
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(
|
||||
String.class,
|
||||
payload -> "call".equals(payload) && breaker.isClosed(),
|
||||
payload -> target.tell("message", self()))
|
||||
.matchEquals("response", payload -> breaker.succeed())
|
||||
.match(Throwable.class, t -> breaker.fail())
|
||||
.match(ReceiveTimeout.class, t -> breaker.fail())
|
||||
.build();
|
||||
}
|
||||
// #circuit-breaker-tell-pattern
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue