add new Streams Quick Start
also move IOResult to akka.stream package
This commit is contained in:
parent
5d3a8256c1
commit
125c996fae
23 changed files with 459 additions and 26 deletions
88
akka-docs/rst/java/code/docs/stream/QuickStartDocTest.java
Normal file
88
akka-docs/rst/java/code/docs/stream/QuickStartDocTest.java
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
/**
|
||||
* Copyright (C) 2016 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
package docs.stream;
|
||||
|
||||
import java.io.File;
|
||||
import java.math.BigInteger;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
import akka.Done;
|
||||
import akka.NotUsed;
|
||||
import akka.actor.ActorSystem;
|
||||
//#imports
|
||||
import akka.stream.*;
|
||||
import akka.stream.javadsl.*;
|
||||
//#imports
|
||||
import akka.util.ByteString;
|
||||
import scala.concurrent.duration.Duration;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public class QuickStartDocTest {
|
||||
|
||||
static
|
||||
//#create-materializer
|
||||
final ActorSystem system = ActorSystem.create("QuickStart");
|
||||
final Materializer materializer = ActorMaterializer.create(system);
|
||||
//#create-materializer
|
||||
|
||||
@AfterClass
|
||||
public static void teardown() {
|
||||
system.terminate();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void demonstrateSource() throws InterruptedException, ExecutionException {
|
||||
//#create-source
|
||||
final Source<Integer, NotUsed> source = Source.range(1, 100);
|
||||
//#create-source
|
||||
|
||||
//#run-source
|
||||
source.runForeach(i -> System.out.println(i), materializer);
|
||||
//#run-source
|
||||
|
||||
//#transform-source
|
||||
final Source<BigInteger, NotUsed> factorials =
|
||||
source
|
||||
.scan(BigInteger.ONE, (acc, next) -> acc.multiply(BigInteger.valueOf(next)));
|
||||
|
||||
final CompletionStage<IOResult> result =
|
||||
factorials
|
||||
.map(num -> ByteString.fromString(num.toString() + "\n"))
|
||||
.runWith(FileIO.toFile(new File("factorials.txt")), materializer);
|
||||
//#transform-source
|
||||
|
||||
//#use-transformed-sink
|
||||
factorials.map(BigInteger::toString).runWith(lineSink("factorial2.txt"), materializer);
|
||||
//#use-transformed-sink
|
||||
|
||||
//#add-streams
|
||||
final CompletionStage<Done> done =
|
||||
factorials
|
||||
.zipWith(Source.range(0, 99), (num, idx) -> String.format("%d! = %s", idx, num))
|
||||
.throttle(1, Duration.create(1, TimeUnit.SECONDS), 1, ThrottleMode.shaping())
|
||||
//#add-streams
|
||||
.take(2)
|
||||
//#add-streams
|
||||
.runForeach(s -> System.out.println(s), materializer);
|
||||
//#add-streams
|
||||
|
||||
done.toCompletableFuture().get();
|
||||
}
|
||||
|
||||
//#transform-sink
|
||||
public Sink<String, CompletionStage<IOResult>> lineSink(String filename) {
|
||||
return Flow.of(String.class)
|
||||
.map(s -> ByteString.fromString(s.toString() + "\n"))
|
||||
.toMat(FileIO.toFile(new File(filename)), Keep.right());
|
||||
}
|
||||
//#transform-sink
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue