Skip to content

Commit 6def745

Browse files
committed
BAEL-679. Java 9 Reactive Streams. Second draft.
1 parent a055ab5 commit 6def745

4 files changed

Lines changed: 314 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.baeldung.java9.reactive;
2+
3+
import java.util.ArrayList;
4+
import java.util.concurrent.Flow.Subscriber;
5+
import java.util.concurrent.Flow.Subscription;
6+
7+
public class BaeldungBatchSubscriberImpl<T> implements Subscriber<String> {
8+
private Subscription subscription;
9+
private boolean completed = false;
10+
private int counter;
11+
private ArrayList<String> buffer;
12+
public static final int BUFFER_SIZE = 5;
13+
14+
public BaeldungBatchSubscriberImpl() {
15+
buffer = new ArrayList<String>();
16+
}
17+
18+
public boolean isCompleted() {
19+
return completed;
20+
}
21+
22+
public void setCompleted(boolean completed) {
23+
this.completed = completed;
24+
}
25+
26+
public int getCounter() {
27+
return counter;
28+
}
29+
30+
public void setCounter(int counter) {
31+
this.counter = counter;
32+
}
33+
34+
@Override
35+
public void onSubscribe(Subscription subscription) {
36+
this.subscription = subscription;
37+
subscription.request(BUFFER_SIZE);
38+
}
39+
40+
@Override
41+
public void onNext(String item) {
42+
buffer.add(item);
43+
// if buffer is full, process the items.
44+
if (buffer.size() >= BUFFER_SIZE) {
45+
processBuffer();
46+
subscription.request(BUFFER_SIZE);
47+
} else if(buffer.size() == 0) {
48+
// If buffer empty, request more items.
49+
subscription.request(BUFFER_SIZE);
50+
}
51+
}
52+
53+
private void processBuffer() {
54+
if (buffer.isEmpty())
55+
return;
56+
// Process all items in the buffer. Here, we just print it and sleep for 1 second.
57+
System.out.print("Processed items: ");
58+
buffer.stream()
59+
.forEach(item -> {
60+
System.out.print(" " + item);
61+
});
62+
System.out.println();
63+
try {
64+
Thread.sleep(1000);
65+
} catch (InterruptedException e) {
66+
e.printStackTrace();
67+
}
68+
counter = counter + buffer.size();
69+
buffer.clear();
70+
}
71+
72+
@Override
73+
public void onError(Throwable t) {
74+
t.printStackTrace();
75+
}
76+
77+
@Override
78+
public void onComplete() {
79+
completed = true;
80+
// process any remaining items in buffer before
81+
processBuffer();
82+
subscription.cancel();
83+
}
84+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.java9.reactive;
2+
3+
import java.util.concurrent.Flow.Subscriber;
4+
import java.util.concurrent.Flow.Subscription;
5+
6+
public class BaeldungSubscriberImpl<T> implements Subscriber<String> {
7+
private Subscription subscription;
8+
private boolean completed = false;
9+
private int counter;
10+
11+
public boolean isCompleted() {
12+
return completed;
13+
}
14+
15+
public void setCompleted(boolean completed) {
16+
this.completed = completed;
17+
}
18+
19+
public int getCounter() {
20+
return counter;
21+
}
22+
23+
public void setCounter(int counter) {
24+
this.counter = counter;
25+
}
26+
27+
@Override
28+
public void onSubscribe(Subscription subscription) {
29+
this.subscription = subscription;
30+
subscription.request(1);
31+
}
32+
33+
@Override
34+
public void onNext(String item) {
35+
try {
36+
Thread.sleep(1000);
37+
} catch (InterruptedException e) {
38+
e.printStackTrace();
39+
}
40+
counter++;
41+
System.out.println("Processed item : " + item);
42+
subscription.request(1);
43+
}
44+
45+
@Override
46+
public void onError(Throwable t) {
47+
t.printStackTrace();
48+
}
49+
50+
@Override
51+
public void onComplete() {
52+
completed = true;
53+
subscription.cancel();
54+
}
55+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.baeldung.java9.reactive;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.util.concurrent.ForkJoinPool;
7+
import java.util.concurrent.SubmissionPublisher;
8+
import java.util.concurrent.TimeUnit;
9+
import java.util.stream.IntStream;
10+
11+
import org.junit.Before;
12+
import org.junit.Rule;
13+
import org.junit.Test;
14+
import org.junit.rules.Stopwatch;
15+
16+
public class BaeldungBatchSubscriberImplTest {
17+
18+
private static final int ITEM_SIZE = 10;
19+
private SubmissionPublisher<String> publisher;
20+
private BaeldungBatchSubscriberImpl<String> subscriber;
21+
22+
@Before
23+
public void initialize() {
24+
this.publisher = new SubmissionPublisher<String>(ForkJoinPool.commonPool(), 6);
25+
this.subscriber = new BaeldungBatchSubscriberImpl<String>();
26+
publisher.subscribe(subscriber);
27+
}
28+
29+
@Rule
30+
public Stopwatch stopwatch = new Stopwatch() {
31+
32+
};
33+
34+
@Test
35+
public void testReactiveStreamCount() {
36+
IntStream.range(0, ITEM_SIZE)
37+
.forEach(item -> publisher.submit(item + ""));
38+
publisher.close();
39+
40+
do {
41+
// wait for subscribers to complete all processing.
42+
try {
43+
Thread.sleep(100);
44+
} catch (InterruptedException e) {
45+
// TODO Auto-generated catch block
46+
e.printStackTrace();
47+
}
48+
} while (!subscriber.isCompleted());
49+
50+
int count = subscriber.getCounter();
51+
52+
assertEquals(ITEM_SIZE, count);
53+
}
54+
55+
@Test
56+
public void testReactiveStreamTime() {
57+
IntStream.range(0, ITEM_SIZE)
58+
.forEach(item -> publisher.submit(item + ""));
59+
publisher.close();
60+
61+
do {
62+
// wait for subscribers to complete all processing.
63+
try {
64+
Thread.sleep(100);
65+
} catch (InterruptedException e) {
66+
// TODO Auto-generated catch block
67+
e.printStackTrace();
68+
}
69+
} while (!subscriber.isCompleted());
70+
71+
// The runtime in seconds should be equal to the number of items in each batch.
72+
assertTrue(stopwatch.runtime(TimeUnit.SECONDS) >= (ITEM_SIZE / subscriber.BUFFER_SIZE));
73+
}
74+
75+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.baeldung.java9.reactive;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.util.concurrent.ForkJoinPool;
7+
import java.util.concurrent.SubmissionPublisher;
8+
import java.util.concurrent.TimeUnit;
9+
import java.util.stream.IntStream;
10+
11+
import org.junit.Before;
12+
import org.junit.Rule;
13+
import org.junit.Test;
14+
import org.junit.rules.Stopwatch;
15+
16+
public class BaeldungSubscriberImplTest {
17+
18+
private static final int ITEM_SIZE = 10;
19+
private SubmissionPublisher<String> publisher;
20+
private BaeldungSubscriberImpl<String> subscriber;
21+
22+
@Before
23+
public void initialize() {
24+
// create Publisher with max buffer capacity 3.
25+
this.publisher = new SubmissionPublisher<String>(ForkJoinPool.commonPool(), 3);
26+
this.subscriber = new BaeldungSubscriberImpl<String>();
27+
publisher.subscribe(subscriber);
28+
}
29+
30+
@Rule
31+
public Stopwatch stopwatch = new Stopwatch() {
32+
33+
};
34+
35+
@Test
36+
public void testReactiveStreamCount() {
37+
IntStream.range(0, ITEM_SIZE)
38+
.forEach(item -> publisher.submit(item + ""));
39+
publisher.close();
40+
41+
do {
42+
// wait for subscribers to complete all processing.
43+
try {
44+
Thread.sleep(100);
45+
} catch (InterruptedException e) {
46+
// TODO Auto-generated catch block
47+
e.printStackTrace();
48+
}
49+
} while (!subscriber.isCompleted());
50+
51+
int count = subscriber.getCounter();
52+
53+
assertEquals(ITEM_SIZE, count);
54+
}
55+
56+
@Test
57+
public void testReactiveStreamTime() {
58+
IntStream.range(0, ITEM_SIZE)
59+
.forEach(item -> publisher.submit(item + ""));
60+
publisher.close();
61+
62+
do {
63+
// wait for subscribers to complete all processing.
64+
try {
65+
Thread.sleep(100);
66+
} catch (InterruptedException e) {
67+
// TODO Auto-generated catch block
68+
e.printStackTrace();
69+
}
70+
} while (!subscriber.isCompleted());
71+
72+
// The runtime in seconds should be equal to the number of items.
73+
assertTrue(stopwatch.runtime(TimeUnit.SECONDS) >= ITEM_SIZE);
74+
}
75+
76+
@Test
77+
public void testReactiveStreamOffer() {
78+
IntStream.range(0, ITEM_SIZE)
79+
.forEach(item -> publisher.offer(item + "", (subscriber, string) -> {
80+
// Returning false means this item will be dropped (no retry), if blocked.
81+
return false;
82+
}));
83+
publisher.close();
84+
85+
do {
86+
// wait for subscribers to complete all processing.
87+
try {
88+
Thread.sleep(100);
89+
} catch (InterruptedException e) {
90+
// TODO Auto-generated catch block
91+
e.printStackTrace();
92+
}
93+
} while (!subscriber.isCompleted());
94+
95+
int count = subscriber.getCounter();
96+
// Because 10 items were offered and the buffer capacity was 3, few items will not be processed.
97+
assertTrue(ITEM_SIZE > count);
98+
}
99+
100+
}

0 commit comments

Comments
 (0)