@@ -18,34 +18,29 @@ abstract class AbstractTransactionDispatcher(
18
18
private val codec : Codec ,
19
19
) {
20
20
21
- private val monoTransactionHandleFunctions =
22
- mutableMapOf<TransactionState , MutableList <MonoFunction >>()
23
-
24
- private val notPublisherTransactionHandlerFunctions =
25
- mutableMapOf<TransactionState , MutableList <NotPublisherFunction >>()
21
+ private val functions =
22
+ mutableMapOf<TransactionState , MutableList <AbstractDispatchFunction <* >>>()
23
+
24
+ fun dispatch (transaction : Transaction , messageId : String ): Mono <String > {
25
+ return Flux .fromIterable(functions[transaction.state] ? : listOf ())
26
+ .flatMap { function ->
27
+ when (function) {
28
+ is MonoDispatchFunction -> {
29
+ mapToTransactionEvent(transaction)
30
+ .callMono(function)
31
+ .warningOnError(" Error occurred in TransactionHandler function \" ${function.name()} \" with transaction id ${transaction.id} " )
32
+ }
26
33
27
- fun dispatch (transaction : Transaction , messageId : String ): Boolean {
28
- var isSuccess = true
29
- if (notPublisherTransactionHandlerFunctions.isEmpty()) {
30
- dispatchToMonoHandler(transaction)
31
- .subscribeOn(Schedulers .boundedElastic())
32
- .ackWhenComplete(transaction, messageId)
33
- .subscribe({ isSuccess = true }, { isSuccess = false })
34
- return isSuccess
35
- }
36
- if (monoTransactionHandleFunctions.isEmpty()) {
37
- dispatchToNotPublisherHandler(transaction)
38
- .subscribeOn(Schedulers .boundedElastic())
39
- .ackWhenComplete(transaction, messageId)
40
- .subscribe({ isSuccess = true }, { isSuccess = false })
41
- return isSuccess
42
- }
43
- dispatchToMonoHandler(transaction)
44
- .flatMap { dispatchToNotPublisherHandler(transaction) }
34
+ is NotPublishDispatchFunction -> {
35
+ mapToTransactionEvent(transaction)
36
+ .callNotPublish(function)
37
+ .warningOnError(" Error occurred in TransactionHandler function \" ${function.name()} \" with transaction id ${transaction.id} " )
38
+ }
39
+ }
40
+ }
45
41
.subscribeOn(Schedulers .boundedElastic())
46
42
.ackWhenComplete(transaction, messageId)
47
- .subscribe({ isSuccess = true }, { isSuccess = false })
48
- return isSuccess
43
+ .then(Mono .just(DISPATHCED ))
49
44
}
50
45
51
46
private fun Flux <* >.ackWhenComplete (
@@ -62,32 +57,6 @@ abstract class AbstractTransactionDispatcher(
62
57
.subscribe()
63
58
}
64
59
65
- private fun dispatchToMonoHandler (transaction : Transaction ): Flux <Any > {
66
- return Mono .just(transaction.state)
67
- .flatMapMany { state ->
68
- Flux .fromIterable(monoTransactionHandleFunctions[state] ? : listOf ())
69
- }
70
- .publishOn(Schedulers .boundedElastic())
71
- .flatMap { monoFunction ->
72
- mapToTransactionEvent(transaction)
73
- .flatMap { monoFunction.call(it) }
74
- .warningOnError(" Error occurred in TransactionHandler function \" ${monoFunction.name()} \" with transaction \n {\n $transaction }" )
75
- }
76
- }
77
-
78
- private fun dispatchToNotPublisherHandler (transaction : Transaction ): Flux <* > {
79
- return Mono .just(transaction.state)
80
- .flatMapMany { state ->
81
- Flux .fromIterable(notPublisherTransactionHandlerFunctions[state] ? : listOf ())
82
- }
83
- .publishOn(Schedulers .boundedElastic())
84
- .flatMap { notPublisherFunction ->
85
- mapToTransactionEvent(transaction)
86
- .map { notPublisherFunction.call(it) }
87
- .warningOnError(" Error occurred in TransactionHandler function \" ${notPublisherFunction.name()} \" with transaction \n {\n $transaction }" )
88
- }
89
- }
90
-
91
60
private fun mapToTransactionEvent (transaction : Transaction ): Mono <TransactionEvent > {
92
61
return when (transaction.state) {
93
62
TransactionState .TRANSACTION_STATE_START -> Mono .just(
@@ -153,19 +122,18 @@ abstract class AbstractTransactionDispatcher(
153
122
@PostConstruct
154
123
fun initHandler () {
155
124
val transactionHandler = findHandlers(TransactionHandler ::class )
156
- val monoFunctions = getMonoFunctions(transactionHandler)
157
- monoTransactionHandleFunctions.putAll(monoFunctions)
158
- val notPublisherFunctions = getNotPublisherFunctions(transactionHandler)
159
- notPublisherTransactionHandlerFunctions.putAll(notPublisherFunctions)
125
+ initMonoFunctions(transactionHandler)
126
+ initNotPublisherFunctions(transactionHandler)
127
+ functions.forEach { (_, notPublisherFunction) ->
128
+ val notPublisherFunctionNames = notPublisherFunction.map { it.name() }.toList()
129
+ info(" Register functions names : \" ${notPublisherFunctionNames} \" " )
130
+ }
160
131
}
161
132
162
133
@Suppress(" UNCHECKED_CAST" )
163
- private fun getMonoFunctions (
134
+ private fun initMonoFunctions (
164
135
foundHandlers : List <Any >,
165
- ): MutableMap <TransactionState , MutableList <MonoFunction >> {
166
- val handlers =
167
- mutableMapOf<TransactionState , MutableList <MonoFunction >>()
168
-
136
+ ) {
169
137
for (handler in foundHandlers) {
170
138
val returnTypeMatchedHandlers = handler::class .declaredMemberFunctions
171
139
.filter { it.returnType.classifier == Mono ::class }
@@ -176,25 +144,27 @@ abstract class AbstractTransactionDispatcher(
176
144
runCatching {
177
145
val transactionState = getMatchedTransactionState(annotation)
178
146
val eventType = getEventType(annotation)
179
- handlers.putIfAbsent(transactionState, mutableListOf ())
180
- handlers[transactionState]?.add(
181
- MonoFunction (eventType, function as KFunction <Mono <* >>, handler)
147
+ val noRetryFor = getNoRetryFor(annotation)
148
+ functions.putIfAbsent(transactionState, mutableListOf ())
149
+ functions[transactionState]?.add(
150
+ MonoDispatchFunction (
151
+ eventType,
152
+ function as KFunction <Mono <* >>,
153
+ handler,
154
+ noRetryFor,
155
+ )
182
156
)
183
157
}.onFailure {
184
158
throw IllegalStateException (" Cannot add Mono TransactionHandler" , it)
185
159
}
186
160
}
187
161
}
188
162
}
189
-
190
- return handlers
191
163
}
192
164
193
- private fun getNotPublisherFunctions (
165
+ private fun initNotPublisherFunctions (
194
166
foundHandlers : List <Any >
195
- ): MutableMap <TransactionState , MutableList <NotPublisherFunction >> {
196
- val handlers =
197
- mutableMapOf<TransactionState , MutableList <NotPublisherFunction >>()
167
+ ) {
198
168
199
169
for (handler in foundHandlers) {
200
170
val returnTypeMatchedHandlers = handler::class .declaredMemberFunctions
@@ -206,18 +176,17 @@ abstract class AbstractTransactionDispatcher(
206
176
runCatching {
207
177
val transactionState = getMatchedTransactionState(annotation)
208
178
val eventType = getEventType(annotation)
209
- handlers.putIfAbsent(transactionState, mutableListOf ())
210
- handlers[transactionState]?.add(
211
- NotPublisherFunction (eventType, function, handler)
179
+ val noRetryFor = getNoRetryFor(annotation)
180
+ functions.putIfAbsent(transactionState, mutableListOf ())
181
+ functions[transactionState]?.add(
182
+ NotPublishDispatchFunction (eventType, function, handler, noRetryFor)
212
183
)
213
184
}.onFailure {
214
185
throw IllegalStateException (" Cannot add TransactionHandler" , it)
215
186
}
216
187
}
217
188
}
218
189
}
219
-
220
- return handlers
221
190
}
222
191
223
192
protected abstract fun <T : Annotation > findHandlers (type : KClass <T >): List <Any >
@@ -232,6 +201,16 @@ abstract class AbstractTransactionDispatcher(
232
201
}
233
202
}
234
203
204
+ private fun getNoRetryFor (annotation : Annotation ): Array <KClass <out Throwable >> {
205
+ return when (annotation) {
206
+ is TransactionStartListener -> annotation.noRetryFor
207
+ is TransactionCommitListener -> annotation.noRetryFor
208
+ is TransactionJoinListener -> annotation.noRetryFor
209
+ is TransactionRollbackListener -> annotation.noRetryFor
210
+ else -> throw notMatchedTransactionHandlerException
211
+ }
212
+ }
213
+
235
214
private fun getMatchedTransactionState (annotation : Annotation ): TransactionState {
236
215
return when (annotation) {
237
216
is TransactionStartListener -> TransactionState .TRANSACTION_STATE_START
@@ -242,65 +221,14 @@ abstract class AbstractTransactionDispatcher(
242
221
}
243
222
}
244
223
245
- private class MonoFunction (
246
- private val eventType : KClass <* >,
247
- private val function : KFunction <Mono <* >>,
248
- private val handler : Any ,
249
- ) {
250
-
251
- fun name (): String = function.name
252
- fun call (transactionEvent : TransactionEvent ): Mono <* > {
253
- runCatching { transactionEvent.decodeEvent(eventType) }
254
- .fold(
255
- onSuccess = {
256
- return function.call(handler, transactionEvent)
257
- .info(" Call Mono TransactionHandler \" ${name()} \" with transactionId \" ${transactionEvent.transactionId} \" " )
258
- },
259
- onFailure = {
260
- if (it is NullPointerException && eventType == Any ::class ) {
261
- return function.call(handler, transactionEvent)
262
- .info(" Call Mono TransactionHandler \" ${name()} \" with transactionId \" ${transactionEvent.transactionId} \" " )
263
- }
264
- }
265
- )
266
- return Mono .empty<String >()
267
- }
268
- }
269
-
270
- private class NotPublisherFunction (
271
- private val eventType : KClass <* >,
272
- private val function : KFunction <* >,
273
- private val handler : Any ,
274
- ) {
275
- fun name (): String = function.name
276
-
277
- fun call (transactionEvent : TransactionEvent ): Any? {
278
- runCatching {
279
- transactionEvent.decodeEvent(eventType)
280
- }.fold(
281
- onSuccess = {
282
- val result = function.call(handler, transactionEvent)
283
- info(" Call NotPublisher TransactionHandler \" ${name()} \" with transactionId \" ${transactionEvent.transactionId} \" " )
284
- return result
285
- },
286
- onFailure = {
287
- if (it is NullPointerException && eventType == Any ::class ) {
288
- val result = function.call(handler, transactionEvent)
289
- info(" Call NotPublisher TransactionHandler \" ${name()} \" with transactionId \" ${transactionEvent.transactionId} \" " )
290
- return result
291
- }
292
- }
293
- )
294
- return Unit
295
- }
296
- }
297
-
298
224
protected abstract fun ack (
299
225
transaction : Transaction ,
300
226
messageId : String
301
227
): Mono <Pair <Transaction , String >>
302
228
303
229
private companion object {
230
+ private const val DISPATHCED = " dispatched"
231
+
304
232
private val cannotFindMatchedTransactionEventException =
305
233
java.lang.IllegalStateException (" Cannot find matched transaction event" )
306
234
0 commit comments