Stop Spring Cloud Stream @StreamListener from listening when target system is down

拥有回忆 提交于 2019-12-23 17:27:40

问题


I have an application that gets messages from Kafka and calls a target system to update a legacy Oracle DB.

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.


回答1:


One solution is to auto wire the application context.

@Autowired
private ConfigurableApplicationContext context;

You can stop() and start() the context.

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).

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.



来源:https://stackoverflow.com/questions/39598943/stop-spring-cloud-stream-streamlistener-from-listening-when-target-system-is-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!