2 回答

TA貢獻1828條經(jīng)驗 獲得超13個贊
在 doStart 方法中設(shè)置無限循環(huán)并不是一個好主意,您將在其中劫持當前線程,然后該線程將永遠不會終止。相反,您應該設(shè)置一個運行此作業(yè)的后臺線程,并且您可以從 doStart 設(shè)置此線程并讓它運行。換句話說,組件“接收”消息的方式是 100% 特定于組件的,因為每個組件都有自己的方式。然后在 doStop 方法中,您有邏輯來停止該后臺線程并清理您的任何資源。

TA貢獻1804條經(jīng)驗 獲得超7個贊
是的,因為我們必須完成consumer的構(gòu)造函數(shù),并將接收消息的邏輯寫在doStart()
class SoroushBotConsumer (private val endpoint: MyEndpoint, processor: Processor) : DefaultConsumer(endpoint, processor) {
val objectMapper:ObjectMapper = ObjectMapper();
override fun doStart() {
val client = ClientBuilder.newBuilder().register(SseFeature::class.java).build()
val target = client.target("MY_URL"))
while(true){
var e: EventInput? target.request().get(EventInput::class.java)!!
val inboundEvent = e.read()
val exchange = endpoint.createExchange()
exchange.getIn().body = objectMapper.readValue(inboundEvent.rawData,MessageModel::class.java)
try {
processor.process(exchange)
} catch (e: Exception) {
if (exchange.exception != null) {
exceptionHandler.handleException("Error processing exchange",exchange, exchange.exception)
}
}
}
}
}
添加回答
舉報