-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamsController.java
More file actions
158 lines (144 loc) · 6.46 KB
/
StreamsController.java
File metadata and controls
158 lines (144 loc) · 6.46 KB
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
157
158
package example;
import static com.caplin.integration.datasourcex.reactive.api.ContainerEvent.RowEvent;
import com.caplin.integration.datasourcex.reactive.api.ContainerEvent;
import com.caplin.integration.datasourcex.spring.annotations.IngressDestinationVariable;
import com.caplin.integration.datasourcex.spring.annotations.IngressToken;
import java.time.Duration;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.stereotype.Controller;
import reactor.core.publisher.Flux;
@Controller
public class StreamsController {
/**
* Streams an infinite sequence of Payload objects, where each payload contains an incrementing
* version number (starting from 0), the parameter1 & parameter2 values extracted from the path,
* and a randomly generated unique string identifier. <br>
* The stream emits new elements at one-second intervals.
*
* @see Flux
*/
@MessageMapping("/public/stream/{parameter1}/{parameter2}")
public Flux<Payload> genericStream(
@DestinationVariable String parameter1, @DestinationVariable int parameter2) {
return createPayloadFlux(null, null, parameter1, parameter2);
}
/**
* Streams an infinite sequence of Payload objects, where each payload contains an incrementing
* version number (starting from 0), the user ID, parameter1 & parameter2 values extracted from
* the path, and a randomly generated unique string identifier. <br>
* The stream emits new elements at one-second intervals.
*
* @see Flux
*/
@MessageMapping("/user/{userId}/stream/{parameter1}/{parameter2}")
public Flux<Payload> userStream(
@IngressDestinationVariable(token = IngressToken.USER_ID) String userId,
@DestinationVariable String parameter1,
@DestinationVariable int parameter2) {
return createPayloadFlux(userId, null, parameter1, parameter2);
}
/**
* Streams an infinite sequence of Payload objects, where each payload contains an incrementing
* version number (starting from 0), the user ID, session ID, parameter1 & parameter2 values
* extracted from the path, and a randomly generated unique string identifier. <br>
* The stream emits new elements at one-second intervals.
*
* @see Flux
*/
@MessageMapping("/session/{userId}/{sessionId}/stream/{parameter1}/{parameter2}")
public Flux<Payload> sessionStream(
@IngressDestinationVariable(token = IngressToken.USER_ID) String userId,
@IngressDestinationVariable(token = IngressToken.PERSISTENT_SESSION_ID) String sessionId,
@DestinationVariable String parameter1,
@DestinationVariable int parameter2) {
return createPayloadFlux(userId, sessionId, parameter1, parameter2);
}
/**
* Streams a sequence of {@link ContainerEvent}s of {@link Payload} objects.
*
* <p>This example demonstrates how to publish a DataSource container. It initially emits a {@link
* ContainerEvent.Bulk} event containing 10 rows (keys "0" through "9"). Subsequently, it emits
* random {@link RowEvent.Upsert} or {@link RowEvent.Remove} events for these rows at one-second
* intervals.
*
* @see Flux
*/
@MessageMapping("/public/container/{parameter1}/{parameter2}")
public Flux<ContainerEvent<Payload>> containerStream(
@DestinationVariable String parameter1, @DestinationVariable int parameter2) {
return createContainerFlux(null, parameter1, parameter2);
}
/**
* Streams a sequence of {@link ContainerEvent}s of {@link Payload} objects.
*
* <p>This example demonstrates how to publish a user-specific DataSource container. It initially
* emits a {@link ContainerEvent.Bulk} event containing 10 rows (keys "0" through "9").
* Subsequently, it emits random {@link RowEvent.Upsert} or {@link RowEvent.Remove} events for
* these rows at one-second intervals.
*
* @see Flux
*/
@MessageMapping("/user/{userId}/container/{parameter1}/{parameter2}")
public Flux<ContainerEvent<Payload>> userContainerStream(
@IngressDestinationVariable(token = IngressToken.USER_ID) String userId,
@DestinationVariable String parameter1,
@DestinationVariable int parameter2) {
return createContainerFlux(userId, parameter1, parameter2);
}
private Flux<Payload> createPayloadFlux(
String userId, String sessionId, String parameter1, int parameter2) {
return Flux.interval(Duration.ZERO, Duration.ofSeconds(1))
.map(
version ->
new Payload(
version.intValue(),
userId,
sessionId,
parameter1,
parameter2,
UUID.randomUUID()));
}
@SuppressWarnings("unchecked")
private Flux<ContainerEvent<Payload>> createContainerFlux(
String userId, String parameter1, int parameter2) {
Map<Integer, Integer> rows = new ConcurrentHashMap<>();
Flux<ContainerEvent<Payload>> initial =
Flux.just(
new ContainerEvent.Bulk<>(
IntStream.rangeClosed(0, 9)
.mapToObj(
row -> {
rows.put(row, 1);
return new RowEvent.Upsert<>(
String.valueOf(row),
new Payload(
0, userId, null, parameter1, parameter2, UUID.randomUUID()));
})
.collect(Collectors.toList())));
Flux<ContainerEvent<Payload>> updates =
Flux.interval(Duration.ofSeconds(1))
.map(
i -> {
int row = ThreadLocalRandom.current().nextInt(10);
if (ThreadLocalRandom.current().nextInt(3) != 2) {
int version = rows.getOrDefault(row, 0);
rows.put(row, version + 1);
return new RowEvent.Upsert<Payload>(
String.valueOf(row),
new Payload(
version, userId, null, parameter1, parameter2, UUID.randomUUID()));
} else {
rows.remove(row);
return new RowEvent.Remove(String.valueOf(row));
}
});
return Flux.concat(initial, updates);
}
}