Skip to content

Commit 20ff0d9

Browse files
Mikalai Lushchytskisnicoll
authored andcommitted
Add reactive health indicator for Couchbase
See gh-13926
1 parent 36e2c8b commit 20ff0d9

File tree

8 files changed

+466
-39
lines changed

8 files changed

+466
-39
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/couchbase/CouchbaseHealthIndicatorAutoConfiguration.java

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,20 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.couchbase;
1818

19-
import java.util.Map;
20-
2119
import com.couchbase.client.java.Bucket;
2220

23-
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthIndicatorConfiguration;
2421
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
2522
import org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorAutoConfiguration;
2623
import org.springframework.boot.actuate.couchbase.CouchbaseHealthIndicator;
27-
import org.springframework.boot.actuate.health.HealthIndicator;
2824
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2925
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
3026
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
31-
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
3227
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
33-
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
28+
import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration;
3429
import org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration;
35-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
36-
import org.springframework.context.annotation.Bean;
30+
import org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveDataAutoConfiguration;
3731
import org.springframework.context.annotation.Configuration;
38-
import org.springframework.data.couchbase.core.CouchbaseOperations;
32+
import org.springframework.context.annotation.Import;
3933

4034
/**
4135
* {@link EnableAutoConfiguration Auto-configuration} for
@@ -46,37 +40,14 @@
4640
* @since 2.0.0
4741
*/
4842
@Configuration
49-
@ConditionalOnClass({ CouchbaseOperations.class, Bucket.class })
50-
@ConditionalOnBean(CouchbaseOperations.class)
43+
@ConditionalOnClass(Bucket.class)
5144
@ConditionalOnEnabledHealthIndicator("couchbase")
5245
@AutoConfigureBefore(HealthIndicatorAutoConfiguration.class)
53-
@AutoConfigureAfter(CouchbaseDataAutoConfiguration.class)
54-
@EnableConfigurationProperties(CouchbaseHealthIndicatorProperties.class)
55-
public class CouchbaseHealthIndicatorAutoConfiguration extends
56-
CompositeHealthIndicatorConfiguration<CouchbaseHealthIndicator, CouchbaseOperations> {
57-
58-
private final Map<String, CouchbaseOperations> couchbaseOperations;
59-
60-
private final CouchbaseHealthIndicatorProperties properties;
61-
62-
public CouchbaseHealthIndicatorAutoConfiguration(
63-
Map<String, CouchbaseOperations> couchbaseOperations,
64-
CouchbaseHealthIndicatorProperties properties) {
65-
this.couchbaseOperations = couchbaseOperations;
66-
this.properties = properties;
67-
}
68-
69-
@Bean
70-
@ConditionalOnMissingBean(name = "couchbaseHealthIndicator")
71-
public HealthIndicator couchbaseHealthIndicator() {
72-
return createHealthIndicator(this.couchbaseOperations);
73-
}
74-
75-
@Override
76-
protected CouchbaseHealthIndicator createHealthIndicator(
77-
CouchbaseOperations couchbaseOperations) {
78-
return new CouchbaseHealthIndicator(couchbaseOperations,
79-
this.properties.getTimeout());
80-
}
46+
@AutoConfigureAfter({ CouchbaseAutoConfiguration.class,
47+
CouchbaseDataAutoConfiguration.class,
48+
CouchbaseReactiveDataAutoConfiguration.class })
49+
@Import({ CouchbaseHealthIndicatorConfiguration.class,
50+
CouchbaseReactiveHealthIndicatorConfiguration.class })
51+
public class CouchbaseHealthIndicatorAutoConfiguration {
8152

8253
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.boot.actuate.autoconfigure.couchbase;
17+
18+
import java.util.Map;
19+
20+
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthIndicatorConfiguration;
21+
import org.springframework.boot.actuate.couchbase.CouchbaseHealthIndicator;
22+
import org.springframework.boot.actuate.health.HealthIndicator;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
26+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.data.couchbase.core.CouchbaseOperations;
30+
31+
/**
32+
* Configuration for {@link CouchbaseHealthIndicator}.
33+
*
34+
* @author Mikalai Lushchytski
35+
* @since 2.1.0
36+
*/
37+
@Configuration
38+
@ConditionalOnClass(CouchbaseOperations.class)
39+
@ConditionalOnBean(CouchbaseOperations.class)
40+
@EnableConfigurationProperties(CouchbaseHealthIndicatorProperties.class)
41+
public class CouchbaseHealthIndicatorConfiguration extends
42+
CompositeHealthIndicatorConfiguration<CouchbaseHealthIndicator, CouchbaseOperations> {
43+
44+
private final Map<String, CouchbaseOperations> couchbaseOperations;
45+
46+
private final CouchbaseHealthIndicatorProperties properties;
47+
48+
CouchbaseHealthIndicatorConfiguration(
49+
Map<String, CouchbaseOperations> couchbaseOperations,
50+
CouchbaseHealthIndicatorProperties properties) {
51+
this.couchbaseOperations = couchbaseOperations;
52+
this.properties = properties;
53+
}
54+
55+
@Bean
56+
@ConditionalOnMissingBean(name = "couchbaseHealthIndicator")
57+
public HealthIndicator couchbaseHealthIndicator() {
58+
return createHealthIndicator(this.couchbaseOperations);
59+
}
60+
61+
@Override
62+
protected CouchbaseHealthIndicator createHealthIndicator(
63+
CouchbaseOperations couchbaseOperations) {
64+
return new CouchbaseHealthIndicator(couchbaseOperations,
65+
this.properties.getTimeout());
66+
}
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.boot.actuate.autoconfigure.couchbase;
17+
18+
import java.util.Map;
19+
20+
import org.springframework.boot.actuate.autoconfigure.health.CompositeReactiveHealthIndicatorConfiguration;
21+
import org.springframework.boot.actuate.couchbase.CouchbaseReactiveHealthIndicator;
22+
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.data.couchbase.core.RxJavaCouchbaseOperations;
29+
30+
/**
31+
* Configuration for
32+
* {@link org.springframework.boot.actuate.couchbase.CouchbaseReactiveHealthIndicator}.
33+
*
34+
* @author Mikalai Lushchytski
35+
* @since 2.1.0
36+
*/
37+
@Configuration
38+
@ConditionalOnClass(RxJavaCouchbaseOperations.class)
39+
@ConditionalOnBean(RxJavaCouchbaseOperations.class)
40+
public class CouchbaseReactiveHealthIndicatorConfiguration extends
41+
CompositeReactiveHealthIndicatorConfiguration<CouchbaseReactiveHealthIndicator, RxJavaCouchbaseOperations> {
42+
43+
private final Map<String, RxJavaCouchbaseOperations> couchbaseOperations;
44+
45+
CouchbaseReactiveHealthIndicatorConfiguration(
46+
Map<String, RxJavaCouchbaseOperations> couchbaseOperations) {
47+
this.couchbaseOperations = couchbaseOperations;
48+
}
49+
50+
@Bean
51+
@ConditionalOnMissingBean(name = "couchbaseReactiveHealthIndicator")
52+
public ReactiveHealthIndicator couchbaseReactiveHealthIndicator() {
53+
return createHealthIndicator(this.couchbaseOperations);
54+
}
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.boot.actuate.autoconfigure.couchbase;
17+
18+
import org.junit.Test;
19+
20+
import org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorAutoConfiguration;
21+
import org.springframework.boot.actuate.couchbase.CouchbaseHealthIndicator;
22+
import org.springframework.boot.actuate.couchbase.CouchbaseReactiveHealthIndicator;
23+
import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
24+
import org.springframework.boot.autoconfigure.AutoConfigurations;
25+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.data.couchbase.core.CouchbaseOperations;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.mockito.Mockito.mock;
32+
33+
/**
34+
* Tests for {@link CouchbaseHealthIndicatorConfiguration}.
35+
*
36+
* @author Mikalai Lushchytski
37+
*/
38+
public class CouchbaseHealthIndicatorConfigurationTests {
39+
40+
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
41+
.withUserConfiguration(CouchbaseMockConfiguration.class).withConfiguration(
42+
AutoConfigurations.of(CouchbaseHealthIndicatorAutoConfiguration.class,
43+
HealthIndicatorAutoConfiguration.class));
44+
45+
@Test
46+
public void runShouldCreateIndicator() {
47+
this.contextRunner.run((context) -> assertThat(context)
48+
.hasSingleBean(CouchbaseHealthIndicator.class)
49+
.doesNotHaveBean(CouchbaseReactiveHealthIndicator.class)
50+
.doesNotHaveBean(ApplicationHealthIndicator.class));
51+
}
52+
53+
@Test
54+
public void runWhenDisabledShouldNotCreateIndicator() {
55+
this.contextRunner.withPropertyValues("management.health.couchbase.enabled:false")
56+
.run((context) -> assertThat(context)
57+
.doesNotHaveBean(CouchbaseHealthIndicator.class)
58+
.hasSingleBean(ApplicationHealthIndicator.class));
59+
}
60+
61+
@Configuration
62+
protected static class CouchbaseMockConfiguration {
63+
64+
@Bean
65+
public CouchbaseOperations couchbaseOperations() {
66+
return mock(CouchbaseOperations.class);
67+
}
68+
69+
}
70+
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.boot.actuate.autoconfigure.couchbase;
17+
18+
import org.junit.Test;
19+
20+
import org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorAutoConfiguration;
21+
import org.springframework.boot.actuate.couchbase.CouchbaseHealthIndicator;
22+
import org.springframework.boot.actuate.couchbase.CouchbaseReactiveHealthIndicator;
23+
import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
24+
import org.springframework.boot.autoconfigure.AutoConfigurations;
25+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.data.couchbase.core.RxJavaCouchbaseOperations;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.mockito.Mockito.mock;
32+
33+
/**
34+
* Tests for {@link CouchbaseReactiveHealthIndicatorConfiguration}.
35+
*
36+
* @author Mikalai Lushchytski
37+
*/
38+
public class CouchbaseReactiveHealthIndicatorConfigurationTests {
39+
40+
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
41+
.withUserConfiguration(CouchbaseMockConfiguration.class).withConfiguration(
42+
AutoConfigurations.of(CouchbaseHealthIndicatorAutoConfiguration.class,
43+
HealthIndicatorAutoConfiguration.class));
44+
45+
@Test
46+
public void runShouldCreateIndicator() {
47+
this.contextRunner.run((context) -> assertThat(context)
48+
.hasSingleBean(CouchbaseReactiveHealthIndicator.class)
49+
.doesNotHaveBean(CouchbaseHealthIndicator.class)
50+
.doesNotHaveBean(ApplicationHealthIndicator.class));
51+
}
52+
53+
@Test
54+
public void runWhenDisabledShouldNotCreateIndicator() {
55+
this.contextRunner.withPropertyValues("management.health.couchbase.enabled:false")
56+
.run((context) -> assertThat(context)
57+
.doesNotHaveBean(CouchbaseReactiveHealthIndicator.class)
58+
.hasSingleBean(ApplicationHealthIndicator.class));
59+
}
60+
61+
@Configuration
62+
protected static class CouchbaseMockConfiguration {
63+
64+
@Bean
65+
public RxJavaCouchbaseOperations couchbaseOperations() {
66+
return mock(RxJavaCouchbaseOperations.class);
67+
}
68+
69+
}
70+
71+
}

spring-boot-project/spring-boot-actuator/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@
7777
</exclusion>
7878
</exclusions>
7979
</dependency>
80+
<dependency>
81+
<groupId>io.reactivex</groupId>
82+
<artifactId>rxjava-reactive-streams</artifactId>
83+
<optional>true</optional>
84+
</dependency>
8085
<dependency>
8186
<groupId>javax.cache</groupId>
8287
<artifactId>cache-api</artifactId>

0 commit comments

Comments
 (0)