Skip to content

Instantly share code, notes, and snippets.

View dalexandrov's full-sized avatar

Dmitry Aleksandrov dalexandrov

View GitHub Profile
@dalexandrov
dalexandrov / builder.java
Last active February 23, 2021 13:48
builder.java
public Builder config(Config config) {
config.get("authentication.username").asString().ifPresent(this::username);
config.get("authentication.password").asString().ifPresent(this::password);
config.get("authentication.enabled").asBoolean().ifPresent(this::authenticationEnabled);
config.get("uri").asString().ifPresent(this::uri);
config.get("encrypted").asBoolean().ifPresent(this::encrypted);
//pool
config.get("pool.metricsEnabled").asBoolean().ifPresent(this::metricsEnabled);
config.get("pool.logLeakedSessions").asBoolean().ifPresent(this::logLeakedSessions);
config.get("pool.maxConnectionPoolSize").asInt().ifPresent(this::maxConnectionPoolSize);
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
</dependency>
...
public Builder password(String password) {
Objects.requireNonNull(password);
this.password = password;
return this;
}
...
@Override
public Neo4j build() {
if (driver == null) {
driver = initDriver();
}
return new Neo4j(this);
}
private Driver initDriver() {
AuthToken authToken = AuthTokens.none();
if (authenticationEnabled) {
authToken = AuthTokens.basic(username, password);
}
org.neo4j.driver.Config.ConfigBuilder configBuilder = createBaseConfig();
configureSsl(configBuilder);
configurePoolSettings(configBuilder);
return GraphDatabase.driver(uri, authToken, configBuilder.build());
}
public Driver driver() {
return driver;
}
Neo4jMetricsSupport.builder()
.driver(neo4j.driver())
.build()
.initialize();
Driver neo4jDriver = neo4j.driver();
neo4j:
uri: bolt://localhost:7687
authentication:
username: neo4j
password: secret
pool:
metricsEnabled: true
public List<Movie> findAll(){
try (var session = driver.session()) {
var query = ""
+ "match (m:Movie) "
+ "match (m) <- [:DIRECTED] - (d:Person) "
+ "match (m) <- [r:ACTED_IN] - (a:Person) "
+ "return m, collect(d) as directors, collect({name:a.name, roles: r.roles}) as actors";
return session.readTransaction(tx -> tx.run(query).list(r -> {
var movieNode = r.get("m").asNode();
public class Neo4jCdiExtension implements Extension {
private static final String NEO4J_METRIC_NAME_PREFIX = "neo4j";
void afterBeanDiscovery(@Observes AfterBeanDiscovery addEvent) {
addEvent.addBean()
.types(Driver.class)
.qualifiers(Default.Literal.INSTANCE, Any.Literal.INSTANCE)
.scope(ApplicationScoped.class)
.name(Driver.class.getName())
.beanClass(Driver.class)
.createWith(creationContext -> {