/
Neo4jReactiveHealthIndicator.java
105 lines (86 loc) · 3.58 KB
/
Neo4jReactiveHealthIndicator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.neo4j;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.neo4j.driver.Driver;
import org.neo4j.driver.Record;
import org.neo4j.driver.exceptions.SessionExpiredException;
import org.neo4j.driver.reactivestreams.ReactiveResult;
import org.neo4j.driver.reactivestreams.ReactiveSession;
import org.neo4j.driver.summary.ResultSummary;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;
import org.springframework.boot.actuate.health.AbstractReactiveHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
/**
* {@link ReactiveHealthIndicator} that tests the status of a Neo4j by executing a Cypher
* statement and extracting server and database information.
*
* @author Michael J. Simons
* @author Stephane Nicoll
* @author Phillip Webb
* @since 2.4.0
*/
public final class Neo4jReactiveHealthIndicator extends AbstractReactiveHealthIndicator {
private static final Log logger = LogFactory.getLog(Neo4jReactiveHealthIndicator.class);
private final Driver driver;
private final Neo4jHealthDetailsHandler healthDetailsHandler;
public Neo4jReactiveHealthIndicator(Driver driver) {
this.driver = driver;
this.healthDetailsHandler = new Neo4jHealthDetailsHandler();
}
@Override
protected Mono<Health> doHealthCheck(Health.Builder builder) {
return runHealthCheckQuery()
.doOnError(SessionExpiredException.class,
(ex) -> logger.warn(Neo4jHealthIndicator.MESSAGE_SESSION_EXPIRED))
.retryWhen(Retry.max(1).filter(SessionExpiredException.class::isInstance)).map((healthDetails) -> {
this.healthDetailsHandler.addHealthDetails(builder, healthDetails);
return builder.build();
});
}
Mono<Neo4jHealthDetails> runHealthCheckQuery() {
return Mono.using(this::session, this::healthDetails, ReactiveSession::close);
}
private ReactiveSession session() {
return this.driver.session(ReactiveSession.class, Neo4jHealthIndicator.DEFAULT_SESSION_CONFIG);
}
private Mono<Neo4jHealthDetails> healthDetails(ReactiveSession session) {
return Mono.from(session.run(Neo4jHealthIndicator.CYPHER)).flatMap(this::healthDetails);
}
private Mono<? extends Neo4jHealthDetails> healthDetails(ReactiveResult result) {
Flux<Record> records = Flux.from(result.records());
Mono<ResultSummary> summary = Mono.from(result.consume());
Neo4jHealthDetailsBuilder builder = new Neo4jHealthDetailsBuilder();
return records.single().doOnNext(builder::record).then(summary).map(builder::build);
}
/**
* Builder used to create a {@link Neo4jHealthDetails} from a {@link Record} and a
* {@link ResultSummary}.
*/
private static class Neo4jHealthDetailsBuilder {
private Record record;
void record(Record record) {
this.record = record;
}
private Neo4jHealthDetails build(ResultSummary summary) {
return new Neo4jHealthDetails(this.record, summary);
}
}
}