Skip to content

Commit 29f5eb0

Browse files
committed
Added an example on the JDK 9 Flow API (session 5).
1 parent 44438b8 commit 29f5eb0

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
### Implementing the reactive programming API
2+
3+
In this example we are going to demonstrate how does the `java.util.concurrent.Flow` API work in general. For that purpose we are going to create an implementation of the `java.util.concurrent.Flow.Subscriber` interface (`com.project.DefaultSubscriber`) and register a concrete subscriber to a given `java.util.concurrent.Flow.Publisher` instance. The flow publisher is an instance of the `java.util.concurrent.SubmissionPublisher` class provided by JDK 9 that implements the `java.util.concurrent.Flow.Publisher` API.
4+
5+
In the source folder there is a `PrimeNumberSubscriber` project that provides a stub implementation of the subscriber and demo for the publisher.
6+
7+
Add implementation of the onNext() and onSubscribe() methods accordingly. The onNext() method should check if the passed number is prime or not and print the result on the standard output. Then create an instance of the `com.project.DefaultSubscriber` class and register it to the publisher created in the `com.project.Main` class.
8+
9+
Use the `compile.sh` script to compile the project:
10+
11+
$ ./compile.sh
12+
13+
Run the compiled `com.project.Main` class with the `run.sh` script:
14+
15+
$ ./run.sh
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
set -eu
4+
5+
javac -d src/PrimeNumberSubscriber/target/classes \
6+
src/PrimeNumberSubscriber/com/project/*.java
7+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
set -eu
4+
5+
java -cp src/PrimeNumberSubscriber/target/classes com.project.Main
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.project;
2+
3+
import java.util.concurrent.Flow.Subscriber;
4+
import java.util.concurrent.Flow.Subscription;
5+
import java.util.logging.Level;
6+
import java.util.logging.Logger;
7+
8+
public class DefaultSubscriber implements Subscriber<Integer>{
9+
10+
private static final Logger LOGGER = Logger.getLogger(DefaultSubscriber.class.getName());
11+
12+
private Subscription subscription;
13+
14+
@Override
15+
public void onComplete() {
16+
LOGGER.info("COMPLETED");
17+
}
18+
19+
@Override
20+
public void onError(Throwable t) {
21+
LOGGER.log(Level.SEVERE, t.getMessage(), t);
22+
}
23+
24+
@Override
25+
public void onNext(Integer number) {
26+
// TODO
27+
}
28+
29+
@Override
30+
public void onSubscribe(Subscription subscription) {
31+
// TODO
32+
}
33+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.project;
2+
3+
import java.util.Arrays;
4+
import java.util.concurrent.SubmissionPublisher;
5+
import java.util.logging.Logger;
6+
7+
public class Main {
8+
9+
private static final Logger LOGGER = Logger.getLogger(Main.class.getName());
10+
11+
public static void main(String[] args) throws InterruptedException {
12+
13+
LOGGER.info("Checking prime numbers ...");
14+
SubmissionPublisher<Integer> publisher = new SubmissionPublisher<>();
15+
16+
// TODO: create subscriber
17+
18+
Integer[] numbers = {4, 17, 19, 101, 105, 107, 201};
19+
20+
Arrays.asList(numbers).stream().forEach(i -> publisher.submit(i));
21+
22+
// add some wait time ...
23+
Thread.sleep(3000);
24+
publisher.close();
25+
}
26+
}

0 commit comments

Comments
 (0)