2424import org .eclipse .microprofile .health .HealthCheckResponse ;
2525import org .eclipse .microprofile .health .HealthCheckResponseBuilder ;
2626import org .eclipse .microprofile .health .Readiness ;
27- import org .neo4j .driver .AccessMode ;
2827import org .neo4j .driver .Driver ;
29- import org .neo4j .driver .Result ;
3028import 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