当目标系统关闭时,阻止Spring Cloud Stream @StreamListener监听 [英] Stop Spring Cloud Stream @StreamListener from listening when target system is down

查看:522
本文介绍了当目标系统关闭时,阻止Spring Cloud Stream @StreamListener监听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,可以从Kafka获取消息并调用目标系统来更新旧版Oracle DB.

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停止"事件监听.我能想到的唯一其他选择是,如果断路器是断开的,则将这些消息传送到错误/重新处理主题,但这对我来说听起来像是一种反模式.我应该能够使系统暂停处理事件,这就是微服务应用程序中pub/sub的全部优点.

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.

将寻求任何帮助.

推荐答案

一种解决方案是自动关联应用程序上下文.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆