This repository has been archived by the owner on Jan 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 124
/
Test_2_3.java
156 lines (136 loc) · 4.96 KB
/
Test_2_3.java
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
152
153
154
155
156
package com.getset;
import com.getset.chapter_2_2.MyEventListener;
import com.getset.chapter_2_2.MyEventSource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.reactivestreams.Subscription;
import reactor.core.publisher.BaseSubscriber;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;
import reactor.core.scheduler.Schedulers;
import java.time.Duration;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class Test_2_3 {
private final int EVENT_DURATION = 10; // 生成的事件间隔时间,单位毫秒
private final int EVENT_COUNT = 20; // 生成的事件个数
private final int PROCESS_DURATION = 30; // 订阅者处理每个元素的时间,单位毫秒
private Flux<MyEventSource.MyEvent> fastPublisher;
private SlowSubscriber slowSubscriber;
private MyEventSource eventSource;
private CountDownLatch countDownLatch;
/**
* 准备工作。
*/
@Before
public void setup() {
countDownLatch = new CountDownLatch(1);
slowSubscriber = new SlowSubscriber();
eventSource = new MyEventSource();
}
/**
* 触发订阅,使用CountDownLatch等待订阅者处理完成。
*/
@After
public void subscribe() throws InterruptedException {
fastPublisher.subscribe(slowSubscriber);
generateEvent(EVENT_COUNT, EVENT_DURATION);
countDownLatch.await(1, TimeUnit.MINUTES);
}
/**
* 测试create方法的不同OverflowStrategy的效果。
*/
@Test
public void testCreateBackPressureStratety() {
fastPublisher =
createFlux(FluxSink.OverflowStrategy.IGNORE) // BUFFER/DROP/LATEST/ERROR/IGNORE
.doOnRequest(n -> System.out.println(" === request: " + n + " ==="))
.publishOn(Schedulers.newSingle("newSingle"), 2);
}
/**
* 测试不同的onBackpressureXxx方法的效果。
*/
@Test
public void testOnBackPressureXxx() {
fastPublisher = createFlux(FluxSink.OverflowStrategy.BUFFER)
// .onBackpressureDrop()
.onBackpressureBuffer()
// .onBackpressureLatest()
// .onBackpressureError()
.doOnRequest(n -> System.out.println(" === request: " + n + " ==="))
.publishOn(Schedulers.newSingle("newSingle"), 1);
}
/**
* 其他可以实现类似效果的操作符。
*/
@Test
public void testSimilarOperators() {
fastPublisher = createFlux(FluxSink.OverflowStrategy.BUFFER)
.doOnRequest(n -> System.out.println(" === request: " + n + " ==="))
.sample(Duration.ofMillis(30))
;
}
/**
* 使用create方法生成“快的发布者”。
*
* @param strategy 回压策略
* @return Flux
*/
private Flux<MyEventSource.MyEvent> createFlux(FluxSink.OverflowStrategy strategy) {
return Flux.create(sink -> eventSource.register(new MyEventListener() {
@Override
public void onNewEvent(MyEventSource.MyEvent event) {
System.out.println("publish >>> " + event.getMessage());
sink.next(event);
}
@Override
public void onEventStopped() {
sink.complete();
}
}), strategy);
}
/**
* 生成MyEvent。
*
* @param count 生成MyEvent的个数。
* @param millis 每个MyEvent之间的时间间隔。
*/
private void generateEvent(int count, int millis) {
for (int i = 0; i < count; i++) {
try {
TimeUnit.MILLISECONDS.sleep(millis);
} catch (InterruptedException e) {
}
eventSource.newEvent(new MyEventSource.MyEvent(new Date(), "Event-" + i));
}
eventSource.eventStopped();
}
/**
* 内部类,“慢的订阅者”。
*/
class SlowSubscriber extends BaseSubscriber<MyEventSource.MyEvent> {
@Override
protected void hookOnSubscribe(Subscription subscription) {
request(1);
}
@Override
protected void hookOnNext(MyEventSource.MyEvent event) {
System.out.println(" receive <<< " + event.getMessage());
try {
TimeUnit.MILLISECONDS.sleep(PROCESS_DURATION);
} catch (InterruptedException e) {
}
request(1);
}
@Override
protected void hookOnError(Throwable throwable) {
System.err.println(" receive <<< " + throwable);
}
@Override
protected void hookOnComplete() {
countDownLatch.countDown();
}
}
}