System wide materializer (#27491)

Introduces a materializer started through an extension and then an implicit
conversion for Scala turning an implicitly available ActorSystem into a
materializer. The Java APIs has been ammended with run-methods accepting
an ActorSystem.
This commit is contained in:
Johan Andrén 2019-08-23 18:19:27 +02:00 committed by GitHub
parent 77d1d33dbc
commit 45c826a218
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
196 changed files with 1148 additions and 1129 deletions

View file

@ -7,8 +7,6 @@ package jdocs.stream.operators;
import akka.NotUsed;
import akka.actor.ActorSystem;
import akka.stream.ActorMaterializer;
import akka.stream.Materializer;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;
// #takeLast-operator-example
@ -22,13 +20,12 @@ import java.util.concurrent.TimeoutException;
public class SinkDocExamples {
private static final ActorSystem system = ActorSystem.create("SourceFromExample");
private static final Materializer materializer = ActorMaterializer.create(system);
static void reduceExample() throws InterruptedException, ExecutionException, TimeoutException {
// #reduce-operator-example
Source<Integer, NotUsed> ints = Source.from(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
CompletionStage<Integer> sum = ints.runWith(Sink.reduce((a, b) -> a + b), materializer);
CompletionStage<Integer> sum = ints.runWith(Sink.reduce((a, b) -> a + b), system);
sum.thenAccept(System.out::println);
// 55
// #reduce-operator-example
@ -48,7 +45,7 @@ public class SinkDocExamples {
Source<Pair, NotUsed> studentSource = Source.from(sortedStudents);
CompletionStage<List<Pair>> topThree = studentSource.runWith(Sink.takeLast(3), materializer);
CompletionStage<List<Pair>> topThree = studentSource.runWith(Sink.takeLast(3), system);
topThree.thenAccept(
result -> {
@ -70,7 +67,7 @@ public class SinkDocExamples {
static void lastExample() throws InterruptedException, ExecutionException, TimeoutException {
// #last-operator-example
Source<Integer, NotUsed> source = Source.from(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
CompletionStage<Integer> result = source.runWith(Sink.last(), materializer);
CompletionStage<Integer> result = source.runWith(Sink.last(), system);
result.thenAccept(System.out::println);
// 10
// #last-operator-example
@ -80,7 +77,7 @@ public class SinkDocExamples {
throws InterruptedException, ExecutionException, TimeoutException {
// #lastOption-operator-example
Source<Integer, NotUsed> source = Source.empty();
CompletionStage<Optional<Integer>> result = source.runWith(Sink.lastOption(), materializer);
CompletionStage<Optional<Integer>> result = source.runWith(Sink.lastOption(), system);
result.thenAccept(System.out::println);
// Optional.empty
// #lastOption-operator-example

View file

@ -10,8 +10,6 @@ import akka.NotUsed;
import akka.actor.ActorSystem;
import akka.actor.testkit.typed.javadsl.ManualTime;
import akka.actor.testkit.typed.javadsl.TestKitJunitResource;
import akka.stream.ActorMaterializer;
import akka.stream.Materializer;
import akka.stream.javadsl.Source;
// #range-imports
@ -35,23 +33,21 @@ public class SourceDocExamples {
public static void fromExample() {
// #source-from-example
final ActorSystem system = ActorSystem.create("SourceFromExample");
final Materializer materializer = ActorMaterializer.create(system);
Source<Integer, NotUsed> ints = Source.from(Arrays.asList(0, 1, 2, 3, 4, 5));
ints.runForeach(System.out::println, materializer);
ints.runForeach(System.out::println, system);
String text =
"Perfection is finally attained not when there is no longer more to add,"
+ "but when there is no longer anything to take away.";
Source<String, NotUsed> words = Source.from(Arrays.asList(text.split("\\s")));
words.runForeach(System.out::println, materializer);
words.runForeach(System.out::println, system);
// #source-from-example
}
static void rangeExample() {
final ActorSystem system = ActorSystem.create("Source");
final Materializer materializer = ActorMaterializer.create(system);
// #range
@ -69,7 +65,7 @@ public class SourceDocExamples {
// #range
// #run-range
source.runForeach(i -> System.out.println(i), materializer);
source.runForeach(i -> System.out.println(i), system);
// #run-range
}
@ -77,12 +73,11 @@ public class SourceDocExamples {
// #actor-ref
final ActorSystem system = ActorSystem.create();
final Materializer materializer = ActorMaterializer.create(system);
int bufferSize = 100;
Source<Object, ActorRef> source = Source.actorRef(bufferSize, OverflowStrategy.dropHead());
ActorRef actorRef = source.to(Sink.foreach(System.out::println)).run(materializer);
ActorRef actorRef = source.to(Sink.foreach(System.out::println)).run(system);
actorRef.tell("hello", ActorRef.noSender());
actorRef.tell("hello", ActorRef.noSender());
@ -96,11 +91,10 @@ public class SourceDocExamples {
// #actor-ref-with-ack
final ActorSystem system = ActorSystem.create();
final Materializer materializer = ActorMaterializer.create(system);
Source<Object, ActorRef> source = Source.actorRefWithAck("ack");
ActorRef actorRef = source.to(Sink.foreach(System.out::println)).run(materializer);
ActorRef actorRef = source.to(Sink.foreach(System.out::println)).run(system);
probe.send(actorRef, "hello");
probe.expectMsg("ack");
probe.send(actorRef, "hello");