Add JMS health indicator
Define an additional health indicator for each ConnectionFactory instance defined in the context. Extracts the provider name from the connection meta-data. Fixes gh-2016
This commit is contained in:
@@ -19,10 +19,11 @@ package org.springframework.boot.actuate.autoconfigure;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.jms.ConnectionFactory;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.solr.client.solrj.SolrServer;
|
||||
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
|
||||
@@ -32,6 +33,7 @@ import org.springframework.boot.actuate.health.DiskSpaceHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.DiskSpaceHealthIndicatorProperties;
|
||||
import org.springframework.boot.actuate.health.HealthAggregator;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.boot.actuate.health.JmsHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.MailHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.MongoHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.OrderedHealthAggregator;
|
||||
@@ -49,6 +51,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadata;
|
||||
import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadataProvider;
|
||||
import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadataProviders;
|
||||
import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration;
|
||||
@@ -74,7 +77,7 @@ import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, MongoAutoConfiguration.class,
|
||||
MongoDataAutoConfiguration.class, RedisAutoConfiguration.class,
|
||||
RabbitAutoConfiguration.class, SolrAutoConfiguration.class,
|
||||
MailSenderAutoConfiguration.class })
|
||||
MailSenderAutoConfiguration.class, JmsAutoConfiguration.class})
|
||||
@EnableConfigurationProperties({ HealthIndicatorAutoConfigurationProperties.class })
|
||||
public class HealthIndicatorAutoConfiguration {
|
||||
|
||||
@@ -316,4 +319,40 @@ public class HealthIndicatorAutoConfiguration {
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnBean(ConnectionFactory.class)
|
||||
@ConditionalOnProperty(prefix = "management.health.jms", name = "enabled", matchIfMissing = true)
|
||||
public static class JmsHealthIndicatorConfiguration {
|
||||
|
||||
@Autowired
|
||||
private HealthAggregator healthAggregator;
|
||||
|
||||
@Autowired(required = false)
|
||||
private Map<String, ConnectionFactory> connectionFactories;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(name = "jmsHealthIndicator")
|
||||
public HealthIndicator jmsHealthIndicator() {
|
||||
if (this.connectionFactories.size() == 1) {
|
||||
ConnectionFactory connectionFactory = this.connectionFactories.values()
|
||||
.iterator().next();
|
||||
return createJmsHealthIndicator(connectionFactory);
|
||||
}
|
||||
CompositeHealthIndicator composite = new CompositeHealthIndicator(
|
||||
this.healthAggregator);
|
||||
for (Map.Entry<String, ConnectionFactory> entry : this.connectionFactories
|
||||
.entrySet()) {
|
||||
String name = entry.getKey();
|
||||
ConnectionFactory connectionFactory = entry.getValue();
|
||||
composite.addHealthIndicator(name, createJmsHealthIndicator(connectionFactory));
|
||||
}
|
||||
return composite;
|
||||
}
|
||||
|
||||
private JmsHealthIndicator createJmsHealthIndicator(
|
||||
ConnectionFactory connectionFactory) {
|
||||
return new JmsHealthIndicator(connectionFactory);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.actuate.health;
|
||||
|
||||
import javax.jms.Connection;
|
||||
import javax.jms.ConnectionFactory;
|
||||
|
||||
/**
|
||||
* {@link HealthIndicator} for a JMS {@link ConnectionFactory}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.3.0
|
||||
*/
|
||||
public class JmsHealthIndicator extends AbstractHealthIndicator {
|
||||
|
||||
private final ConnectionFactory connectionFactory;
|
||||
|
||||
public JmsHealthIndicator(ConnectionFactory connectionFactory) {
|
||||
this.connectionFactory = connectionFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doHealthCheck(Health.Builder builder) throws Exception {
|
||||
Connection conToClose = null;
|
||||
try {
|
||||
conToClose = this.connectionFactory.createConnection();
|
||||
builder.up().withDetail("provider",
|
||||
conToClose.getMetaData().getJMSProviderName());
|
||||
} finally {
|
||||
if (conToClose != null) {
|
||||
conToClose.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -34,6 +34,12 @@
|
||||
"description": "Enable disk space health check.",
|
||||
"defaultValue": true
|
||||
},
|
||||
{
|
||||
"name": "management.health.jms.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "Enable JMS health check.",
|
||||
"defaultValue": true
|
||||
},
|
||||
{
|
||||
"name": "management.health.mongo.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
|
||||
Reference in New Issue
Block a user