├── .gitignore
├── .idea
├── compiler.xml
├── dictionaries
│ └── javabrains.xml
├── inspectionProfiles
│ └── Project_Default.xml
├── jarRepositories.xml
├── misc.xml
├── uiDesigner.xml
├── vcs.xml
└── workspace.xml
├── pom.xml
└── src
└── main
└── java
└── io
└── javabrains
└── reactiveworkshop
├── Exercise1.java
├── Exercise2.java
├── Exercise3.java
├── Exercise4.java
├── Exercise5.java
├── Exercise6.java
├── Exercise7.java
├── Exercise8.java
├── Exercise9.java
├── ReactiveSources.java
├── StreamSources.java
└── User.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | .DS_Store
4 |
5 | # Compiled class file
6 | *.class
7 |
8 | # Log file
9 | *.log
10 |
11 | # BlueJ files
12 | *.ctxt
13 |
14 | # Mobile Tools for Java (J2ME)
15 | .mtj.tmp/
16 |
17 | # Package Files #
18 | *.jar
19 | *.war
20 | *.nar
21 | *.ear
22 | *.zip
23 | *.tar.gz
24 | *.rar
25 |
26 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
27 | hs_err_pid*
28 | replay_pid*
29 | .idea/inspectionProfiles/Project_Default.xml
30 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/dictionaries/javabrains.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/inspectionProfiles/Project_Default.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/jarRepositories.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/.idea/uiDesigner.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | -
6 |
7 |
8 | -
9 |
10 |
11 | -
12 |
13 |
14 | -
15 |
16 |
17 | -
18 |
19 |
20 |
21 |
22 |
23 | -
24 |
25 |
26 |
27 |
28 |
29 | -
30 |
31 |
32 |
33 |
34 |
35 | -
36 |
37 |
38 |
39 |
40 |
41 | -
42 |
43 |
44 |
45 |
46 | -
47 |
48 |
49 |
50 |
51 | -
52 |
53 |
54 |
55 |
56 | -
57 |
58 |
59 |
60 |
61 | -
62 |
63 |
64 |
65 |
66 | -
67 |
68 |
69 |
70 |
71 | -
72 |
73 |
74 | -
75 |
76 |
77 |
78 |
79 | -
80 |
81 |
82 |
83 |
84 | -
85 |
86 |
87 |
88 |
89 | -
90 |
91 |
92 |
93 |
94 | -
95 |
96 |
97 |
98 |
99 | -
100 |
101 |
102 | -
103 |
104 |
105 | -
106 |
107 |
108 | -
109 |
110 |
111 | -
112 |
113 |
114 |
115 |
116 | -
117 |
118 |
119 | -
120 |
121 |
122 |
123 |
124 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 | 1650213103534
152 |
153 |
154 | 1650213103534
155 |
156 |
157 |
158 |
159 |
160 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | io.javabrains
8 | reactive-example
9 | 1.0-SNAPSHOT
10 |
11 |
12 | 17
13 | 17
14 |
15 |
16 |
17 |
18 | io.projectreactor
19 | reactor-core
20 | 3.4.14
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/src/main/java/io/javabrains/reactiveworkshop/Exercise1.java:
--------------------------------------------------------------------------------
1 | package io.javabrains.reactiveworkshop;
2 |
3 | public class Exercise1 {
4 |
5 | public static void main(String[] args) {
6 |
7 | // Use StreamSources.intNumbersStream() and StreamSources.userStream()
8 |
9 | // Print all numbers in the intNumbersStream stream
10 | // TODO: Write code here
11 |
12 | // Print numbers from intNumbersStream that are less than 5
13 | // TODO: Write code here
14 |
15 | // Print the second and third numbers in intNumbersStream that's greater than 5
16 | // TODO: Write code here
17 |
18 | // Print the first number in intNumbersStream that's greater than 5.
19 | // If nothing is found, print -1
20 | // TODO: Write code here
21 |
22 | // Print first names of all users in userStream
23 | // TODO: Write code here
24 |
25 | // Print first names in userStream for users that have IDs from number stream
26 | // TODO: Write code here
27 |
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/io/javabrains/reactiveworkshop/Exercise2.java:
--------------------------------------------------------------------------------
1 | package io.javabrains.reactiveworkshop;
2 |
3 | import java.io.IOException;
4 |
5 | public class Exercise2 {
6 |
7 | public static void main(String[] args) throws IOException {
8 |
9 | // Use ReactiveSources.intNumbersFlux() and ReactiveSources.userFlux()
10 |
11 | // Print all numbers in the ReactiveSources.intNumbersFlux stream
12 | // TODO: Write code here
13 |
14 | // Print all users in the ReactiveSources.userFlux stream
15 | // TODO: Write code here
16 |
17 | System.out.println("Press a key to end");
18 | System.in.read();
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/io/javabrains/reactiveworkshop/Exercise3.java:
--------------------------------------------------------------------------------
1 | package io.javabrains.reactiveworkshop;
2 |
3 | import java.io.IOException;
4 |
5 | public class Exercise3 {
6 |
7 | public static void main(String[] args) throws IOException {
8 |
9 | // Use ReactiveSources.intNumbersFlux()
10 |
11 | // Get all numbers in the ReactiveSources.intNumbersFlux stream
12 | // into a List and print the list and its size
13 | // TODO: Write code here
14 |
15 |
16 | System.out.println("Press a key to end");
17 | System.in.read();
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/io/javabrains/reactiveworkshop/Exercise4.java:
--------------------------------------------------------------------------------
1 | package io.javabrains.reactiveworkshop;
2 |
3 | import java.io.IOException;
4 |
5 | public class Exercise4 {
6 |
7 | public static void main(String[] args) throws IOException {
8 |
9 | // Use ReactiveSources.intNumberMono()
10 |
11 | // Print the value from intNumberMono when it emits
12 | // TODO: Write code here
13 |
14 | // Get the value from the Mono into an integer variable
15 | // TODO: Write code here
16 |
17 | System.out.println("Press a key to end");
18 | System.in.read();
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/io/javabrains/reactiveworkshop/Exercise5.java:
--------------------------------------------------------------------------------
1 | package io.javabrains.reactiveworkshop;
2 |
3 | import java.io.IOException;
4 |
5 | public class Exercise5 {
6 |
7 | public static void main(String[] args) throws IOException {
8 |
9 | // Use ReactiveSources.intNumberMono() and ReactiveSources.userMono()
10 |
11 | // Subscribe to a flux using the error and completion hooks
12 | // TODO: Write code here
13 |
14 | // Subscribe to a flux using an implementation of BaseSubscriber
15 | // TODO: Write code here
16 |
17 | System.out.println("Press a key to end");
18 | System.in.read();
19 | }
20 |
21 | }
--------------------------------------------------------------------------------
/src/main/java/io/javabrains/reactiveworkshop/Exercise6.java:
--------------------------------------------------------------------------------
1 | package io.javabrains.reactiveworkshop;
2 |
3 | import java.io.IOException;
4 |
5 | public class Exercise6 {
6 |
7 |
8 | public static void main(String[] args) throws IOException {
9 |
10 | // Use ReactiveSources.unresponsiveFlux() and ReactiveSources.unresponsiveMono()
11 |
12 | // Get the value from the Mono into a String variable but give up after 5 seconds
13 | // TODO: Write code here
14 |
15 | // Get the value from unresponsiveFlux into a String list but give up after 5 seconds
16 | // Come back and do this when you've learnt about operators!
17 | // TODO: Write code here
18 |
19 | System.out.println("Press a key to end");
20 | System.in.read();
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/io/javabrains/reactiveworkshop/Exercise7.java:
--------------------------------------------------------------------------------
1 | package io.javabrains.reactiveworkshop;
2 |
3 | import java.io.IOException;
4 |
5 | public class Exercise7 {
6 |
7 |
8 | public static void main(String[] args) throws IOException {
9 |
10 | // Use ReactiveSources.intNumberMono() and ReactiveSources.userMono()
11 |
12 | // Print all values from intNumbersFlux that's greater than 5
13 | // TODO: Write code here
14 |
15 | // Print 10 times each value from intNumbersFlux that's greater than 5
16 | // TODO: Write code here
17 |
18 | // Print 10 times each value from intNumbersFlux for the first 3 numbers emitted that's greater than 5
19 | // TODO: Write code here
20 |
21 | // Print each value from intNumbersFlux that's greater than 20. Print -1 if no elements are found
22 | // TODO: Write code here
23 |
24 | // Switch ints from intNumbersFlux to the right user from userFlux
25 | // TODO: Write code here
26 |
27 | // Print only distinct numbers from intNumbersFluxWithRepeat
28 | // TODO: Write code here
29 |
30 | // Print from intNumbersFluxWithRepeat excluding immediately repeating numbers
31 | // TODO: Write code here
32 |
33 | System.out.println("Press a key to end");
34 | System.in.read();
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/io/javabrains/reactiveworkshop/Exercise8.java:
--------------------------------------------------------------------------------
1 | package io.javabrains.reactiveworkshop;
2 |
3 | import reactor.core.publisher.Flux;
4 |
5 | import java.io.IOException;
6 |
7 | public class Exercise8 {
8 |
9 |
10 | public static void main(String[] args) throws IOException {
11 |
12 | // Use ReactiveSources.intNumbersFluxWithException()
13 |
14 | // Print values from intNumbersFluxWithException and print a message when error happens
15 | // TODO: Write code here
16 |
17 | // Print values from intNumbersFluxWithException and continue on errors
18 | // TODO: Write code here
19 |
20 | // Print values from intNumbersFluxWithException and when errors
21 | // happen, replace with a fallback sequence of -1 and -2
22 | // TODO: Write code here
23 |
24 | System.out.println("Press a key to end");
25 | System.in.read();
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/io/javabrains/reactiveworkshop/Exercise9.java:
--------------------------------------------------------------------------------
1 | package io.javabrains.reactiveworkshop;
2 |
3 | import java.io.IOException;
4 |
5 | public class Exercise9 {
6 |
7 |
8 | public static void main(String[] args) throws IOException {
9 |
10 | // Use ReactiveSources.intNumbersFlux()
11 |
12 | // Print size of intNumbersFlux after the last item returns
13 | // TODO: Write code here
14 |
15 | // Collect all items of intNumbersFlux into a single list and print it
16 | // TODO: Write code here
17 |
18 | // Transform to a sequence of sums of adjacent two numbers
19 | // TODO: Write code here
20 |
21 | System.out.println("Press a key to end");
22 | System.in.read();
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/io/javabrains/reactiveworkshop/ReactiveSources.java:
--------------------------------------------------------------------------------
1 | package io.javabrains.reactiveworkshop;
2 |
3 | import reactor.core.publisher.Flux;
4 | import reactor.core.publisher.Mono;
5 |
6 | import java.time.Duration;
7 |
8 | /**
9 | * This class is a source of reactive streams used in the exercises.
10 | * DO NOT MODIFY THIS CODE
11 | *
12 | * @author koushikkothagal
13 | */
14 | public class ReactiveSources {
15 |
16 | public static Flux stringNumbersFlux() {
17 | return Flux.just("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten")
18 | .delayElements(Duration.ofSeconds(1));
19 | }
20 |
21 | public static Flux intNumbersFlux() {
22 | return Flux
23 | .range(1, 10)
24 | .delayElements(Duration.ofSeconds(1));
25 | }
26 |
27 | public static Flux intNumbersFluxWithException() {
28 | return Flux
29 | .range(1, 10)
30 | .delayElements(Duration.ofSeconds(1))
31 | .map(e -> {
32 | if (e == 5) throw new RuntimeException("An error happened in the flux");
33 | return e;
34 | });
35 | }
36 |
37 | public static Mono intNumberMono() {
38 | return Mono.just(42)
39 | .delayElement(Duration.ofSeconds(1));
40 | }
41 |
42 | public static Flux userFlux() {
43 | return Flux.just(
44 | new User(1, "Lionel", "Messi"),
45 | new User(2, "Cristiano", "Ronaldo"),
46 | new User(2, "Diego", "Maradona"),
47 | new User(4, "Zinedine", "Zidane"),
48 | new User(5, "Jürgen", "Klinsmann"),
49 | new User(6, "Gareth", "Bale")
50 | ).delayElements(Duration.ofSeconds(1));
51 | }
52 |
53 | public static Mono userMono() {
54 | return Mono.just(
55 | new User(1, "Lionel", "Messi")
56 | ).delayElement(Duration.ofSeconds(1));
57 |
58 | }
59 |
60 | public static Flux unresponsiveFlux() {
61 | return Flux.never();
62 | }
63 |
64 | public static Mono unresponsiveMono() {
65 | return Mono.never();
66 | }
67 |
68 | public static Flux intNumbersFluxWithRepeat() {
69 | return Flux
70 | .just(1, 2, 1, 1, 3, 2, 4, 5, 1)
71 | .delayElements(Duration.ofSeconds(1));
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/main/java/io/javabrains/reactiveworkshop/StreamSources.java:
--------------------------------------------------------------------------------
1 | package io.javabrains.reactiveworkshop;
2 |
3 | import java.util.stream.Stream;
4 |
5 | /**
6 | * This class is a source of collection streams used in the exercises.
7 | * DO NOT MODIFY THIS CODE
8 | *
9 | * @author koushikkothagal
10 | */
11 | public class StreamSources {
12 |
13 | public static Stream stringNumbersStream() {
14 | return Stream.of("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten");
15 | }
16 |
17 | public static Stream intNumbersStream() {
18 | return Stream.iterate(0, i -> i + 2)
19 | .limit(10);
20 | }
21 |
22 | public static Stream userStream() {
23 | return Stream.of(
24 | new User(1, "Lionel", "Messi"),
25 | new User(2, "Cristiano", "Ronaldo"),
26 | new User(2, "Diego", "Maradona"),
27 | new User(4, "Zinedine", "Zidane"),
28 | new User(5, "Jürgen", "Klinsmann"),
29 | new User(6, "Gareth", "Bale")
30 | );
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/io/javabrains/reactiveworkshop/User.java:
--------------------------------------------------------------------------------
1 | package io.javabrains.reactiveworkshop;
2 |
3 | /**
4 | * This class is a sample custom Java type used in the exercises.
5 | * DO NOT MODIFY THIS CODE
6 | *
7 | * @author koushikkothagal
8 | */
9 |
10 | public class User {
11 |
12 | private int id;
13 | private String firstName;
14 | private String lastName;
15 |
16 | public User(int id, String firstName, String lastName) {
17 | this.id = id;
18 | this.firstName = firstName;
19 | this.lastName = lastName;
20 | }
21 |
22 | public int getId() {
23 | return id;
24 | }
25 |
26 | public void setId(int id) {
27 | this.id = id;
28 | }
29 |
30 | public String getFirstName() {
31 | return firstName;
32 | }
33 |
34 | public void setFirstName(String firstName) {
35 | this.firstName = firstName;
36 | }
37 |
38 | public String getLastName() {
39 | return lastName;
40 | }
41 |
42 | public void setLastName(String lastName) {
43 | this.lastName = lastName;
44 | }
45 |
46 | @Override
47 | public String toString() {
48 | return "User{" + "id=" + id + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + '}';
49 | }
50 | }
51 |
--------------------------------------------------------------------------------