2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2019-01-02 18:55:26 +08:00
|
|
|
* Copyright (C) 2016-2019 Lightbend Inc. <https://www.lightbend.com>
|
2016-02-16 16:56:06 +01:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.stream;
|
2016-02-16 16:56:06 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #stream-imports
|
2016-06-18 11:15:17 +02:00
|
|
|
import akka.stream.*;
|
|
|
|
|
import akka.stream.javadsl.*;
|
2019-01-12 04:00:53 +08:00
|
|
|
// #stream-imports
|
2016-06-18 11:15:17 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #other-imports
|
2016-06-18 11:15:17 +02:00
|
|
|
import akka.Done;
|
|
|
|
|
import akka.NotUsed;
|
|
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
import akka.util.ByteString;
|
|
|
|
|
|
2016-04-25 19:25:26 +10:00
|
|
|
import java.nio.file.Paths;
|
2016-02-16 16:56:06 +01:00
|
|
|
import java.math.BigInteger;
|
2018-03-19 22:14:33 +08:00
|
|
|
import java.time.Duration;
|
2016-02-16 16:56:06 +01:00
|
|
|
import java.util.concurrent.CompletionStage;
|
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
import jdocs.AbstractJavaTest;
|
2019-01-12 04:00:53 +08:00
|
|
|
// #other-imports
|
2016-06-18 11:15:17 +02:00
|
|
|
|
|
|
|
|
import org.junit.*;
|
2016-02-16 16:56:06 +01:00
|
|
|
|
|
|
|
|
/**
|
2019-01-12 04:00:53 +08:00
|
|
|
* This class is not meant to be run as a test in the test suite, but it is set up such that it can
|
|
|
|
|
* be run interactively from within an IDE.
|
2016-02-16 16:56:06 +01:00
|
|
|
*/
|
2017-03-02 11:55:03 +01:00
|
|
|
public class QuickStartDocTest extends AbstractJavaTest {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2016-02-16 16:56:06 +01:00
|
|
|
@Test
|
|
|
|
|
public void demonstrateSource() throws InterruptedException, ExecutionException {
|
2017-04-06 11:17:10 +02:00
|
|
|
final ActorSystem system = ActorSystem.create("QuickStart");
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #create-source
|
2016-02-16 16:56:06 +01:00
|
|
|
final Source<Integer, NotUsed> source = Source.range(1, 100);
|
2019-01-12 04:00:53 +08:00
|
|
|
// #create-source
|
|
|
|
|
|
|
|
|
|
// #run-source
|
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-source
|
|
|
|
|
|
|
|
|
|
// #transform-source
|
2016-02-16 16:56:06 +01:00
|
|
|
final Source<BigInteger, NotUsed> factorials =
|
2019-01-12 04:00:53 +08:00
|
|
|
source.scan(BigInteger.ONE, (acc, next) -> acc.multiply(BigInteger.valueOf(next)));
|
|
|
|
|
|
2016-02-16 16:56:06 +01:00
|
|
|
final CompletionStage<IOResult> result =
|
2019-01-12 04:00:53 +08:00
|
|
|
factorials
|
|
|
|
|
.map(num -> ByteString.fromString(num.toString() + "\n"))
|
2019-08-23 18:19:27 +02:00
|
|
|
.runWith(FileIO.toPath(Paths.get("factorials.txt")), system);
|
2019-01-12 04:00:53 +08:00
|
|
|
// #transform-source
|
2016-02-16 16:56:06 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #use-transformed-sink
|
2019-08-23 18:19:27 +02:00
|
|
|
factorials.map(BigInteger::toString).runWith(lineSink("factorial2.txt"), system);
|
2019-01-12 04:00:53 +08:00
|
|
|
// #use-transformed-sink
|
|
|
|
|
|
|
|
|
|
// #add-streams
|
2017-04-06 11:17:10 +02:00
|
|
|
factorials
|
2019-01-12 04:00:53 +08:00
|
|
|
.zipWith(Source.range(0, 99), (num, idx) -> String.format("%d! = %s", idx, num))
|
|
|
|
|
.throttle(1, Duration.ofSeconds(1))
|
|
|
|
|
// #add-streams
|
|
|
|
|
.take(2)
|
|
|
|
|
// #add-streams
|
2019-08-23 18:19:27 +02:00
|
|
|
.runForeach(s -> System.out.println(s), system);
|
2019-01-12 04:00:53 +08:00
|
|
|
// #add-streams
|
|
|
|
|
|
|
|
|
|
// #run-source-and-terminate
|
2019-08-23 18:19:27 +02:00
|
|
|
final CompletionStage<Done> done = source.runForeach(i -> System.out.println(i), system);
|
2017-04-06 11:17:10 +02:00
|
|
|
|
|
|
|
|
done.thenRun(() -> system.terminate());
|
2019-01-12 04:00:53 +08:00
|
|
|
// #run-source-and-terminate
|
2017-04-06 11:17:10 +02:00
|
|
|
|
2016-02-16 16:56:06 +01:00
|
|
|
done.toCompletableFuture().get();
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
|
|
|
|
// #transform-sink
|
2016-02-16 16:56:06 +01:00
|
|
|
public Sink<String, CompletionStage<IOResult>> lineSink(String filename) {
|
|
|
|
|
return Flow.of(String.class)
|
2019-01-12 04:00:53 +08:00
|
|
|
.map(s -> ByteString.fromString(s.toString() + "\n"))
|
|
|
|
|
.toMat(FileIO.toPath(Paths.get(filename)), Keep.right());
|
2016-02-16 16:56:06 +01:00
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
// #transform-sink
|
|
|
|
|
|
2016-02-16 16:56:06 +01:00
|
|
|
}
|