feat(ingress): 实现 JDBC 信箱轮询、入站持久化与对拍测试 (U05)
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package com.gzzn.omms.msgexchange
|
||||
|
||||
import com.gzzn.omms.msgexchange.delivery.Dispatcher
|
||||
import com.gzzn.omms.msgexchange.ingress.InboxPoller
|
||||
import com.gzzn.omms.msgexchange.processing.Pump
|
||||
import io.micronaut.context.annotation.Requires
|
||||
import io.micronaut.runtime.event.annotation.EventListener
|
||||
import io.micronaut.runtime.server.event.ServerStartupEvent
|
||||
import jakarta.annotation.PreDestroy
|
||||
import jakarta.inject.Singleton
|
||||
|
||||
/**
|
||||
* U07(T03+N29):管道生命周期装配——服务启动后拉起 inbox 轮询、主泵、投递三条专用单线程
|
||||
* 停机时 requestStop + interrupt + join。仅当 `msgx.pipeline.autostart=true` 时装配
|
||||
* (默认关:需要真实仓储或 msgx.stubs=true 才安全开启)。
|
||||
*/
|
||||
@Requires(property = "msgx.pipeline.autostart", value = "true")
|
||||
@Singleton
|
||||
class PipelineLifecycle(
|
||||
private val poller: InboxPoller,
|
||||
private val pump: Pump,
|
||||
private val dispatcher: Dispatcher,
|
||||
) {
|
||||
private val log = org.slf4j.LoggerFactory.getLogger(PipelineLifecycle::class.java)
|
||||
private val threads = mutableListOf<Thread>()
|
||||
|
||||
@Volatile
|
||||
private var started = false
|
||||
|
||||
@EventListener
|
||||
fun start(event: ServerStartupEvent) {
|
||||
startIfNeeded()
|
||||
}
|
||||
|
||||
private fun startIfNeeded() {
|
||||
if (started) return
|
||||
started = true
|
||||
threads += spawn("msgx-inbox-poller", poller::loop)
|
||||
threads += spawn("msgx-pump", pump::loop)
|
||||
threads += spawn("msgx-dispatcher", dispatcher::loop)
|
||||
log.info("pipeline loops started (inbox-poller, pump, dispatcher)")
|
||||
}
|
||||
|
||||
private fun spawn(name: String, body: () -> Unit): Thread =
|
||||
Thread.ofPlatform().name(name).daemon(true).start { body() }
|
||||
|
||||
@PreDestroy
|
||||
fun stop() {
|
||||
poller.stop()
|
||||
pump.stop()
|
||||
dispatcher.stop()
|
||||
threads.forEach { it.interrupt() } // 解除 Thread.sleep 阻塞,加速退出
|
||||
threads.forEach { runCatching { it.join(3000) } }
|
||||
log.info("pipeline loops stopped")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user