pekko/docs/src/test/java/jdocs/stream/RestartDocTest.java

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

113 lines
3.4 KiB
Java
Raw Normal View History

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* license agreements; and to You under the Apache License, version 2.0:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
2023-06-22 14:19:26 +01:00
* This file is part of the Apache Pekko project, which was derived from Akka.
*/
/*
2022-02-04 12:36:44 +01:00
* Copyright (C) 2015-2022 Lightbend Inc. <https://www.lightbend.com>
2017-07-20 23:02:34 +10:00
*/
2017-07-20 23:02:34 +10:00
package jdocs.stream;
import org.apache.pekko.NotUsed;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.stream.KillSwitch;
import org.apache.pekko.stream.KillSwitches;
import org.apache.pekko.stream.Materializer;
import org.apache.pekko.stream.RestartSettings;
import org.apache.pekko.stream.javadsl.Keep;
import org.apache.pekko.stream.javadsl.RestartSource;
import org.apache.pekko.stream.javadsl.Sink;
import org.apache.pekko.stream.javadsl.Source;
2017-07-20 23:02:34 +10:00
import java.time.Duration;
2017-07-20 23:02:34 +10:00
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
public class RestartDocTest {
static ActorSystem system;
static Materializer materializer;
// Mocking pekko-http
2017-07-20 23:02:34 +10:00
public static class Http {
public static Http get(ActorSystem system) {
return new Http();
}
2017-07-20 23:02:34 +10:00
public CompletionStage<Http> singleRequest(String uri) {
return new CompletableFuture<>();
}
2017-07-20 23:02:34 +10:00
public NotUsed entity() {
return NotUsed.getInstance();
}
}
2017-07-20 23:02:34 +10:00
public static class HttpRequest {
public static String create(String uri) {
return uri;
}
}
2017-07-20 23:02:34 +10:00
public static class ServerSentEvent {}
2017-07-20 23:02:34 +10:00
public static class EventStreamUnmarshalling {
public static EventStreamUnmarshalling fromEventStream() {
return new EventStreamUnmarshalling();
}
2017-07-20 23:02:34 +10:00
public CompletionStage<Source<ServerSentEvent, NotUsed>> unmarshall(
Http http, Materializer mat) {
return new CompletableFuture<>();
}
}
public void doSomethingElse() {}
public void recoverWithBackoffSource() {
// #restart-with-backoff-source
RestartSettings settings =
RestartSettings.create(
Duration.ofSeconds(3), // min backoff
Duration.ofSeconds(30), // max backoff
0.2 // adds 20% "noise" to vary the intervals slightly
)
.withMaxRestarts(
20, Duration.ofMinutes(5)); // limits the amount of restarts to 20 within 5 minutes
2017-07-20 23:02:34 +10:00
Source<ServerSentEvent, NotUsed> eventStream =
RestartSource.withBackoff(
settings,
2017-07-20 23:02:34 +10:00
() ->
// Create a source from a future of a source
Source.completionStageSource(
2017-07-20 23:02:34 +10:00
// Issue a GET request on the event stream
Http.get(system)
.singleRequest(HttpRequest.create("http://example.com/eventstream"))
.thenCompose(
response ->
// Unmarshall it to a stream of ServerSentEvents
EventStreamUnmarshalling.fromEventStream()
.unmarshall(response, materializer))));
// #restart-with-backoff-source
2017-07-20 23:02:34 +10:00
// #with-kill-switch
KillSwitch killSwitch =
eventStream
.viaMat(KillSwitches.single(), Keep.right())
.toMat(Sink.foreach(event -> System.out.println("Got event: " + event)), Keep.left())
.run(materializer);
doSomethingElse();
killSwitch.shutdown();
// #with-kill-switch
}
}