Add HealthIndicator for Rabbit

This commit is contained in:
Christian Dupuis 2014-05-10 11:10:29 +02:00
parent dac03fdb7b
commit b026b13c66
4 changed files with 161 additions and 2 deletions

View File

@ -111,6 +111,11 @@
<artifactId>jedis</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<optional>true</optional>
</dependency>
<!-- Test -->
<dependency>
<groupId>ch.qos.logback</groupId>

View File

@ -20,15 +20,18 @@ import java.util.Map;
import javax.sql.DataSource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.CompositeHealthIndicator;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.MongoHealthIndicator;
import org.springframework.boot.actuate.health.RabbitHealthIndicator;
import org.springframework.boot.actuate.health.RedisHealthIndicator;
import org.springframework.boot.actuate.health.SimpleDataSourceHealthIndicator;
import org.springframework.boot.actuate.health.VanillaHealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.CommonsDataSourceConfiguration;
@ -46,7 +49,7 @@ import org.springframework.data.redis.connection.RedisConnectionFactory;
/**
* {@link EnableAutoConfiguration Auto-configuration} for {@link HealthIndicator}s.
*
*
* @author Christian Dupuis
* @since 1.1.0
*/
@ -55,7 +58,7 @@ import org.springframework.data.redis.connection.RedisConnectionFactory;
EmbeddedDataSourceConfiguration.class, CommonsDataSourceConfiguration.class,
HikariDataSourceConfiguration.class, TomcatDataSourceConfiguration.class,
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
RedisAutoConfiguration.class })
RedisAutoConfiguration.class, RabbitAutoConfiguration.class })
public class HealthIndicatorAutoConfiguration {
@Bean
@ -137,4 +140,29 @@ public class HealthIndicatorAutoConfiguration {
}
}
@Configuration
@ConditionalOnBean(RabbitTemplate.class)
public static class RabbitHealthIndicatorConfiguration {
@Autowired
private Map<String, RabbitTemplate> rabbitTemplates;
@Bean
@ConditionalOnMissingBean(name = "rabbitHealthIndicator")
public HealthIndicator<?> redisHealthIndicator() {
if (this.rabbitTemplates.size() == 1) {
return new RabbitHealthIndicator(this.rabbitTemplates.values().iterator()
.next());
}
CompositeHealthIndicator composite = new CompositeHealthIndicator();
for (Map.Entry<String, RabbitTemplate> entry : this.rabbitTemplates
.entrySet()) {
composite.addHealthIndicator(entry.getKey(), new RabbitHealthIndicator(
entry.getValue()));
}
return composite;
}
}
}

View File

@ -0,0 +1,67 @@
/*
* Copyright 2012-2014 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 java.util.HashMap;
import java.util.Map;
import org.springframework.amqp.rabbit.core.ChannelCallback;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.util.Assert;
import com.rabbitmq.client.Channel;
/**
* Simple implementation of a {@link HealthIndicator} returning status information for the
* RabbitMQ messaging system.
*
* @author Christian Dupuis
* @since 1.1.0
*/
public class RabbitHealthIndicator implements HealthIndicator<Map<String, Object>> {
private final RabbitTemplate rabbitTemplate;
public RabbitHealthIndicator(RabbitTemplate rabbitTemplate) {
Assert.notNull(rabbitTemplate, "RabbitTemplate must not be null.");
this.rabbitTemplate = rabbitTemplate;
}
@Override
public Map<String, Object> health() {
Map<String, Object> health = new HashMap<String, Object>();
try {
health.put("version",
this.rabbitTemplate.execute(new ChannelCallback<String>() {
@Override
public String doInRabbit(Channel channel) throws Exception {
Map<String, Object> serverProperties = channel
.getConnection().getServerProperties();
return serverProperties.get("version").toString();
}
}));
health.put("status", "ok");
}
catch (Exception ex) {
health.put("status", "error");
health.put("error", ex.getClass().getName() + ": " + ex.getMessage());
}
return health;
}
}

View File

@ -0,0 +1,59 @@
/*
* Copyright 2014 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 org.junit.After;
import org.junit.Test;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* Tests for {@link RabbitHealthIndicator}.
*
* @author Christian Dupuis
*/
public class RabbitHealthIndicatorTests {
private AnnotationConfigApplicationContext context;
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void indicatorExists() {
this.context = new AnnotationConfigApplicationContext(
PropertyPlaceholderAutoConfiguration.class,
RabbitAutoConfiguration.class, EndpointAutoConfiguration.class,
HealthIndicatorAutoConfiguration.class);
assertEquals(1, this.context.getBeanNamesForType(RabbitAdmin.class).length);
RabbitHealthIndicator healthIndicator = this.context
.getBean(RabbitHealthIndicator.class);
assertNotNull(healthIndicator);
}
}