Merge remote-tracking branch 'akka/master' into scalatest310
This commit is contained in:
commit
52c01832da
17 changed files with 321 additions and 28 deletions
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright (C) 2019-2020 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.stream.operators.sourceorflow;
|
||||
|
||||
import akka.NotUsed;
|
||||
import akka.actor.ActorSystem;
|
||||
import akka.stream.javadsl.Source;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class FlatMapMerge {
|
||||
private static ActorSystem system = null;
|
||||
|
||||
// #flatmap-merge
|
||||
// e.g. could be a query to a database
|
||||
private Source<String, NotUsed> lookupCustomerEvents(String customerId) {
|
||||
return Source.from(Arrays.asList(customerId + "-evt-1", customerId + "-evt-2"));
|
||||
}
|
||||
// #flatmap-merge
|
||||
|
||||
void example() {
|
||||
// #flatmap-merge
|
||||
Source.from(Arrays.asList("customer-1", "customer-2"))
|
||||
.flatMapMerge(10, this::lookupCustomerEvents)
|
||||
.runForeach(System.out::println, system);
|
||||
// prints - events from different customers could interleave
|
||||
// customer-1-evt-1
|
||||
// customer-2-evt-1
|
||||
// customer-1-evt-2
|
||||
// customer-2-evt-2
|
||||
// #flatmap-merge
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2020 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.stream.operators.sourceorflow;
|
||||
|
||||
import akka.NotUsed;
|
||||
import akka.actor.typed.ActorSystem;
|
||||
import akka.stream.javadsl.Source;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Sliding {
|
||||
|
||||
private final ActorSystem<Void> system = null;
|
||||
|
||||
public void slidingExample1() {
|
||||
// #sliding-1
|
||||
Source<Integer, NotUsed> source = Source.range(1, 4);
|
||||
source.sliding(2, 1).runForeach(n -> System.out.println(n), system);
|
||||
// prints:
|
||||
// [1, 2]
|
||||
// [2, 3]
|
||||
// [3, 4]
|
||||
// #sliding-1
|
||||
}
|
||||
|
||||
public void slidingExample2() {
|
||||
// #sliding-2
|
||||
Source<Integer, NotUsed> source = Source.range(1, 4);
|
||||
source.sliding(3, 2).runForeach(n -> System.out.println(n), system);
|
||||
// prints:
|
||||
// Vector(1, 2, 3)
|
||||
// [1, 2, 3]
|
||||
// [3, 4] - shorter because stream ended before we got 3 elements
|
||||
// #sliding-2
|
||||
}
|
||||
|
||||
public void slidingExample3() {
|
||||
// #moving-average
|
||||
Source<Integer, NotUsed> numbers = Source.from(Arrays.asList(1, 3, 10, 2, 3, 4, 2, 10, 11));
|
||||
Source<Float, NotUsed> movingAverage =
|
||||
numbers
|
||||
.sliding(5, 1)
|
||||
.map(window -> ((float) window.stream().mapToInt(i -> i).sum()) / window.size());
|
||||
movingAverage.runForeach(n -> System.out.println(n), system);
|
||||
// prints
|
||||
// 3.8 = average of 1, 3, 10, 2, 3
|
||||
// 4.4 = average of 3, 10, 2, 3, 4
|
||||
// 4.2 = average of 10, 2, 3, 4, 2
|
||||
// 4.2 = average of 2, 3, 4, 2, 10
|
||||
// 6.0 = average of 3, 4, 2, 10, 11
|
||||
// #moving-average
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue