-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamingexecutionresultFluxDocument.java
51 lines (42 loc) · 1.48 KB
/
streamingexecutionresultFluxDocument.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
import reactor.core.publisher.Flux;
import reactor.core.publisher.Sinks;
import java.util.Random;
public class SocketPoller {
public static void main(String[] args) {
Sinks.Many<String> sink1 = Sinks.many().unicast().onBackpressureBuffer();
Sinks.Many<String> sink2 = Sinks.many().unicast().onBackpressureBuffer();
// Thread 1 polling socket
new Thread(() -> {
while (true) {
String data = pollDataFromSocket1(); // Mocked method
sink1.tryEmitNext(data);
}
}).start();
// Thread 2 polling socket
new Thread(() -> {
while (true) {
String data = pollDataFromSocket2(); // Mocked method
sink2.tryEmitNext(data);
}
}).start();
Flux<String> mergedFlux = Flux.merge(sink1.asFlux(), sink2.asFlux());
mergedFlux.subscribe(data -> System.out.println("Received: " + data));
}
// Mock methods for demonstration
private static String pollDataFromSocket1() {
try {
Thread.sleep(new Random().nextInt(1000)); // Random delay
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Data1";
}
private static String pollDataFromSocket2() {
try {
Thread.sleep(new Random().nextInt(1000)); // Random delay
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Data2";
}
}