2026-09-07 15:11:33 +08:00
|
|
|
package com.gzzn.omms.msgexchange
|
2026-09-06 20:53:14 +08:00
|
|
|
|
2026-09-07 15:11:33 +08:00
|
|
|
import com.gzzn.omms.msgexchange.delivery.Dispatcher
|
|
|
|
|
import com.gzzn.omms.msgexchange.ingress.InboxPoller
|
|
|
|
|
import com.gzzn.omms.msgexchange.processing.Pump
|
2026-09-06 20:53:14 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-07 15:11:33 +08:00
|
|
|
* U07(T03+N29):管道生命周期装配——服务启动后拉起 inbox 轮询、主泵、投递三条专用单线程
|
2026-09-06 20:53:14 +08:00
|
|
|
* 停机时 requestStop + interrupt + join。仅当 `msgx.pipeline.autostart=true` 时装配
|
|
|
|
|
* (默认关:需要真实仓储或 msgx.stubs=true 才安全开启)。
|
|
|
|
|
*/
|
|
|
|
|
@Requires(property = "msgx.pipeline.autostart", value = "true")
|
|
|
|
|
@Singleton
|
|
|
|
|
class PipelineLifecycle(
|
2026-09-07 15:11:33 +08:00
|
|
|
private val poller: InboxPoller,
|
2026-09-06 20:53:14 +08:00
|
|
|
private val pump: Pump,
|
|
|
|
|
private val dispatcher: Dispatcher,
|
|
|
|
|
) {
|
2026-09-06 21:02:27 +08:00
|
|
|
private val log = org.slf4j.LoggerFactory.getLogger(PipelineLifecycle::class.java)
|
2026-09-06 20:53:14 +08:00
|
|
|
private val threads = mutableListOf<Thread>()
|
|
|
|
|
|
|
|
|
|
@Volatile
|
|
|
|
|
private var started = false
|
|
|
|
|
|
|
|
|
|
@EventListener
|
|
|
|
|
fun start(event: ServerStartupEvent) {
|
|
|
|
|
startIfNeeded()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun startIfNeeded() {
|
|
|
|
|
if (started) return
|
|
|
|
|
started = true
|
2026-09-07 15:11:33 +08:00
|
|
|
threads += spawn("msgx-inbox-poller", poller::loop)
|
2026-09-06 20:53:14 +08:00
|
|
|
threads += spawn("msgx-pump", pump::loop)
|
|
|
|
|
threads += spawn("msgx-dispatcher", dispatcher::loop)
|
2026-09-07 15:11:33 +08:00
|
|
|
log.info("pipeline loops started (inbox-poller, pump, dispatcher)")
|
2026-09-06 20:53:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun spawn(name: String, body: () -> Unit): Thread =
|
|
|
|
|
Thread.ofPlatform().name(name).daemon(true).start { body() }
|
|
|
|
|
|
|
|
|
|
@PreDestroy
|
|
|
|
|
fun stop() {
|
2026-09-07 15:11:33 +08:00
|
|
|
poller.stop()
|
2026-09-06 20:53:14 +08:00
|
|
|
pump.stop()
|
|
|
|
|
dispatcher.stop()
|
|
|
|
|
threads.forEach { it.interrupt() } // 解除 Thread.sleep 阻塞,加速退出
|
|
|
|
|
threads.forEach { runCatching { it.join(3000) } }
|
2026-09-06 21:02:27 +08:00
|
|
|
log.info("pipeline loops stopped")
|
2026-09-06 20:53:14 +08:00
|
|
|
}
|
|
|
|
|
}
|