spring-boot: JmsHealthIndicator doesn't work properly for ActiveMQ failover transport
For failover transport:
spring.activemq.broker-url: failover:(tcp://localhost:61616,tcp://127.0.0.1:61616)?randomize=false&nested.daemon=true
when ActiveMQ become unavailable JmsHealthIndicator
reports UP
.
Our solution for this issue is to use TransportListener
. See http://activemq.apache.org/how-can-i-monitor-the-connection-with-the-broker.html
public class TransportInteruptionListener extends DefaultTransportListener {
private boolean interupted;
@Override
public void transportInterupted() {
interupted = true;
}
@Override
public void transportResumed() {
interupted = false;
}
public boolean isInterupted() {
return interupted;
}
}
public class CustomJmsHealthIndicatorAutoConfiguration {
@Bean
public TransportInteruptionListener daivbTransportInteruptionListener() {
return new TransportInteruptionListener();
}
@Bean
@ConditionalOnMissingBean(name = "customJmsHealthIndicator")
customJmsHealthIndicator customJmsHealthIndicator(ApplicationContext ctx, ConnectionFactory connectionFactory,
TransportInteruptionListener l) {
return new CustomJmsHealthIndicator(ctx, connectionFactory, l);
}
@Bean
public ActiveMQConnectionFactoryCustomizer customActiveMQConnectionFactoryCustomizer(TransportInteruptionListener l) {
return factory -> factory.setTransportListener(l);
}
}
public class CustomJmsHealthIndicator extends AbstractHealthIndicator {
...
protected void doHealthCheck(Health.Builder builder) throws Exception {
...
connection.start();
if (transportInteruptionListener.isInterupted()) {
throw new IllegalStateException("Transport interupted");
}
...
}
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 20 (14 by maintainers)
@anandshah123 What you’ve got looks pretty good to me. Thank you. I think we’ll need to try and test this before we could merge the changes. Would you like to take a look at that and then open a pull request? Hopefully it’ll be possible to start an appropriately configured broker, check that the health indicator reports up, stop the broker, and then check that the indicator now reports down.
Yes please, @anandshastri1990. Thanks very much for the offer. Please let us know if you have any questions.
I would like to work on this enhancement. Please let me know if I can contribute.
Thanks for the suggestion. Currently
JmsHealthIndicator
only usesjavax.jms
package imports so I think we’ll need an enhanced ActiveMQ specific version.