Add source.completionStage example #25468 (#28338)

This commit is contained in:
Arnout Engelen 2020-01-15 15:04:15 +01:00 committed by Johan Andrén
parent a8086e86e5
commit 087ed4b5a5
3 changed files with 42 additions and 1 deletions

View file

@ -30,3 +30,9 @@ For the corresponding operator for the Scala standard library `Future` see @ref:
@@@
## Example
Java
: @@snip [SourceFromCompletionStage.java](/akka-docs/src/test/java/jdocs/stream/operators/source/FromCompletionStage.java) { #sourceFromCompletionStage }
For the corresponding operator for the Scala standard library `Future` see @ref:[future](future.md).

View file

@ -1,4 +1,4 @@
# fromFuture
# future
Send the single value of the `Future` when it completes and there is demand.
@ -20,6 +20,7 @@ If the future fails the stream is failed with that exception.
For the corresponding operator for the Java standard library `CompletionStage` see @ref:[completionStage](completionStage.md).
## Example
Scala
: @@snip [SourceFromFuture.scala](/akka-docs/src/test/scala/docs/stream/operators/SourceOperators.scala) { #sourceFromFuture }

View file

@ -0,0 +1,34 @@
/*
* Copyright (C) 2018-2020 Lightbend Inc. <https://www.lightbend.com>
*/
package jdocs.stream.operators.source;
// #sourceFromCompletionStage
import java.util.concurrent.CompletionStage;
import java.util.concurrent.CompletableFuture;
import akka.NotUsed;
import akka.Done;
import akka.actor.typed.ActorSystem;
import akka.stream.javadsl.*;
// #sourceFromCompletionStage
class FromCompletionStage {
public static void sourceFromCompletionStage() {
// Use one ActorSystem per application
ActorSystem system = null;
// #sourceFromCompletionStage
CompletionStage<Integer> stage = CompletableFuture.completedFuture(10);
Source<Integer, NotUsed> source = Source.completionStage(stage);
Sink<Integer, CompletionStage<Done>> sink = Sink.foreach(i -> System.out.println(i.toString()));
source.runWith(sink, system); // 10
// #sourceFromCompletionStage
}
}