Skip to content

Commit

Permalink
Day094
Browse files Browse the repository at this point in the history
  • Loading branch information
hbelmiro committed Aug 21, 2021
1 parent d923bf4 commit 6c0f0d0
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 2 deletions.
60 changes: 59 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3532,7 +3532,7 @@ public class Day093 {
public static void main(String[] args) {
List<String> myList = getList();
// If myList is null, a NullPointerException will be thrown
// If myList is null, a NullPointerException will be thrown
for (String s : myList) {
System.out.println(s);
}
Expand All @@ -3554,4 +3554,62 @@ public class Day093 {
return Instant.now().get(ChronoField.MILLI_OF_SECOND) % 2 == 0;
}
}
----

== Day 94 - Using Reactive Streams from Java 9
[source,java]
----
package com.thegreatapi.ahundreddaysofjava.day094;
import java.util.List;
import java.util.concurrent.Flow;
import java.util.concurrent.SubmissionPublisher;
public class Day094 {
public static void main(String[] args) {
List<String> judasPriestMembers = List.of(
"Rob Halford",
"K.K. Downing",
"Glenn Tipton",
"Ian Hill",
"Scott Travis"
);
var subscriber = new MySubscriber();
try (var publisher = new SubmissionPublisher<String>()) {
publisher.subscribe(subscriber);
judasPriestMembers.forEach(publisher::submit);
}
}
static class MySubscriber implements Flow.Subscriber<String> {
private Flow.Subscription subscription;
@Override
public void onSubscribe(Flow.Subscription subscription) {
System.out.println("Started subscription");
this.subscription = subscription;
subscription.request(1);
}
@Override
public void onNext(String item) {
System.out.println(item);
subscription.request(1);
}
@Override
public void onError(Throwable throwable) {
System.err.println("Error: " + throwable);
}
@Override
public void onComplete() {
System.out.println("Subscription complete");
}
}
}
----
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Day093 {
public static void main(String[] args) {
List<String> myList = getList();

// If myList is null, a NullPointerException will be thrown
// If myList is null, a NullPointerException will be thrown
for (String s : myList) {
System.out.println(s);
}
Expand Down
19 changes: 19 additions & 0 deletions days/day094/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>days</artifactId>
<groupId>com.thegreatapi.100daysofjava</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>day094</artifactId>

<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.thegreatapi.ahundreddaysofjava.day094;

import java.util.List;
import java.util.concurrent.Flow;
import java.util.concurrent.SubmissionPublisher;

public class Day094 {

public static void main(String[] args) {
List<String> judasPriestMembers = List.of(
"Rob Halford",
"K.K. Downing",
"Glenn Tipton",
"Ian Hill",
"Scott Travis"
);

var subscriber = new MySubscriber();

try (var publisher = new SubmissionPublisher<String>()) {
publisher.subscribe(subscriber);
judasPriestMembers.forEach(publisher::submit);
}
}

static class MySubscriber implements Flow.Subscriber<String> {

private Flow.Subscription subscription;

@Override
public void onSubscribe(Flow.Subscription subscription) {
System.out.println("Started subscription");
this.subscription = subscription;
subscription.request(1);
}

@Override
public void onNext(String item) {
System.out.println(item);
subscription.request(1);
}

@Override
public void onError(Throwable throwable) {
System.err.println("Error: " + throwable);
}

@Override
public void onComplete() {
System.out.println("Subscription complete");
}
}
}
1 change: 1 addition & 0 deletions days/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
<module>day091</module>
<module>day092</module>
<module>day093</module>
<module>day094</module>
</modules>

</project>

0 comments on commit 6c0f0d0

Please sign in to comment.