Skip to content

Commit 7bc1863

Browse files
committed
Merge pull request #13864 from ayudovin:add-health-indicator-for-reactive-cassandra
* pr/13864: Polish "Add reactive health indicator for Cassandra" Add reactive health indicator for Cassandra
2 parents 0c41384 + af0aa11 commit 7bc1863

File tree

7 files changed

+337
-29
lines changed

7 files changed

+337
-29
lines changed
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-2018 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.
@@ -16,55 +16,39 @@
1616

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

19-
import java.util.Map;
20-
2119
import com.datastax.driver.core.Cluster;
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.cassandra.CassandraHealthIndicator;
27-
import org.springframework.boot.actuate.health.HealthIndicator;
24+
import org.springframework.boot.actuate.cassandra.CassandraReactiveHealthIndicator;
2825
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2926
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
3027
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3128
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
32-
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
3329
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
34-
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3530
import org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration;
36-
import org.springframework.context.annotation.Bean;
31+
import org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration;
3732
import org.springframework.context.annotation.Configuration;
38-
import org.springframework.data.cassandra.core.CassandraOperations;
33+
import org.springframework.context.annotation.Import;
3934

4035
/**
41-
* {@link EnableAutoConfiguration Auto-configuration} for
42-
* {@link CassandraHealthIndicator}.
36+
* {@link EnableAutoConfiguration Auto-configuration} for {@link CassandraHealthIndicator}
37+
* and {@link CassandraReactiveHealthIndicator}.
4338
*
4439
* @author Julien Dubois
40+
* @author Stephane Nicoll
4541
* @since 2.0.0
4642
*/
4743
@Configuration
48-
@ConditionalOnClass({ CassandraOperations.class, Cluster.class })
49-
@ConditionalOnBean(CassandraOperations.class)
44+
@ConditionalOnClass(Cluster.class)
5045
@ConditionalOnEnabledHealthIndicator("cassandra")
5146
@AutoConfigureBefore(HealthIndicatorAutoConfiguration.class)
5247
@AutoConfigureAfter({ CassandraAutoConfiguration.class,
53-
CassandraDataAutoConfiguration.class })
54-
public class CassandraHealthIndicatorAutoConfiguration extends
55-
CompositeHealthIndicatorConfiguration<CassandraHealthIndicator, CassandraOperations> {
56-
57-
private final Map<String, CassandraOperations> cassandraOperations;
58-
59-
public CassandraHealthIndicatorAutoConfiguration(
60-
Map<String, CassandraOperations> cassandraOperations) {
61-
this.cassandraOperations = cassandraOperations;
62-
}
63-
64-
@Bean
65-
@ConditionalOnMissingBean(name = "cassandraHealthIndicator")
66-
public HealthIndicator cassandraHealthIndicator() {
67-
return createHealthIndicator(this.cassandraOperations);
68-
}
48+
CassandraDataAutoConfiguration.class,
49+
CassandraReactiveDataAutoConfiguration.class })
50+
@Import({ CassandraReactiveHealthIndicatorConfiguration.class,
51+
CassandraHealthIndicatorConfiguration.class })
52+
public class CassandraHealthIndicatorAutoConfiguration {
6953

7054
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
17+
package org.springframework.boot.actuate.autoconfigure.cassandra;
18+
19+
import java.util.Map;
20+
21+
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthIndicatorConfiguration;
22+
import org.springframework.boot.actuate.cassandra.CassandraHealthIndicator;
23+
import org.springframework.boot.actuate.health.HealthIndicator;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.data.cassandra.core.CassandraOperations;
30+
31+
/**
32+
* Configuration for {@link CassandraHealthIndicator}.
33+
*
34+
* @author Julien Dubois
35+
*/
36+
@Configuration
37+
@ConditionalOnClass(CassandraOperations.class)
38+
@ConditionalOnBean(CassandraOperations.class)
39+
class CassandraHealthIndicatorConfiguration extends
40+
CompositeHealthIndicatorConfiguration<CassandraHealthIndicator, CassandraOperations> {
41+
42+
private final Map<String, CassandraOperations> cassandraOperations;
43+
44+
CassandraHealthIndicatorConfiguration(
45+
Map<String, CassandraOperations> cassandraOperations) {
46+
this.cassandraOperations = cassandraOperations;
47+
}
48+
49+
@Bean
50+
@ConditionalOnMissingBean(name = "cassandraHealthIndicator")
51+
public HealthIndicator cassandraHealthIndicator() {
52+
return createHealthIndicator(this.cassandraOperations);
53+
}
54+
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.cassandra;
17+
18+
import java.util.Map;
19+
20+
import org.springframework.boot.actuate.autoconfigure.health.CompositeReactiveHealthIndicatorConfiguration;
21+
import org.springframework.boot.actuate.cassandra.CassandraReactiveHealthIndicator;
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.cassandra.core.ReactiveCassandraOperations;
29+
30+
/**
31+
* Configuration for {@link CassandraReactiveHealthIndicator}.
32+
*
33+
* @author Artsiom Yudovin
34+
* @author Stephane Nicoll
35+
*/
36+
@Configuration
37+
@ConditionalOnClass(ReactiveCassandraOperations.class)
38+
@ConditionalOnBean(ReactiveCassandraOperations.class)
39+
class CassandraReactiveHealthIndicatorConfiguration extends
40+
CompositeReactiveHealthIndicatorConfiguration<CassandraReactiveHealthIndicator, ReactiveCassandraOperations> {
41+
42+
private final Map<String, ReactiveCassandraOperations> reactiveCassandraOperations;
43+
44+
CassandraReactiveHealthIndicatorConfiguration(
45+
Map<String, ReactiveCassandraOperations> reactiveCassandraOperations) {
46+
this.reactiveCassandraOperations = reactiveCassandraOperations;
47+
}
48+
49+
@Bean
50+
@ConditionalOnMissingBean(name = "cassandraReactiveHealthIndicator")
51+
public ReactiveHealthIndicator cassandraHealthIndicator() {
52+
return createHealthIndicator(this.reactiveCassandraOperations);
53+
}
54+
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
17+
package org.springframework.boot.actuate.autoconfigure.cassandra;
18+
19+
import org.junit.Test;
20+
21+
import org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorAutoConfiguration;
22+
import org.springframework.boot.actuate.cassandra.CassandraHealthIndicator;
23+
import org.springframework.boot.actuate.cassandra.CassandraReactiveHealthIndicator;
24+
import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
25+
import org.springframework.boot.autoconfigure.AutoConfigurations;
26+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.data.cassandra.core.ReactiveCassandraOperations;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.mockito.Mockito.mock;
33+
34+
/**
35+
* Tests for {@link CassandraReactiveHealthIndicatorConfiguration}.
36+
*
37+
* @author Artsiom Yudovin
38+
* @author Stephane Nicoll
39+
*/
40+
public class CassandraReactiveHealthIndicatorConfigurationTests {
41+
42+
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
43+
.withUserConfiguration(CassandraMockConfiguration.class).withConfiguration(
44+
AutoConfigurations.of(CassandraHealthIndicatorAutoConfiguration.class,
45+
HealthIndicatorAutoConfiguration.class));
46+
47+
@Test
48+
public void runShouldCreateIndicator() {
49+
this.contextRunner.run((context) -> assertThat(context)
50+
.hasSingleBean(CassandraReactiveHealthIndicator.class)
51+
.doesNotHaveBean(CassandraHealthIndicator.class)
52+
.doesNotHaveBean(ApplicationHealthIndicator.class));
53+
}
54+
55+
@Test
56+
public void runWhenDisabledShouldNotCreateIndicator() {
57+
this.contextRunner.withPropertyValues("management.health.cassandra.enabled:false")
58+
.run((context) -> assertThat(context)
59+
.doesNotHaveBean(CassandraReactiveHealthIndicator.class)
60+
.hasSingleBean(ApplicationHealthIndicator.class));
61+
}
62+
63+
@Configuration
64+
protected static class CassandraMockConfiguration {
65+
66+
@Bean
67+
public ReactiveCassandraOperations cassandraOperations() {
68+
return mock(ReactiveCassandraOperations.class);
69+
}
70+
71+
}
72+
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.cassandra;
17+
18+
import com.datastax.driver.core.querybuilder.QueryBuilder;
19+
import com.datastax.driver.core.querybuilder.Select;
20+
import reactor.core.publisher.Mono;
21+
22+
import org.springframework.boot.actuate.health.AbstractReactiveHealthIndicator;
23+
import org.springframework.boot.actuate.health.Health;
24+
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
25+
import org.springframework.data.cassandra.core.ReactiveCassandraOperations;
26+
import org.springframework.util.Assert;
27+
28+
/**
29+
* A {@link ReactiveHealthIndicator} for Cassandra.
30+
*
31+
* @author Artsiom Yudovin
32+
* @since 2.1.0
33+
*/
34+
public class CassandraReactiveHealthIndicator extends AbstractReactiveHealthIndicator {
35+
36+
private final ReactiveCassandraOperations reactiveCassandraOperations;
37+
38+
/**
39+
* Create a new {@link CassandraHealthIndicator} instance.
40+
* @param reactiveCassandraOperations the Cassandra operations
41+
*/
42+
public CassandraReactiveHealthIndicator(
43+
ReactiveCassandraOperations reactiveCassandraOperations) {
44+
Assert.notNull(reactiveCassandraOperations,
45+
"ReactiveCassandraOperations must not be null");
46+
this.reactiveCassandraOperations = reactiveCassandraOperations;
47+
}
48+
49+
@Override
50+
protected Mono<Health> doHealthCheck(Health.Builder builder) {
51+
Select select = QueryBuilder.select("release_version").from("system", "local");
52+
return this.reactiveCassandraOperations.getReactiveCqlOperations()
53+
.queryForObject(select, String.class)
54+
.map((version) -> builder.up().withDetail("version", version).build())
55+
.single();
56+
}
57+
58+
}

0 commit comments

Comments
 (0)