Skip to content

Commit 51a26a4

Browse files
committed
Polish
1 parent 0a756b5 commit 51a26a4

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/redis/RedisHealthIndicator.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.actuate.redis;
1818

19-
import java.util.Properties;
20-
2119
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
2220
import org.springframework.boot.actuate.health.Health;
2321
import org.springframework.boot.actuate.health.HealthIndicator;
@@ -38,9 +36,7 @@
3836
*/
3937
public class RedisHealthIndicator extends AbstractHealthIndicator {
4038

41-
static final String VERSION = "version";
42-
43-
static final String REDIS_VERSION = "redis_version";
39+
private static final String REDIS_VERSION_PROPERTY = "redis_version";
4440

4541
private final RedisConnectionFactory redisConnectionFactory;
4642

@@ -54,20 +50,24 @@ public RedisHealthIndicator(RedisConnectionFactory connectionFactory) {
5450
protected void doHealthCheck(Health.Builder builder) throws Exception {
5551
RedisConnection connection = RedisConnectionUtils.getConnection(this.redisConnectionFactory);
5652
try {
57-
if (connection instanceof RedisClusterConnection) {
58-
ClusterInfo clusterInfo = ((RedisClusterConnection) connection).clusterGetClusterInfo();
59-
builder.up().withDetail("cluster_size", clusterInfo.getClusterSize())
60-
.withDetail("slots_up", clusterInfo.getSlotsOk())
61-
.withDetail("slots_fail", clusterInfo.getSlotsFail());
62-
}
63-
else {
64-
Properties info = connection.info();
65-
builder.up().withDetail(VERSION, info.getProperty(REDIS_VERSION));
66-
}
53+
doHealthCheck(builder, connection);
6754
}
6855
finally {
6956
RedisConnectionUtils.releaseConnection(connection, this.redisConnectionFactory, false);
7057
}
7158
}
7259

60+
private void doHealthCheck(Health.Builder builder, RedisConnection connection) {
61+
if (connection instanceof RedisClusterConnection) {
62+
ClusterInfo clusterInfo = ((RedisClusterConnection) connection).clusterGetClusterInfo();
63+
builder.up().withDetail("cluster_size", clusterInfo.getClusterSize())
64+
.withDetail("slots_up", clusterInfo.getSlotsOk())
65+
.withDetail("slots_fail", clusterInfo.getSlotsFail());
66+
}
67+
else {
68+
String version = connection.info().getProperty(REDIS_VERSION_PROPERTY);
69+
builder.up().withDetail("version", version);
70+
}
71+
}
72+
7373
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/redis/RedisReactiveHealthIndicator.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
*/
4040
public class RedisReactiveHealthIndicator extends AbstractReactiveHealthIndicator {
4141

42+
private static final String REDIS_VERSION_PROPERTY = "redis_version";
43+
4244
private final ReactiveRedisConnectionFactory connectionFactory;
4345

4446
public RedisReactiveHealthIndicator(ReactiveRedisConnectionFactory connectionFactory) {
@@ -52,8 +54,8 @@ protected Mono<Health> doHealthCheck(Health.Builder builder) {
5254
}
5355

5456
private Mono<Health> doHealthCheck(Health.Builder builder, ReactiveRedisConnection connection) {
55-
return connection.serverCommands().info()
56-
.map((info) -> up(builder, info, (connection instanceof ReactiveRedisClusterConnection)))
57+
boolean isClusterConnection = connection instanceof ReactiveRedisClusterConnection;
58+
return connection.serverCommands().info().map((info) -> up(builder, info, isClusterConnection))
5759
.onErrorResume((ex) -> Mono.just(down(builder, ex)))
5860
.flatMap((health) -> connection.closeLater().thenReturn(health));
5961
}
@@ -64,24 +66,22 @@ private Mono<ReactiveRedisConnection> getConnection() {
6466
}
6567

6668
private Health up(Health.Builder builder, Properties info, boolean isClusterConnection) {
67-
if (isClusterConnection) {
68-
return builder.up().withDetail(RedisHealthIndicator.VERSION, getClusterVersionProperty(info)).build();
69-
}
70-
else {
71-
return builder.up()
72-
.withDetail(RedisHealthIndicator.VERSION, info.getProperty(RedisHealthIndicator.REDIS_VERSION))
73-
.build();
74-
}
75-
}
76-
77-
private Object getClusterVersionProperty(Properties info) {
78-
return info.keySet().stream().map(String.class::cast)
79-
.filter((key) -> key.endsWith(RedisHealthIndicator.REDIS_VERSION)).findFirst().map(info::get)
80-
.orElse("");
69+
String version = isClusterConnection ? getClusterVersionProperty(info)
70+
: info.getProperty(REDIS_VERSION_PROPERTY);
71+
return builder.up().withDetail("version", version).build();
8172
}
8273

8374
private Health down(Health.Builder builder, Throwable cause) {
8475
return builder.down(cause).build();
8576
}
8677

78+
private String getClusterVersionProperty(Properties info) {
79+
for (String propertyName : info.stringPropertyNames()) {
80+
if (propertyName.endsWith(REDIS_VERSION_PROPERTY)) {
81+
return info.getProperty(propertyName);
82+
}
83+
}
84+
return "";
85+
}
86+
8787
}

0 commit comments

Comments
 (0)