问题
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