Skip to content

Commit 963a544

Browse files
committed
Merge branch '2.1.x'
Closes gh-16860
2 parents 3a9ca5f + e0e9c4a commit 963a544

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.util.Properties;
2020

2121
import reactor.core.publisher.Mono;
22+
import reactor.core.scheduler.Schedulers;
2223

2324
import org.springframework.boot.actuate.health.AbstractReactiveHealthIndicator;
2425
import org.springframework.boot.actuate.health.Health;
@@ -31,6 +32,7 @@
3132
*
3233
* @author Stephane Nicoll
3334
* @author Mark Paluch
35+
* @author Artsiom Yudovin
3436
* @since 2.0.0
3537
*/
3638
public class RedisReactiveHealthIndicator extends AbstractReactiveHealthIndicator {
@@ -44,15 +46,29 @@ public RedisReactiveHealthIndicator(
4446

4547
@Override
4648
protected Mono<Health> doHealthCheck(Health.Builder builder) {
47-
ReactiveRedisConnection connection = this.connectionFactory
48-
.getReactiveConnection();
49+
return getConnection()
50+
.flatMap((connection) -> doHealthCheck(builder, connection));
51+
}
52+
53+
private Mono<Health> doHealthCheck(Health.Builder builder,
54+
ReactiveRedisConnection connection) {
4955
return connection.serverCommands().info().map((info) -> up(builder, info))
50-
.doFinally((signal) -> connection.close());
56+
.onErrorResume((ex) -> Mono.just(down(builder, ex)))
57+
.flatMap((health) -> connection.closeLater().thenReturn(health));
58+
}
59+
60+
private Mono<ReactiveRedisConnection> getConnection() {
61+
return Mono.fromSupplier(this.connectionFactory::getReactiveConnection)
62+
.subscribeOn(Schedulers.parallel());
5163
}
5264

5365
private Health up(Health.Builder builder, Properties info) {
5466
return builder.up().withDetail(RedisHealthIndicator.VERSION,
5567
info.getProperty(RedisHealthIndicator.REDIS_VERSION)).build();
5668
}
5769

70+
private Health down(Health.Builder builder, Throwable cause) {
71+
return builder.down(cause).build();
72+
}
73+
5874
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/redis/RedisReactiveHealthIndicatorTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* @author Stephane Nicoll
4242
* @author Mark Paluch
4343
* @author Nikolay Rybak
44+
* @author Artsiom Yudovin
4445
*/
4546
public class RedisReactiveHealthIndicatorTests {
4647

@@ -49,6 +50,7 @@ public void redisIsUp() {
4950
Properties info = new Properties();
5051
info.put("redis_version", "2.8.9");
5152
ReactiveRedisConnection redisConnection = mock(ReactiveRedisConnection.class);
53+
given(redisConnection.closeLater()).willReturn(Mono.empty());
5254
ReactiveServerCommands commands = mock(ReactiveServerCommands.class);
5355
given(commands.info()).willReturn(Mono.just(info));
5456
RedisReactiveHealthIndicator healthIndicator = createHealthIndicator(
@@ -59,7 +61,7 @@ public void redisIsUp() {
5961
assertThat(h.getDetails()).containsOnlyKeys("version");
6062
assertThat(h.getDetails().get("version")).isEqualTo("2.8.9");
6163
}).verifyComplete();
62-
verify(redisConnection).close();
64+
verify(redisConnection).closeLater();
6365
}
6466

6567
@Test
@@ -68,13 +70,14 @@ public void redisCommandIsDown() {
6870
given(commands.info()).willReturn(
6971
Mono.error(new RedisConnectionFailureException("Connection failed")));
7072
ReactiveRedisConnection redisConnection = mock(ReactiveRedisConnection.class);
73+
given(redisConnection.closeLater()).willReturn(Mono.empty());
7174
RedisReactiveHealthIndicator healthIndicator = createHealthIndicator(
7275
redisConnection, commands);
7376
Mono<Health> health = healthIndicator.health();
7477
StepVerifier.create(health)
7578
.consumeNextWith((h) -> assertThat(h.getStatus()).isEqualTo(Status.DOWN))
7679
.verifyComplete();
77-
verify(redisConnection).close();
80+
verify(redisConnection).closeLater();
7881
}
7982

8083
@Test

0 commit comments

Comments
 (0)