2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2020-01-02 07:24:59 -05:00
|
|
|
* Copyright (C) 2018-2020 Lightbend Inc. <https://www.lightbend.com>
|
2018-07-19 06:38:47 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package jdocs.stream.operators;
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #imports
|
|
|
|
|
// #range-imports
|
2020-03-12 17:24:31 +01:00
|
|
|
import akka.Done;
|
2018-07-19 06:38:47 +02:00
|
|
|
import akka.NotUsed;
|
|
|
|
|
import akka.actor.ActorSystem;
|
2019-05-20 12:19:44 +02:00
|
|
|
import akka.actor.testkit.typed.javadsl.ManualTime;
|
|
|
|
|
import akka.actor.testkit.typed.javadsl.TestKitJunitResource;
|
2018-07-19 06:38:47 +02:00
|
|
|
import akka.stream.javadsl.Source;
|
2019-01-12 04:00:53 +08:00
|
|
|
// #range-imports
|
2018-07-19 06:38:47 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #actor-ref-imports
|
2019-01-02 17:08:35 +01:00
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.Status.Success;
|
|
|
|
|
import akka.stream.OverflowStrategy;
|
2019-05-20 12:19:44 +02:00
|
|
|
import akka.stream.CompletionStrategy;
|
2019-01-02 17:08:35 +01:00
|
|
|
import akka.stream.javadsl.Sink;
|
2019-05-20 12:19:44 +02:00
|
|
|
import akka.testkit.TestProbe;
|
2019-01-12 04:00:53 +08:00
|
|
|
// #actor-ref-imports
|
2019-01-02 17:08:35 +01:00
|
|
|
|
2019-10-03 14:09:45 +02:00
|
|
|
// #maybe
|
|
|
|
|
import akka.stream.javadsl.RunnableGraph;
|
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
// #maybe
|
|
|
|
|
|
2018-07-19 06:38:47 +02:00
|
|
|
import java.util.Arrays;
|
2019-09-10 11:59:19 +02:00
|
|
|
import java.util.Optional;
|
2018-07-19 06:38:47 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #imports
|
2018-07-19 06:38:47 +02:00
|
|
|
|
|
|
|
|
public class SourceDocExamples {
|
|
|
|
|
|
2019-05-20 12:19:44 +02:00
|
|
|
public static final TestKitJunitResource testKit = new TestKitJunitResource(ManualTime.config());
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
public static void fromExample() {
|
2019-10-03 14:09:45 +02:00
|
|
|
final ActorSystem system = null;
|
2018-07-19 06:38:47 +02:00
|
|
|
|
2019-10-03 14:09:45 +02:00
|
|
|
// #source-from-example
|
2019-01-12 04:00:53 +08:00
|
|
|
Source<Integer, NotUsed> ints = Source.from(Arrays.asList(0, 1, 2, 3, 4, 5));
|
2019-08-23 18:19:27 +02:00
|
|
|
ints.runForeach(System.out::println, system);
|
2018-07-19 06:38:47 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
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")));
|
2019-08-23 18:19:27 +02:00
|
|
|
words.runForeach(System.out::println, system);
|
2019-01-12 04:00:53 +08:00
|
|
|
// #source-from-example
|
|
|
|
|
}
|
2018-07-19 06:38:47 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
static void rangeExample() {
|
2018-07-27 17:51:00 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
final ActorSystem system = ActorSystem.create("Source");
|
2018-07-27 17:51:00 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #range
|
2018-07-27 17:51:00 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
Source<Integer, NotUsed> source = Source.range(1, 100);
|
2018-07-27 17:51:00 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #range
|
2018-07-27 17:51:00 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #range
|
|
|
|
|
Source<Integer, NotUsed> sourceStepFive = Source.range(1, 100, 5);
|
2018-07-27 17:51:00 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #range
|
2018-07-27 17:51:00 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #range
|
|
|
|
|
Source<Integer, NotUsed> sourceStepNegative = Source.range(100, 1, -1);
|
|
|
|
|
// #range
|
2018-07-27 17:51:00 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #run-range
|
2019-08-23 18:19:27 +02:00
|
|
|
source.runForeach(i -> System.out.println(i), system);
|
2019-01-12 04:00:53 +08:00
|
|
|
// #run-range
|
|
|
|
|
}
|
2018-07-27 17:51:00 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
static void actorRef() {
|
2019-10-03 14:09:45 +02:00
|
|
|
final ActorSystem system = null;
|
2019-01-02 17:08:35 +01:00
|
|
|
|
2019-10-03 14:09:45 +02:00
|
|
|
// #actor-ref
|
2019-01-02 17:08:35 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
int bufferSize = 100;
|
2020-03-12 17:24:31 +01:00
|
|
|
Source<Object, ActorRef> source =
|
|
|
|
|
Source.actorRef(
|
|
|
|
|
elem -> {
|
|
|
|
|
// complete stream immediately if we send it Done
|
|
|
|
|
if (elem == Done.done()) return Optional.of(CompletionStrategy.immediately());
|
|
|
|
|
else return Optional.empty();
|
|
|
|
|
},
|
|
|
|
|
// never fail the stream because of a message
|
|
|
|
|
elem -> Optional.empty(),
|
|
|
|
|
bufferSize,
|
|
|
|
|
OverflowStrategy.dropHead());
|
2019-01-02 17:08:35 +01:00
|
|
|
|
2019-08-23 18:19:27 +02:00
|
|
|
ActorRef actorRef = source.to(Sink.foreach(System.out::println)).run(system);
|
2019-01-12 04:00:53 +08:00
|
|
|
actorRef.tell("hello", ActorRef.noSender());
|
|
|
|
|
actorRef.tell("hello", ActorRef.noSender());
|
2019-01-02 17:08:35 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// The stream completes successfully with the following message
|
2019-03-27 14:16:38 +01:00
|
|
|
actorRef.tell(new Success(CompletionStrategy.draining()), ActorRef.noSender());
|
2019-01-12 04:00:53 +08:00
|
|
|
// #actor-ref
|
|
|
|
|
}
|
2019-05-20 12:19:44 +02:00
|
|
|
|
2019-09-10 11:59:19 +02:00
|
|
|
static void actorRefWithBackpressure() {
|
2019-05-20 12:19:44 +02:00
|
|
|
final TestProbe probe = null;
|
2019-10-03 14:09:45 +02:00
|
|
|
final ActorSystem system = null;
|
2019-05-20 12:19:44 +02:00
|
|
|
|
2019-09-10 11:59:19 +02:00
|
|
|
// #actorRefWithBackpressure
|
|
|
|
|
Source<Object, ActorRef> source =
|
|
|
|
|
Source.actorRefWithBackpressure(
|
|
|
|
|
"ack",
|
|
|
|
|
o -> {
|
|
|
|
|
if (o == "complete") return Optional.of(CompletionStrategy.draining());
|
|
|
|
|
else return Optional.empty();
|
|
|
|
|
},
|
|
|
|
|
o -> Optional.empty());
|
2019-05-20 12:19:44 +02:00
|
|
|
|
2019-08-23 18:19:27 +02:00
|
|
|
ActorRef actorRef = source.to(Sink.foreach(System.out::println)).run(system);
|
2019-05-20 12:19:44 +02:00
|
|
|
probe.send(actorRef, "hello");
|
|
|
|
|
probe.expectMsg("ack");
|
|
|
|
|
probe.send(actorRef, "hello");
|
|
|
|
|
probe.expectMsg("ack");
|
|
|
|
|
|
|
|
|
|
// The stream completes successfully with the following message
|
2019-09-10 11:59:19 +02:00
|
|
|
actorRef.tell("complete", ActorRef.noSender());
|
|
|
|
|
// #actorRefWithBackpressure
|
2019-05-20 12:19:44 +02:00
|
|
|
}
|
2019-10-03 14:09:45 +02:00
|
|
|
|
|
|
|
|
static void maybeExample() {
|
|
|
|
|
final ActorSystem system = null;
|
|
|
|
|
|
|
|
|
|
// #maybe
|
|
|
|
|
Source<Integer, CompletableFuture<Optional<Integer>>> source = Source.<Integer>maybe();
|
|
|
|
|
RunnableGraph<CompletableFuture<Optional<Integer>>> runnable =
|
|
|
|
|
source.to(Sink.foreach(System.out::println));
|
|
|
|
|
|
|
|
|
|
CompletableFuture<Optional<Integer>> completable1 = runnable.run(system);
|
|
|
|
|
completable1.complete(Optional.of(1)); // prints 1
|
|
|
|
|
|
|
|
|
|
CompletableFuture<Optional<Integer>> completable2 = runnable.run(system);
|
|
|
|
|
completable2.complete(Optional.of(2)); // prints 2
|
|
|
|
|
// #maybe
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
|
// #maybe-signature
|
|
|
|
|
<Out> Source<Out, CompletableFuture<Optional<Out>>> maybe()
|
|
|
|
|
// #maybe-signature
|
|
|
|
|
{
|
|
|
|
|
return Source.maybe();
|
|
|
|
|
}
|
2018-07-19 06:38:47 +02:00
|
|
|
}
|