-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(wow-core): WaitStrategy (#1123)
- Remove the static methods in the WaitingFor class and implement them in the companion object instead - Add multiple waiting strategy implementation classes to handle different command stages - Optimize the registration and handling logic of waiting strategies - Update relevant test cases
- Loading branch information
Showing
12 changed files
with
334 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
wow-core/src/main/kotlin/me/ahoo/wow/command/wait/WaitingFor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package me.ahoo.wow.command.wait | ||
|
||
import reactor.core.publisher.Mono | ||
import reactor.core.publisher.Sinks | ||
import java.util.* | ||
|
||
interface WaitingFor : WaitStrategy { | ||
val stage: CommandStage | ||
|
||
companion object { | ||
|
||
fun processed(): WaitingFor = WaitingForProcessed() | ||
|
||
fun snapshot(): WaitingFor = WaitingForSnapshot() | ||
|
||
fun projected(contextName: String, processorName: String = ""): WaitingFor = | ||
WaitingForProjected( | ||
contextName = contextName, | ||
processorName = processorName | ||
) | ||
|
||
fun eventHandled(contextName: String, processorName: String = ""): WaitingFor = | ||
WaitingForEventHandled( | ||
contextName = contextName, | ||
processorName = processorName | ||
) | ||
|
||
fun sagaHandled(contextName: String, processorName: String = ""): WaitingFor = | ||
WaitingForSagaHandled( | ||
contextName = contextName, | ||
processorName = processorName | ||
) | ||
|
||
fun stage(stage: CommandStage, contextName: String, processorName: String = ""): WaitingFor { | ||
return when (stage) { | ||
CommandStage.PROCESSED -> processed() | ||
CommandStage.SNAPSHOT -> snapshot() | ||
CommandStage.PROJECTED -> projected(contextName, processorName) | ||
CommandStage.EVENT_HANDLED -> eventHandled(contextName, processorName) | ||
CommandStage.SAGA_HANDLED -> sagaHandled(contextName, processorName) | ||
CommandStage.SENT -> throw IllegalArgumentException("Unsupported stage: $stage") | ||
} | ||
} | ||
|
||
fun stage(stage: String, contextName: String, processorName: String = ""): WaitingFor = | ||
stage( | ||
stage = CommandStage.valueOf(stage.uppercase(Locale.getDefault())), | ||
contextName = contextName, | ||
processorName = processorName | ||
) | ||
} | ||
} | ||
|
||
abstract class AbstractWaitingFor : WaitingFor { | ||
|
||
protected val sink: Sinks.One<WaitSignal> = Sinks.one() | ||
|
||
override fun waiting(): Mono<WaitSignal> { | ||
return sink.asMono() | ||
} | ||
|
||
override fun error(throwable: Throwable) { | ||
sink.tryEmitError(throwable) | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
wow-core/src/main/kotlin/me/ahoo/wow/command/wait/WaitingForAfterProcessed.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package me.ahoo.wow.command.wait | ||
|
||
abstract class WaitingForAfterProcessed : AbstractWaitingFor() { | ||
@Volatile | ||
private var processedSignal: WaitSignal? = null | ||
|
||
@Volatile | ||
private var waitingForSignal: WaitSignal? = null | ||
private val result: MutableMap<String, Any> = mutableMapOf() | ||
protected fun nextSignal() { | ||
val waitingForSignal = waitingForSignal | ||
if (processedSignal == null || waitingForSignal == null) { | ||
return | ||
} | ||
val mergedSignal = waitingForSignal.copyResult(result) | ||
sink.tryEmitValue(mergedSignal) | ||
} | ||
|
||
open fun isWaitingForSignal(signal: WaitSignal): Boolean { | ||
if (signal.stage != stage || !isSameBoundedContext(signal.function)) { | ||
return false | ||
} | ||
if (processorName.isBlank()) { | ||
return true | ||
} | ||
return signal.function.processorName == processorName | ||
} | ||
|
||
override fun next(signal: WaitSignal) { | ||
result.putAll(signal.result) | ||
if (signal.stage == CommandStage.PROCESSED) { | ||
processedSignal = signal | ||
if (!signal.succeeded) { | ||
sink.tryEmitValue(signal) | ||
return | ||
} | ||
} | ||
if (isWaitingForSignal(signal)) { | ||
waitingForSignal = signal | ||
} | ||
nextSignal() | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
wow-core/src/main/kotlin/me/ahoo/wow/command/wait/WaitingForEventHandled.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package me.ahoo.wow.command.wait | ||
|
||
class WaitingForEventHandled( | ||
override val contextName: String, | ||
override val processorName: String = "" | ||
) : WaitingForAfterProcessed() { | ||
override val stage: CommandStage | ||
get() = CommandStage.EVENT_HANDLED | ||
} |
27 changes: 27 additions & 0 deletions
27
wow-core/src/main/kotlin/me/ahoo/wow/command/wait/WaitingForProcessed.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package me.ahoo.wow.command.wait | ||
|
||
class WaitingForProcessed : AbstractWaitingFor() { | ||
override val stage: CommandStage | ||
get() = CommandStage.PROCESSED | ||
override val contextName: String = "" | ||
override val processorName: String = "" | ||
override fun next(signal: WaitSignal) { | ||
if (signal.stage != CommandStage.PROCESSED) { | ||
return | ||
} | ||
sink.tryEmitValue(signal) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
wow-core/src/main/kotlin/me/ahoo/wow/command/wait/WaitingForProjected.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package me.ahoo.wow.command.wait | ||
|
||
class WaitingForProjected( | ||
override val contextName: String, | ||
override val processorName: String = "" | ||
) : WaitingForAfterProcessed() { | ||
override val stage: CommandStage | ||
get() = CommandStage.PROJECTED | ||
|
||
override fun isWaitingForSignal(signal: WaitSignal): Boolean { | ||
return super.isWaitingForSignal(signal) && signal.isLastProjection | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
wow-core/src/main/kotlin/me/ahoo/wow/command/wait/WaitingForSagaHandled.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package me.ahoo.wow.command.wait | ||
|
||
class WaitingForSagaHandled( | ||
override val contextName: String, | ||
override val processorName: String = "" | ||
) : WaitingForAfterProcessed() { | ||
override val stage: CommandStage | ||
get() = CommandStage.SAGA_HANDLED | ||
} |
25 changes: 25 additions & 0 deletions
25
wow-core/src/main/kotlin/me/ahoo/wow/command/wait/WaitingForSnapshot.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package me.ahoo.wow.command.wait | ||
|
||
class WaitingForSnapshot : WaitingForAfterProcessed() { | ||
override val stage: CommandStage | ||
get() = CommandStage.SNAPSHOT | ||
override val contextName: String = "" | ||
override val processorName: String = "" | ||
|
||
override fun isWaitingForSignal(signal: WaitSignal): Boolean { | ||
return signal.stage == stage | ||
} | ||
} |
Oops, something went wrong.