Adding example of cancelled operator (#29749)
Co-authored-by: Arnout Engelen <github@bzzt.net> Co-authored-by: Renato Cavalcanti <renato@cavalcanti.be>
This commit is contained in:
parent
9c200e3a04
commit
432d94d5a4
3 changed files with 40 additions and 2 deletions
|
|
@ -13,6 +13,16 @@ Immediately cancel the stream
|
|||
|
||||
Immediately cancel the stream
|
||||
|
||||
## Example
|
||||
|
||||
In this example, we have a source that generates numbers from 1 to 5 but as we have used cancelled we get `NotUsed` as materialized value and stream cancels.
|
||||
|
||||
Scala
|
||||
: @@snip [Cancelled.scala](/akka-docs/src/test/scala/docs/stream/operators/sink/Cancelled.scala) { #cancelled }
|
||||
|
||||
Java
|
||||
: @@snip [SinkDocExamples.java](/akka-docs/src/test/java/jdocs/stream/operators/SinkDocExamples.java) { #cancelled }
|
||||
|
||||
## Reactive Streams semantics
|
||||
|
||||
@@@div { .callout }
|
||||
|
|
@ -20,5 +30,3 @@ Immediately cancel the stream
|
|||
**cancels** immediately
|
||||
|
||||
@@@
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -114,6 +114,14 @@ public class SinkDocExamples {
|
|||
// #fold
|
||||
}
|
||||
|
||||
static NotUsed cancelledExample() {
|
||||
// #cancelled
|
||||
Source<Integer, NotUsed> source = Source.range(1, 5);
|
||||
NotUsed sum = source.runWith(Sink.cancelled(), system);
|
||||
return sum;
|
||||
// #cancelled
|
||||
}
|
||||
|
||||
static void headOptionExample() {
|
||||
// #headoption
|
||||
Source<Integer, NotUsed> source = Source.empty();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* Copyright (C) 2020 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package docs.stream.operators.sink
|
||||
|
||||
import akka.NotUsed
|
||||
import akka.actor.ActorSystem
|
||||
import akka.stream.scaladsl.{ Sink, Source }
|
||||
|
||||
import scala.concurrent.ExecutionContextExecutor
|
||||
|
||||
object Cancelled {
|
||||
implicit val system: ActorSystem = ???
|
||||
implicit val ec: ExecutionContextExecutor = system.dispatcher
|
||||
def cancelledExample(): NotUsed = {
|
||||
//#cancelled
|
||||
val source = Source(1 to 5)
|
||||
source.runWith(Sink.cancelled)
|
||||
//#cancelled
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue