Skip to content

Commit 73026b6

Browse files
committed
Added reactor core
1 parent 86029a3 commit 73026b6

3 files changed

Lines changed: 160 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
<module>querydsl</module>
9595

9696
<!-- <module>raml</module> -->
97+
<module>reactor-core</module>
9798
<module>redis</module>
9899
<module>rest-assured</module>
99100
<module>rest-testing</module>

‎reactor-core/pom.xml‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>org.baeldung</groupId>
6+
<artifactId>reactor-core</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
9+
<build>
10+
<plugins>
11+
<plugin>
12+
<groupId>org.apache.maven.plugins</groupId>
13+
<artifactId>maven-compiler-plugin</artifactId>
14+
<configuration>
15+
<source>1.8</source>
16+
<target>1.8</target>
17+
</configuration>
18+
</plugin>
19+
</plugins>
20+
</build>
21+
22+
<dependencies>
23+
24+
<dependency>
25+
<groupId>io.projectreactor</groupId>
26+
<artifactId>reactor-core</artifactId>
27+
<version>3.0.4.RELEASE</version>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>junit</groupId>
32+
<artifactId>junit</artifactId>
33+
<version>4.12</version>
34+
<scope>test</scope>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.assertj</groupId>
39+
<artifactId>assertj-core</artifactId>
40+
<version>3.6.1</version>
41+
<scope>test</scope>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>ch.qos.logback</groupId>
46+
<artifactId>logback-classic</artifactId>
47+
<version>1.1.3</version>
48+
</dependency>
49+
50+
</dependencies>
51+
52+
</project>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.baeldung.reactor;
2+
3+
import org.junit.Test;
4+
import org.reactivestreams.Subscriber;
5+
import org.reactivestreams.Subscription;
6+
import reactor.core.publisher.ConnectableFlux;
7+
import reactor.core.publisher.Flux;
8+
import reactor.core.publisher.FluxSink;
9+
import reactor.core.scheduler.Schedulers;
10+
11+
import java.time.Duration;
12+
import java.time.LocalDateTime;
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
import java.util.stream.Stream;
16+
17+
import static java.time.Duration.ofSeconds;
18+
import static java.util.stream.Collectors.toList;
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
public class ReactorTest {
22+
23+
@Test
24+
public void givenFlux_whenSubscribing_shouldStream() throws InterruptedException {
25+
26+
List<Integer> elements = new ArrayList<>();
27+
28+
Flux.just(1, 2, 3, 4)
29+
.log()
30+
.map(i -> i * 2)
31+
.subscribe(elements::add);
32+
33+
assertThat(elements).containsExactly(2, 4, 6, 8);
34+
}
35+
36+
@Test
37+
public void givenFlux_whenZipping_shouldCombine() {
38+
List<String> elements = new ArrayList<>();
39+
40+
Flux.just(1, 2, 3, 4)
41+
.log()
42+
.map(i -> i * 2)
43+
.zipWith(Flux.range(0, Integer.MAX_VALUE).log(), (two, one) -> String.format("First Flux: %d, Second Flux: %d", one, two))
44+
.subscribe(elements::add);
45+
46+
assertThat(elements).containsExactly(
47+
"First Flux: 0, Second Flux: 2",
48+
"First Flux: 1, Second Flux: 4",
49+
"First Flux: 2, Second Flux: 6",
50+
"First Flux: 3, Second Flux: 8");
51+
}
52+
53+
@Test
54+
public void givenFlux_whenApplyingBackPressure_shouldPushLessElements() throws InterruptedException {
55+
56+
List<Integer> elements = new ArrayList<>();
57+
58+
Flux.just(1, 2, 3, 4)
59+
.log()
60+
.map(i -> i * 2)
61+
.onBackpressureBuffer()
62+
.subscribe(new Subscriber<Integer>() {
63+
private Subscription s;
64+
int onNextAmount;
65+
66+
@Override
67+
public void onSubscribe(final Subscription s) {
68+
this.s = s;
69+
s.request(2);
70+
}
71+
72+
@Override
73+
public void onNext(final Integer integer) {
74+
elements.add(integer);
75+
onNextAmount++;
76+
if (onNextAmount % 2 == 0) {
77+
s.request(2);
78+
}
79+
}
80+
81+
@Override
82+
public void onError(final Throwable t) {
83+
}
84+
85+
@Override
86+
public void onComplete() {
87+
int ham = 2;
88+
}
89+
});
90+
91+
assertThat(elements).containsExactly(2, 4, 6, 8);
92+
}
93+
94+
@Test
95+
public void givenFlux_whenInParalle_shouldSubscribeInDifferentThreads() {
96+
List<Integer> elements = new ArrayList<>();
97+
98+
Flux.just(1, 2, 3, 4)
99+
.log()
100+
.map(i -> i * 2)
101+
.subscribeOn(Schedulers.parallel())
102+
.subscribe(elements::add);
103+
104+
assertThat(elements).containsExactly(2, 4, 6, 8);
105+
}
106+
107+
}

0 commit comments

Comments
 (0)