Skip to content

Commit 0482cc8

Browse files
committed
Merge pull request #22302 from meistermeier
* pr/22302: Polish "Update Neo4j health check to use the Neo4j Driver" Update Neo4j health check to use the Neo4j Driver Closes gh-22302
2 parents 2756f59 + c6fde1e commit 0482cc8

File tree

15 files changed

+559
-119
lines changed

15 files changed

+559
-119
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ dependencies {
8686
optional("org.liquibase:liquibase-core")
8787
optional("org.mongodb:mongodb-driver-reactivestreams")
8888
optional("org.mongodb:mongodb-driver-sync")
89+
optional("org.neo4j.driver:neo4j-java-driver")
8990
optional("org.springframework:spring-jdbc")
9091
optional("org.springframework:spring-jms")
9192
optional("org.springframework:spring-messaging")
@@ -96,7 +97,6 @@ dependencies {
9697
optional("org.springframework.data:spring-data-couchbase")
9798
optional("org.springframework.data:spring-data-ldap")
9899
optional("org.springframework.data:spring-data-mongodb")
99-
optional("org.springframework.data:spring-data-neo4j")
100100
optional("org.springframework.data:spring-data-redis")
101101
optional("org.springframework.data:spring-data-elasticsearch")
102102
optional("org.springframework.data:spring-data-solr")
Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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,42 +16,36 @@
1616

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

19-
import java.util.Map;
19+
import org.neo4j.driver.Driver;
2020

21-
import org.neo4j.ogm.session.SessionFactory;
22-
23-
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration;
2421
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
25-
import org.springframework.boot.actuate.health.HealthContributor;
22+
import org.springframework.boot.actuate.autoconfigure.neo4j.Neo4jHealthContributorConfigurations.Neo4jConfiguration;
23+
import org.springframework.boot.actuate.autoconfigure.neo4j.Neo4jHealthContributorConfigurations.Neo4jReactiveConfiguration;
2624
import org.springframework.boot.actuate.neo4j.Neo4jHealthIndicator;
25+
import org.springframework.boot.actuate.neo4j.Neo4jReactiveHealthIndicator;
2726
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2827
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2928
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
3029
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
31-
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
32-
import org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration;
33-
import org.springframework.context.annotation.Bean;
30+
import org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration;
3431
import org.springframework.context.annotation.Configuration;
32+
import org.springframework.context.annotation.Import;
3533

3634
/**
37-
* {@link EnableAutoConfiguration Auto-configuration} for {@link Neo4jHealthIndicator}.
35+
* {@link EnableAutoConfiguration Auto-configuration} for
36+
* {@link Neo4jReactiveHealthIndicator} and {@link Neo4jHealthIndicator}.
3837
*
3938
* @author Eric Spiegelberg
4039
* @author Stephane Nicoll
40+
* @author Michael J. Simons
4141
* @since 2.0.0
4242
*/
4343
@Configuration(proxyBeanMethods = false)
44-
@ConditionalOnClass(SessionFactory.class)
45-
@ConditionalOnBean(SessionFactory.class)
44+
@ConditionalOnClass(Driver.class)
45+
@ConditionalOnBean(Driver.class)
4646
@ConditionalOnEnabledHealthIndicator("neo4j")
47-
@AutoConfigureAfter(Neo4jDataAutoConfiguration.class)
48-
public class Neo4jHealthContributorAutoConfiguration
49-
extends CompositeHealthContributorConfiguration<Neo4jHealthIndicator, SessionFactory> {
50-
51-
@Bean
52-
@ConditionalOnMissingBean(name = { "neo4jHealthIndicator", "neo4jHealthContributor" })
53-
public HealthContributor neo4jHealthContributor(Map<String, SessionFactory> sessionFactories) {
54-
return createContributor(sessionFactories);
55-
}
47+
@AutoConfigureAfter(Neo4jAutoConfiguration.class)
48+
@Import({ Neo4jReactiveConfiguration.class, Neo4jConfiguration.class })
49+
public class Neo4jHealthContributorAutoConfiguration {
5650

5751
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2012-2020 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+
* https://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.neo4j;
18+
19+
import java.util.Map;
20+
21+
import org.neo4j.driver.Driver;
22+
import reactor.core.publisher.Flux;
23+
24+
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration;
25+
import org.springframework.boot.actuate.autoconfigure.health.CompositeReactiveHealthContributorConfiguration;
26+
import org.springframework.boot.actuate.health.HealthContributor;
27+
import org.springframework.boot.actuate.health.ReactiveHealthContributor;
28+
import org.springframework.boot.actuate.neo4j.Neo4jHealthIndicator;
29+
import org.springframework.boot.actuate.neo4j.Neo4jReactiveHealthIndicator;
30+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
31+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
32+
import org.springframework.context.annotation.Bean;
33+
import org.springframework.context.annotation.Configuration;
34+
35+
/**
36+
* Health contributor options for Neo4j.
37+
*
38+
* @author Michael J. Simons
39+
* @author Stephane Nicoll
40+
*/
41+
class Neo4jHealthContributorConfigurations {
42+
43+
@Configuration(proxyBeanMethods = false)
44+
static class Neo4jConfiguration extends CompositeHealthContributorConfiguration<Neo4jHealthIndicator, Driver> {
45+
46+
@Bean
47+
@ConditionalOnMissingBean(name = { "neo4jHealthIndicator", "neo4jHealthContributor" })
48+
HealthContributor neo4jHealthContributor(Map<String, Driver> drivers) {
49+
return createContributor(drivers);
50+
}
51+
52+
}
53+
54+
@Configuration(proxyBeanMethods = false)
55+
@ConditionalOnClass(Flux.class)
56+
static class Neo4jReactiveConfiguration
57+
extends CompositeReactiveHealthContributorConfiguration<Neo4jReactiveHealthIndicator, Driver> {
58+
59+
@Bean
60+
@ConditionalOnMissingBean(name = { "neo4jHealthIndicator", "neo4jHealthContributor" })
61+
ReactiveHealthContributor neo4jHealthContributor(Map<String, Driver> drivers) {
62+
return createContributor(drivers);
63+
}
64+
65+
}
66+
67+
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/neo4j/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/SpringApplicationHierarchyTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
3333
import org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration;
3434
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
35+
import org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration;
3536
import org.springframework.boot.builder.SpringApplicationBuilder;
3637
import org.springframework.boot.test.util.ApplicationContextTestUtils;
3738
import org.springframework.context.ConfigurableApplicationContext;
@@ -69,7 +70,7 @@ void testChild() {
6970
@EnableAutoConfiguration(exclude = { ElasticsearchDataAutoConfiguration.class,
7071
ElasticsearchRepositoriesAutoConfiguration.class, CassandraAutoConfiguration.class,
7172
CassandraDataAutoConfiguration.class, MongoDataAutoConfiguration.class,
72-
MongoReactiveDataAutoConfiguration.class, Neo4jDataAutoConfiguration.class,
73+
MongoReactiveDataAutoConfiguration.class, Neo4jAutoConfiguration.class, Neo4jDataAutoConfiguration.class,
7374
Neo4jRepositoriesAutoConfiguration.class, RedisAutoConfiguration.class,
7475
RedisRepositoriesAutoConfiguration.class, FlywayAutoConfiguration.class, MetricsAutoConfiguration.class })
7576
static class Parent {
@@ -80,7 +81,7 @@ static class Parent {
8081
@EnableAutoConfiguration(exclude = { ElasticsearchDataAutoConfiguration.class,
8182
ElasticsearchRepositoriesAutoConfiguration.class, CassandraAutoConfiguration.class,
8283
CassandraDataAutoConfiguration.class, MongoDataAutoConfiguration.class,
83-
MongoReactiveDataAutoConfiguration.class, Neo4jDataAutoConfiguration.class,
84+
MongoReactiveDataAutoConfiguration.class, Neo4jAutoConfiguration.class, Neo4jDataAutoConfiguration.class,
8485
Neo4jRepositoriesAutoConfiguration.class, RedisAutoConfiguration.class,
8586
RedisRepositoriesAutoConfiguration.class, FlywayAutoConfiguration.class, MetricsAutoConfiguration.class })
8687
static class Child {

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/neo4j/Neo4jHealthContributorAutoConfigurationTests.java

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -13,17 +13,20 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.boot.actuate.autoconfigure.neo4j;
1817

1918
import org.junit.jupiter.api.Test;
20-
import org.neo4j.ogm.session.Session;
21-
import org.neo4j.ogm.session.SessionFactory;
19+
import org.neo4j.driver.Driver;
20+
import reactor.core.publisher.Flux;
2221

2322
import org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration;
23+
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
2424
import org.springframework.boot.actuate.health.Health;
25+
import org.springframework.boot.actuate.health.HealthIndicator;
2526
import org.springframework.boot.actuate.neo4j.Neo4jHealthIndicator;
27+
import org.springframework.boot.actuate.neo4j.Neo4jReactiveHealthIndicator;
2628
import org.springframework.boot.autoconfigure.AutoConfigurations;
29+
import org.springframework.boot.test.context.FilteredClassLoader;
2730
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2831
import org.springframework.context.annotation.Bean;
2932
import org.springframework.context.annotation.Configuration;
@@ -37,39 +40,57 @@
3740
*
3841
* @author Phillip Webb
3942
* @author Stephane Nicoll
43+
* @author Michael J. Simons
4044
*/
4145
class Neo4jHealthContributorAutoConfigurationTests {
4246

43-
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
44-
.withUserConfiguration(Neo4jConfiguration.class).withConfiguration(AutoConfigurations
45-
.of(Neo4jHealthContributorAutoConfiguration.class, HealthContributorAutoConfiguration.class));
47+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
48+
.withConfiguration(AutoConfigurations.of(HealthContributorAutoConfiguration.class,
49+
Neo4jHealthContributorAutoConfiguration.class));
50+
51+
@Test
52+
void runShouldCreateHealthIndicator() {
53+
this.contextRunner.withUserConfiguration(Neo4jConfiguration.class).run((context) -> assertThat(context)
54+
.hasSingleBean(Neo4jReactiveHealthIndicator.class).doesNotHaveBean(Neo4jHealthIndicator.class));
55+
}
4656

4757
@Test
48-
void runShouldCreateIndicator() {
49-
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(Neo4jHealthIndicator.class));
58+
void runWithoutReactorShouldCreateHealthIndicator() {
59+
this.contextRunner.withUserConfiguration(Neo4jConfiguration.class)
60+
.withClassLoader(new FilteredClassLoader(Flux.class)).run((context) -> assertThat(context)
61+
.hasSingleBean(Neo4jHealthIndicator.class).doesNotHaveBean(Neo4jReactiveHealthIndicator.class));
5062
}
5163

5264
@Test
5365
void runWhenDisabledShouldNotCreateIndicator() {
54-
this.contextRunner.withPropertyValues("management.health.neo4j.enabled:false")
55-
.run((context) -> assertThat(context).doesNotHaveBean(Neo4jHealthIndicator.class));
66+
this.contextRunner.withUserConfiguration(Neo4jConfiguration.class)
67+
.withPropertyValues("management.health.neo4j.enabled=false")
68+
.run((context) -> assertThat(context).doesNotHaveBean(Neo4jHealthIndicator.class)
69+
.doesNotHaveBean(Neo4jReactiveHealthIndicator.class));
5670
}
5771

5872
@Test
5973
void defaultIndicatorCanBeReplaced() {
60-
this.contextRunner.withUserConfiguration(CustomIndicatorConfiguration.class).run((context) -> {
61-
assertThat(context).hasSingleBean(Neo4jHealthIndicator.class);
62-
Health health = context.getBean(Neo4jHealthIndicator.class).health();
63-
assertThat(health.getDetails()).containsOnly(entry("test", true));
64-
});
74+
this.contextRunner.withUserConfiguration(Neo4jConfiguration.class, CustomIndicatorConfiguration.class)
75+
.run((context) -> {
76+
assertThat(context).hasBean("neo4jHealthIndicator");
77+
Health health = context.getBean("neo4jHealthIndicator", HealthIndicator.class).health();
78+
assertThat(health.getDetails()).containsOnly(entry("test", true));
79+
});
80+
}
81+
82+
@Test
83+
void shouldRequireDriverBean() {
84+
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(Neo4jHealthIndicator.class)
85+
.doesNotHaveBean(Neo4jReactiveHealthIndicator.class));
6586
}
6687

6788
@Configuration(proxyBeanMethods = false)
6889
static class Neo4jConfiguration {
6990

7091
@Bean
71-
SessionFactory sessionFactory() {
72-
return mock(SessionFactory.class);
92+
Driver driver() {
93+
return mock(Driver.class);
7394
}
7495

7596
}
@@ -78,14 +99,12 @@ SessionFactory sessionFactory() {
7899
static class CustomIndicatorConfiguration {
79100

80101
@Bean
81-
Neo4jHealthIndicator neo4jHealthIndicator(SessionFactory sessionFactory) {
82-
return new Neo4jHealthIndicator(sessionFactory) {
102+
HealthIndicator neo4jHealthIndicator() {
103+
return new AbstractHealthIndicator() {
83104

84-
@Override
85-
protected void extractResult(Session session, Health.Builder builder) {
105+
protected void doHealthCheck(Health.Builder builder) {
86106
builder.up().withDetail("test", true);
87107
}
88-
89108
};
90109
}
91110

spring-boot-project/spring-boot-actuator/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ dependencies {
4646
optional("org.liquibase:liquibase-core")
4747
optional("org.mongodb:mongodb-driver-reactivestreams")
4848
optional("org.mongodb:mongodb-driver-sync")
49+
optional("org.neo4j.driver:neo4j-java-driver")
4950
optional("org.springframework:spring-jdbc")
5051
optional("org.springframework:spring-messaging")
5152
optional("org.springframework:spring-webflux")
@@ -57,7 +58,6 @@ dependencies {
5758
optional("org.springframework.data:spring-data-elasticsearch")
5859
optional("org.springframework.data:spring-data-ldap")
5960
optional("org.springframework.data:spring-data-mongodb")
60-
optional("org.springframework.data:spring-data-neo4j")
6161
optional("org.springframework.data:spring-data-redis")
6262
optional("org.springframework.data:spring-data-rest-webmvc")
6363
optional("org.springframework.data:spring-data-solr")
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2012-2020 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+
* https://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.neo4j;
18+
19+
import org.neo4j.driver.summary.DatabaseInfo;
20+
import org.neo4j.driver.summary.ResultSummary;
21+
import org.neo4j.driver.summary.ServerInfo;
22+
23+
import org.springframework.boot.actuate.health.Health.Builder;
24+
import org.springframework.util.StringUtils;
25+
26+
/**
27+
* Handle health check details for a Neo4j server.
28+
*
29+
* @author Stephane Nicoll
30+
*/
31+
class Neo4jHealthDetailsHandler {
32+
33+
/**
34+
* Add health details for the specified {@link ResultSummary} and {@code edition}.
35+
* @param builder the {@link Builder} to use
36+
* @param edition the edition of the server
37+
* @param resultSummary server information
38+
*/
39+
void addHealthDetails(Builder builder, String edition, ResultSummary resultSummary) {
40+
ServerInfo serverInfo = resultSummary.server();
41+
builder.up().withDetail("server", serverInfo.version() + "@" + serverInfo.address()).withDetail("edition",
42+
edition);
43+
DatabaseInfo databaseInfo = resultSummary.database();
44+
if (StringUtils.hasText(databaseInfo.name())) {
45+
builder.withDetail("database", databaseInfo.name());
46+
}
47+
}
48+
49+
}

0 commit comments

Comments
 (0)