Skip to content

Commit 71ddfee

Browse files
tomekl007KevinGilmore
authored andcommitted
BAEL-936 code for akka_streams article (eugenp#1939)
* BAEL-936 code for akka_streams article * rename
1 parent c17d19f commit 71ddfee

4 files changed

Lines changed: 161 additions & 0 deletions

File tree

‎akka-streams/pom.xml‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>parent-modules</artifactId>
7+
<groupId>com.baeldung</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<artifactId>akka-streams</artifactId>
12+
<name>akka-streams</name>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.typesafe.akka</groupId>
17+
<artifactId>akka-stream_2.11</artifactId>
18+
<version>${akkastreams.version}</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.typesafe.akka</groupId>
22+
<artifactId>akka-stream-testkit_2.11</artifactId>
23+
<version>${akkastreams.version}</version>
24+
</dependency>
25+
</dependencies>
26+
<properties>
27+
<akkastreams.version>2.5.2</akkastreams.version>
28+
</properties>
29+
30+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.akkastreams;
2+
3+
4+
import java.util.concurrent.CompletableFuture;
5+
import java.util.concurrent.CompletionStage;
6+
7+
public class AverageRepository {
8+
CompletionStage<Double> save(Double average) {
9+
return CompletableFuture.supplyAsync(() -> {
10+
System.out.println("saving average: " + average);
11+
return average;
12+
});
13+
}
14+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.baeldung.akkastreams;
2+
3+
4+
import akka.Done;
5+
import akka.NotUsed;
6+
import akka.actor.ActorSystem;
7+
import akka.stream.ActorMaterializer;
8+
import akka.stream.javadsl.Flow;
9+
import akka.stream.javadsl.Keep;
10+
import akka.stream.javadsl.Sink;
11+
import akka.stream.javadsl.Source;
12+
13+
import java.util.Arrays;
14+
import java.util.List;
15+
import java.util.concurrent.CompletableFuture;
16+
import java.util.concurrent.CompletionStage;
17+
import java.util.stream.Collectors;
18+
19+
public class DataImporter {
20+
private final ActorSystem actorSystem;
21+
private final AverageRepository averageRepository = new AverageRepository();
22+
23+
public DataImporter(ActorSystem actorSystem) {
24+
this.actorSystem = actorSystem;
25+
26+
}
27+
28+
private List<Integer> parseLine(String line) {
29+
String[] fields = line.split(";");
30+
return Arrays.stream(fields)
31+
.map(Integer::parseInt)
32+
.collect(Collectors.toList());
33+
}
34+
35+
private Flow<String, Integer, NotUsed> parseContent() {
36+
return Flow.of(String.class).mapConcat(this::parseLine);
37+
}
38+
39+
private Flow<Integer, Double, NotUsed> computeAverage() {
40+
return Flow.of(Integer.class).grouped(2).mapAsyncUnordered(8, integers ->
41+
CompletableFuture.supplyAsync(() -> integers
42+
.stream()
43+
.mapToDouble(v -> v)
44+
.average()
45+
.orElse(-1.0)));
46+
}
47+
48+
Flow<String, Double, NotUsed> calculateAverage() {
49+
return Flow.of(String.class)
50+
.via(parseContent())
51+
.via(computeAverage());
52+
}
53+
54+
private Sink<Double, CompletionStage<Done>> storeAverages() {
55+
return Flow.of(Double.class)
56+
.mapAsyncUnordered(4, averageRepository::save)
57+
.toMat(Sink.ignore(), Keep.right());
58+
}
59+
60+
61+
CompletionStage<Done> calculateAverageForContent(String content) {
62+
return Source.single(content)
63+
.via(calculateAverage())
64+
.runWith(storeAverages(), ActorMaterializer.create(actorSystem))
65+
.whenComplete((d, e) -> {
66+
if (d != null) {
67+
System.out.println("Import finished ");
68+
} else {
69+
e.printStackTrace();
70+
}
71+
});
72+
}
73+
74+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.akkastreams;
2+
3+
import akka.NotUsed;
4+
import akka.actor.ActorSystem;
5+
import akka.stream.ActorMaterializer;
6+
import akka.stream.javadsl.Flow;
7+
import akka.stream.javadsl.Source;
8+
import akka.stream.testkit.javadsl.TestSink;
9+
import org.junit.Test;
10+
11+
12+
public class DataImporterUnitTest {
13+
private final ActorSystem actorSystem = ActorSystem.create();
14+
15+
@Test
16+
public void givenStreamOfIntegers_whenCalculateAverageOfPairs_thenShouldReturnProperResults() {
17+
//given
18+
Flow<String, Double, NotUsed> tested = new DataImporter(actorSystem).calculateAverage();
19+
String input = "1;9;11;0";
20+
21+
//when
22+
Source<Double, NotUsed> flow = Source.single(input).via(tested);
23+
24+
//then
25+
flow
26+
.runWith(TestSink.probe(actorSystem), ActorMaterializer.create(actorSystem))
27+
.request(4)
28+
.expectNextUnordered(5d, 5.5);
29+
}
30+
31+
@Test
32+
public void givenStreamOfIntegers_whenCalculateAverageAndSaveToSink_thenShouldFinishSuccessfully() {
33+
//given
34+
DataImporter dataImporter = new DataImporter(actorSystem);
35+
String input = "10;90;110;10";
36+
37+
//when
38+
dataImporter.calculateAverageForContent(input)
39+
.thenAccept(d -> actorSystem.terminate());
40+
41+
}
42+
43+
}

0 commit comments

Comments
 (0)