当目标系统关闭时停止 Spring Cloud Stream @StreamListener 的监听 [英] Stop Spring Cloud Stream @StreamListener from listening when target system is down
问题描述
我有一个应用程序,它从 Kafka 获取消息并调用目标系统来更新旧的 Oracle 数据库.
I have an application that gets messages from Kafka and calls a target system to update a legacy Oracle DB.
我想启用一个场景,如果目标系统停机,将消息留在 Kafka 总线上,并且在给定的时间段内不处理它们.我正在考虑一些基于断路器 Hystrix 的解决方案,但我找不到任何机制来告诉 Spring Cloud Stream停止"事件侦听.我能想到的唯一另一种选择是,如果断路器打开,将这些消息传输到错误/重新处理主题,但这对我来说听起来像是一种反模式.我应该能够暂停系统处理事件,这就是微服务应用中发布/订阅的全部优势.
I want to enable a scenario where if the target system is down, to leave the messages on Kafka bus and not process them for a given period of time. I was thinking of some Circuit-breaker Hystrix-based solution, but I can't find any mechanism to tell Spring Cloud Stream to "stop" the event listening. The only other alternative I can think of is if the circuit breaker is open, to transfer these messages to an error/reprocess topic, but that sounds like an anti-pattern to me. I should be able to just pause the system from handling events, that's the whole advantage of pub/sub in a micro services app.
如有任何帮助,我们将不胜感激.
Any help would be appriciated.
推荐答案
一种解决方案是自动连接应用程序上下文.
One solution is to auto wire the application context.
@Autowired
private ConfigurableApplicationContext context;
您可以stop()
和start()
上下文.
您不应该在调用 @StreamListener
的线程上调用 stop()
,否则停止将被延迟(因为容器将等待该线程默认退出 5 秒 - 至少使用 Rabbit 活页夹).
You should not call stop()
on the thread that invokes the @StreamListener
though, or the stop will be delayed (because the container will wait for that thread to exit for 5 seconds by default - with a Rabbit binder at least).
当然,您将需要某种带外机制来重新启动 - 可能是 JMX 或侦听某种控制主题的单独应用程序上下文.
Of course, you will need some kind of out-of-band mechanism to restart - perhaps JMX or a separate application context listening on some kind of control topic.
这篇关于当目标系统关闭时停止 Spring Cloud Stream @StreamListener 的监听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!