Skip to content

Commit 79a0931

Browse files
authored
Neo4j health check fix (helidon-io#3084)
* Apply fix for Neo4jHealthCheck: Unable to build native image. helidon-io#3060 * Fix native image build using workaround * Checkstyle fixes
1 parent 2eb1222 commit 79a0931

2 files changed

Lines changed: 53 additions & 57 deletions

File tree

  • examples/integrations/neo4j/neo4j-mp
  • integrations/neo4j/health/src/main/java/io/helidon/integrations/neo4j/health

examples/integrations/neo4j/neo4j-mp/pom.xml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
-->
1919

2020
<project xmlns="http://maven.apache.org/POM/4.0.0"
21-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2323
<modelVersion>4.0.0</modelVersion>
2424
<parent>
2525
<groupId>io.helidon.applications</groupId>
@@ -77,6 +77,22 @@
7777
<groupId>org.junit.vintage</groupId>
7878
<artifactId>junit-vintage-engine</artifactId>
7979
</exclusion>
80+
<exclusion>
81+
<groupId>org.neo4j.app</groupId>
82+
<artifactId>neo4j-server</artifactId>
83+
</exclusion>
84+
</exclusions>
85+
</dependency>
86+
<dependency>
87+
<groupId>org.neo4j.app</groupId>
88+
<artifactId>neo4j-server</artifactId>
89+
<version>${neo4j-harness.version}</version>
90+
<scope>test</scope>
91+
<exclusions>
92+
<exclusion>
93+
<groupId>*</groupId>
94+
<artifactId>*</artifactId>
95+
</exclusion>
8096
</exclusions>
8197
</dependency>
8298
<dependency>

integrations/neo4j/health/src/main/java/io/helidon/integrations/neo4j/health/Neo4jHealthCheck.java

Lines changed: 35 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,9 @@
2424
import org.eclipse.microprofile.health.HealthCheckResponse;
2525
import org.eclipse.microprofile.health.HealthCheckResponseBuilder;
2626
import org.eclipse.microprofile.health.Readiness;
27-
import org.neo4j.driver.AccessMode;
2827
import org.neo4j.driver.Driver;
29-
import org.neo4j.driver.Result;
3028
import org.neo4j.driver.Session;
31-
import org.neo4j.driver.SessionConfig;
32-
import org.neo4j.driver.exceptions.SessionExpiredException;
33-
import org.neo4j.driver.summary.ResultSummary;
34-
import org.neo4j.driver.summary.ServerInfo;
29+
3530

3631
/**
3732
* Health support module for Neo4j. Follows the standard MicroProfile HealthCheck pattern.
@@ -43,78 +38,63 @@ public class Neo4jHealthCheck implements HealthCheck {
4338
/**
4439
* The Cypher statement used to verify Neo4j is up.
4540
*/
46-
private static final String CYPHER = "RETURN 1 AS result";
47-
48-
private static final SessionConfig DEFAULT_SESSION_CONFIG = SessionConfig.builder()
49-
.withDefaultAccessMode(AccessMode.WRITE)
50-
.build();
41+
static final String CYPHER = "CALL dbms.components() YIELD name, edition WHERE name = 'Neo4j Kernel' RETURN edition";
5142

5243
private final Driver driver;
5344

54-
@Inject
55-
//will be ignored outside of CDI
45+
/**
46+
* Constructor for Health checks.
47+
*
48+
* @param driver Neo4j.
49+
*/
50+
@Inject //will be ignored out of CDI
5651
Neo4jHealthCheck(Driver driver) {
5752
this.driver = driver;
5853
}
5954

6055
/**
61-
* To be used in SE context.
56+
* Creates the Neo4j driver.
6257
*
63-
* @param driver create
64-
* @return Driver
58+
* @param driver Neo4j.
59+
* @return Neo4jHealthCheck.
6560
*/
6661
public static Neo4jHealthCheck create(Driver driver) {
6762
return new Neo4jHealthCheck(driver);
6863
}
6964

70-
/**
71-
* Applies the given ResultSummaryto the HealthCheckResponseBuilder builder and calls build
72-
* afterwards.
73-
*
74-
* @param resultSummary the result summary returned by the server
75-
* @param builder the health builder to be modified
76-
* @return the final HealthCheckResponse health check response
77-
*/
78-
private static HealthCheckResponse buildStatusUp(ResultSummary resultSummary, HealthCheckResponseBuilder builder) {
79-
ServerInfo serverInfo = resultSummary.server();
65+
private HealthCheckResponse runHealthCheckQuery(HealthCheckResponseBuilder builder) {
8066

81-
builder.withData("server", serverInfo.version() + "@" + serverInfo.address());
67+
try (Session session = this.driver.session()) {
8268

83-
String databaseName = resultSummary.database().name();
84-
if (!(databaseName == null || databaseName.trim().isEmpty())) {
85-
builder.withData("database", databaseName.trim());
86-
}
69+
return session.writeTransaction(tx -> {
70+
var result = tx.run(CYPHER);
8771

88-
return builder.build();
89-
}
72+
var edition = result.single().get("edition").asString();
73+
var resultSummary = result.consume();
74+
var serverInfo = resultSummary.server();
9075

91-
@Override
92-
public HealthCheckResponse call() {
76+
var responseBuilder = builder
77+
.withData("server", serverInfo.version() + "@" + serverInfo.address())
78+
.withData("edition", edition);
9379

94-
HealthCheckResponseBuilder builder = HealthCheckResponse.named("Neo4j connection health check").up();
95-
try {
96-
ResultSummary resultSummary;
97-
// Retry one time when the session has been expired
98-
try {
99-
resultSummary = runHealthCheckQuery();
100-
} catch (SessionExpiredException sessionExpiredException) {
101-
resultSummary = runHealthCheckQuery();
102-
}
103-
return buildStatusUp(resultSummary, builder);
104-
} catch (Exception e) {
105-
return builder.down().withData("reason", e.getMessage()).build();
80+
var databaseInfo = resultSummary.database();
81+
if (!databaseInfo.name().trim().isBlank()) {
82+
responseBuilder.withData("database", databaseInfo.name().trim());
83+
}
84+
85+
return responseBuilder.up().build();
86+
});
10687
}
10788
}
10889

109-
private ResultSummary runHealthCheckQuery() {
110-
// We use WRITE here to make sure UP is returned for a server that supports
111-
// all possible workloads
112-
if (driver != null) {
113-
Session session = this.driver.session(DEFAULT_SESSION_CONFIG);
90+
@Override
91+
public HealthCheckResponse call() {
11492

115-
Result run = session.run(CYPHER);
116-
return run.consume();
93+
var builder = HealthCheckResponse.named("Neo4j connection health check");
94+
try {
95+
return runHealthCheckQuery(builder);
96+
} catch (Exception ex) {
97+
return builder.down().withData("reason", ex.getMessage()).build();
11798
}
118-
return null;
11999
}
120100
}

0 commit comments

Comments
 (0)