-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamsController.kt
More file actions
170 lines (159 loc) · 5.88 KB
/
StreamsController.kt
File metadata and controls
170 lines (159 loc) · 5.88 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
159
160
161
162
163
164
165
166
167
168
169
170
package example
import com.caplin.integration.datasourcex.reactive.api.ContainerEvent
import com.caplin.integration.datasourcex.reactive.api.ContainerEvent.Bulk
import com.caplin.integration.datasourcex.reactive.api.ContainerEvent.RowEvent.Remove
import com.caplin.integration.datasourcex.reactive.api.ContainerEvent.RowEvent.Upsert
import com.caplin.integration.datasourcex.spring.annotations.IngressDestinationVariable
import com.caplin.integration.datasourcex.spring.annotations.IngressToken
import java.util.UUID
import kotlin.random.Random
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import org.springframework.messaging.handler.annotation.DestinationVariable
import org.springframework.messaging.handler.annotation.MessageMapping
import org.springframework.stereotype.Controller
@Controller
class StreamsController {
/**
* Streams an infinite sequence of [Payload] objects, where each payload contains an incrementing
* version number (starting from 0), the [parameter1] and [parameter2] values extracted from the
* path, and a randomly generated unique string identifier.
*
* The stream emits new elements at one-second intervals.
*
* @see Flow
*/
@MessageMapping("/public/stream/{parameter1}/{parameter2}")
fun genericStream(
@DestinationVariable parameter1: String,
@DestinationVariable parameter2: Int,
) = createPayloadFlow(null, null, parameter1, parameter2)
/**
* Streams an infinite sequence of Payload objects, where each payload contains an incrementing
* version number (starting from 0), the [userId], [parameter1] and [parameter2] values extracted
* from the path, and a randomly generated unique string identifier.
*
* The stream emits new elements at one-second intervals.
*
* @see Flow
*/
@MessageMapping("/user/{userId}/stream/{parameter1}/{parameter2}")
fun userStream(
@IngressDestinationVariable(IngressToken.USER_ID) userId: String,
@DestinationVariable parameter1: String,
@DestinationVariable parameter2: Int,
) = createPayloadFlow(userId, null, parameter1, parameter2)
/**
* Streams an infinite sequence of Payload objects, where each payload contains an incrementing
* version number (starting from 0), the [userId], [sessionId], [parameter1] and [parameter2]
* values extracted from the path, and a randomly generated unique string identifier.
*
* The stream emits new elements at one-second intervals.
*
* @see Flow
*/
@MessageMapping("/session/{userId}/{sessionId}/stream/{parameter1}/{parameter2}")
fun sessionStream(
@IngressDestinationVariable(IngressToken.USER_ID) userId: String,
@IngressDestinationVariable(IngressToken.PERSISTENT_SESSION_ID) sessionId: String,
@DestinationVariable parameter1: String,
@DestinationVariable parameter2: Int,
) = createPayloadFlow(userId, sessionId, parameter1, parameter2)
/**
* Streams a sequence of [ContainerEvent]s of [Payload] objects.
*
* This example demonstrates how to publish a DataSource container. It initially emits a [Bulk]
* event containing 10 rows (keys "0" through "9"). Subsequently, it emits random [Upsert] or
* [Remove] events for these rows at one-second intervals.
*
* @see Flow
*/
@MessageMapping("/public/container/{parameter1}/{parameter2}")
fun containerStream(
@DestinationVariable parameter1: String,
@DestinationVariable parameter2: Int,
): Flow<ContainerEvent<Payload>> = createContainerFlow(null, parameter1, parameter2)
/**
* Streams a sequence of [ContainerEvent]s of [Payload] objects.
*
* This example demonstrates how to publish a DataSource container. It initially emits a [Bulk]
* event containing 10 rows (keys "0" through "9"). Subsequently, it emits random [Upsert] or
* [Remove] events for these rows at one-second intervals.
*
* @see Flow
*/
@MessageMapping("/user/{userId}/container/{parameter1}/{parameter2}")
fun userContainerStream(
@IngressDestinationVariable(IngressToken.USER_ID) userId: String,
@DestinationVariable parameter1: String,
@DestinationVariable parameter2: Int,
): Flow<ContainerEvent<Payload>> = createContainerFlow(userId, parameter1, parameter2)
private fun createContainerFlow(userId: String?, parameter1: String, parameter2: Int) = flow {
val rows = mutableMapOf<Int, Int>()
emit(
Bulk(
(0 until 10).map { row ->
rows[row] = 1
Upsert(
row.toString(),
Payload(
0,
userId,
null,
parameter1,
parameter2,
UUID.randomUUID(),
),
)
},
),
)
while (true) {
delay(1.seconds)
val row = Random.nextInt(10)
if (Random.nextInt(3) != 2) {
val version = rows.getOrPut(row) { 0 }
emit(
Upsert(
row.toString(),
Payload(
version,
userId,
null,
parameter1,
parameter2,
UUID.randomUUID(),
),
),
)
rows[row] = version + 1
} else {
rows.remove(row)
emit(Remove(row.toString()))
}
}
}
private fun createPayloadFlow(
userId: String?,
sessionId: String?,
parameter1: String,
parameter2: Int,
) = flow {
var version = 0
while (true) {
emit(
Payload(
version = version++,
userId = userId,
sessionId = sessionId,
parameter1 = parameter1,
parameter2 = parameter2,
uuid = UUID.randomUUID(),
),
)
delay(1.seconds)
}
}
}