Commit db74d27e authored by Christian Dupuis's avatar Christian Dupuis

Extract AbstractHealthIndicator

to make it more simple to implement HealthIndicator
parent 1e6a4f3f
/*
* 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;
/**
* Base {@link HealthIndicator} implementations that encapsulates creation of
* {@link Health} instance and error handling.
*
* <p>
* This implementation is only suitable if an {@link Exception} raised from
* {@link #doHealthCheck(Health)} should create a {@link Status#DOWN} health status.
*
* @author Christian Dupuis
* @since 1.1.0
*/
public abstract class AbstractHealthIndicator implements HealthIndicator {
@Override
public final Health health() {
Health health = new Health();
try {
doHealthCheck(health);
}
catch (Exception ex) {
health.down().withException(ex);
}
return health;
}
/**
* Actual health check logic.
* @param health {@link Health} instance of report status.
* @throws Exception any {@link Exception} that should create a {@link Status#DOWN}
* system status.
*/
protected abstract void doHealthCheck(Health health) throws Exception;
}
...@@ -28,7 +28,7 @@ import com.mongodb.CommandResult; ...@@ -28,7 +28,7 @@ import com.mongodb.CommandResult;
* @author Christian Dupuis * @author Christian Dupuis
* @since 1.1.0 * @since 1.1.0
*/ */
public class MongoHealthIndicator implements HealthIndicator { public class MongoHealthIndicator extends AbstractHealthIndicator {
private final MongoTemplate mongoTemplate; private final MongoTemplate mongoTemplate;
...@@ -38,17 +38,9 @@ public class MongoHealthIndicator implements HealthIndicator { ...@@ -38,17 +38,9 @@ public class MongoHealthIndicator implements HealthIndicator {
} }
@Override @Override
public Health health() { protected void doHealthCheck(Health health) throws Exception {
Health health = new Health(); CommandResult result = this.mongoTemplate.executeCommand("{ serverStatus: 1 }");
try { health.up().withDetail("version", result.getString("version"));
CommandResult result = this.mongoTemplate
.executeCommand("{ serverStatus: 1 }");
health.up().withDetail("version", result.getString("version"));
}
catch (Exception ex) {
health.down().withException(ex);
}
return health;
} }
} }
...@@ -31,7 +31,7 @@ import com.rabbitmq.client.Channel; ...@@ -31,7 +31,7 @@ import com.rabbitmq.client.Channel;
* @author Christian Dupuis * @author Christian Dupuis
* @since 1.1.0 * @since 1.1.0
*/ */
public class RabbitHealthIndicator implements HealthIndicator { public class RabbitHealthIndicator extends AbstractHealthIndicator {
private final RabbitTemplate rabbitTemplate; private final RabbitTemplate rabbitTemplate;
...@@ -41,23 +41,17 @@ public class RabbitHealthIndicator implements HealthIndicator { ...@@ -41,23 +41,17 @@ public class RabbitHealthIndicator implements HealthIndicator {
} }
@Override @Override
public Health health() { protected void doHealthCheck(Health health) throws Exception {
Health health = new Health(); health.up().withDetail("version",
try { this.rabbitTemplate.execute(new ChannelCallback<String>() {
health.up().withDetail("version",
this.rabbitTemplate.execute(new ChannelCallback<String>() { @Override
public String doInRabbit(Channel channel) throws Exception {
@Override Map<String, Object> serverProperties = channel.getConnection()
public String doInRabbit(Channel channel) throws Exception { .getServerProperties();
Map<String, Object> serverProperties = channel return serverProperties.get("version").toString();
.getConnection().getServerProperties(); }
return serverProperties.get("version").toString(); }));
}
}));
}
catch (Exception ex) {
health.down().withException(ex);
}
return health;
} }
} }
...@@ -30,7 +30,7 @@ import org.springframework.util.Assert; ...@@ -30,7 +30,7 @@ import org.springframework.util.Assert;
* @author Christian Dupuis * @author Christian Dupuis
* @since 1.1.0 * @since 1.1.0
*/ */
public class RedisHealthIndicator implements HealthIndicator { public class RedisHealthIndicator extends AbstractHealthIndicator {
private final RedisConnectionFactory redisConnectionFactory; private final RedisConnectionFactory redisConnectionFactory;
...@@ -40,22 +40,17 @@ public class RedisHealthIndicator implements HealthIndicator { ...@@ -40,22 +40,17 @@ public class RedisHealthIndicator implements HealthIndicator {
} }
@Override @Override
public Health health() { protected void doHealthCheck(Health health) throws Exception {
Health health = new Health();
RedisConnection connection = null; RedisConnection connection = null;
try { try {
connection = RedisConnectionUtils.getConnection(this.redisConnectionFactory); connection = RedisConnectionUtils.getConnection(this.redisConnectionFactory);
Properties info = connection.info(); Properties info = connection.info();
health.up().withDetail("version", info.getProperty("redis_version")); health.up().withDetail("version", info.getProperty("redis_version"));
} }
catch (Exception ex) {
health.down().withException(ex);
}
finally { finally {
RedisConnectionUtils.releaseConnection(connection, RedisConnectionUtils.releaseConnection(connection,
this.redisConnectionFactory); this.redisConnectionFactory);
} }
return health;
} }
} }
...@@ -22,11 +22,11 @@ package org.springframework.boot.actuate.health; ...@@ -22,11 +22,11 @@ package org.springframework.boot.actuate.health;
* @author Dave Syer * @author Dave Syer
* @author Christian Dupuis * @author Christian Dupuis
*/ */
public class VanillaHealthIndicator implements HealthIndicator { public class VanillaHealthIndicator extends AbstractHealthIndicator {
@Override @Override
public Health health() { protected void doHealthCheck(Health health) throws Exception {
return new Health(Status.UP); health.up();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment