We have a JMS listener that pull the messages from MQ and save them in database. My question is what is the best way to pause the JMS Listener temporary while db is down and resume it again after some time.
My thought is using Spring Circuit Breaker around the db operation to monitor its health and fallback to an error handler method. This will help to avoid waiting for db timeout every time. However, the listener still is pulling the messages and try to save them which is wasting CPU and network.
Is there any way to handle the circuit breaker commands events like: open/half-open/close? Then I can write the code to stop JMS when circuit-open event happens and start JMS when circuit-half-open event happens.
Or what is the best practice to temporary pause a JMS listener when a dependency is failed?
Note: We are using Spring Integration message-driven-channel-adapter for the listener.
For those who have the same problem, after checking the code for Hystrix Circuit Breaker library, I realised that firstly there is not any mechanism to notify Circuit state changes. Secondly, even if the events were there it wouldn't solve my issue. Because, Half-Open state is reached with the first attempt (service call) after the sleep window (when the circuit is open). while, for my case when JMS Listener is stopped there won't be any next attempt/call.
Eventually, I developed a custom circuit-breaker library that starts a timer/scheduler when opening the circuit and moves the circuit to half-open state automatically when the timer (sleep window) triggered.