Neo4j
Overview
Neo4j is a graph database management system developed by Neo4j, Inc. It is an ACID-compliant transactional database with native graph storage and processing. Neo4j is available in a GPL3-licensed open-source “community edition”.
Maven Coordinates
To enable Neo4j, add the following dependency to your project’s pom.xml (see
Managing Dependencies).
<dependency>
<groupId>io.helidon.integrations.neo4j</groupId>
<artifactId>helidon-integrations-neo4j</artifactId>
</dependency>
Metrics and Health Checks integration.Usage
The support for Neo4j is implemented in Neo4j driver level. Just add the
dependency, add configuration in microprofile-config.properties file and Neo4j
driver will be configured by Helidon and can be injected using CDI.
First describe Neo4j connection properties:
# Neo4j settings
neo4j.uri=bolt://localhost:7687
neo4j.authentication.username=neo4j
neo4j.authentication.password=secret
neo4j.pool.metricsEnabled=true
Then just inject the driver:
@Inject
public MovieRepository(Driver driver) {
this.driver = driver;
}
The driver can be used according to the Neo4j documentation.
Configuration
Configuration options
| Key | Type | Default | Description |
|---|---|---|---|
idle- | Duration | PT1MS | Set idle time |
certificate | Path | Set certificate path | |
max- | Integer | 100 | Set pool size |
hostname- | Boolean | Enable hostname verification | |
metrics- | Boolean | Enable metrics | |
trust- | Trust | Set trust strategy | |
uri | String | Create uri | |
connection- | Duration | PT1M | Set connection acquisition timeout |
authentication- | Boolean | true | Enable authentication |
password | String | Create password | |
encrypted | Boolean | Enable encrypted field | |
log- | Boolean | Enable log leaked sessions | |
max- | Duration | PT5H | Set max life time |
username | String | Create username |
Examples
This example implements a simple Neo4j REST service using MicroProfile. For this example a working Neo4j database is required. The Neo4j Movie database is used for this example.
Bring up a Neo4j instance via Docker
docker run --publish=7474:7474 --publish=7687:7687 -e 'NEO4J_AUTH=neo4j/secret' neo4j:latest
Go to the Neo4j browser and play the first step of the movies graph: :play movies
Now go to the pom.xml and add the following dependencies:
<dependencies>
<dependency>
<groupId>io.helidon.integrations.neo4j</groupId>
<artifactId>helidon-integrations-neo4j</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.integrations.neo4j</groupId>
<artifactId>helidon-integrations-neo4j-metrics</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.integrations.neo4j</groupId>
<artifactId>helidon-integrations-neo4j-health</artifactId>
</dependency>
</dependencies>
Next add the connection configuration properties for Neo4j:
# Neo4j settings
neo4j.uri=bolt://localhost:7687
neo4j.authentication.username=neo4j
neo4j.authentication.password=secret
neo4j.pool.metricsEnabled=true
# Enable the optional MicroProfile Metrics REST.request metrics
metrics.rest-request.enabled=true
This includes both connection information and enables Neo4j metrics propagation.
Finally, we are able to inject and use the Neo4j driver.
Movies can now be returned as JSON objects:
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Movie> getAllMovies() {
return movieRepository.findAll();
}
Now build and run.
mvn package
java -jar target/helidon-examples-integration-neo4j-mp.jar
Exercise the application:
curl -X GET http://localhost:8080/movies
# Try health
curl -s -X GET http://localhost:8080/health
# Try metrics in Prometheus Format
curl -s -X GET http://localhost:8080/metrics
# Try metrics in JSON Format
curl -H 'Accept: application/json' -X GET http://localhost:8080/metrics
Additional Information
Neo4j Metrics propagation
Neo4j’s metrics can be propagated to the user as MicroProfile metrics. This is
implemented in a separate Maven module. Just add
<dependency>
<groupId>io.helidon.integrations.neo4j</groupId>
<artifactId>helidon-integrations-neo4j-metrics</artifactId>
</dependency>
To enable metrics in Neo4j, add the following property to
microprofile-config.properties:
neo4j.pool.metricsEnabled=true
By applying these two actions, Neo4j metrics will be automatically added to the
output of the /metrics endpoint.
Neo4j Health Checks
If your application is highly dependent on Neo4j database, health and liveness checks are essential for this application to work correctly.
MicroProfile Health checks for Neo4j are implemented in a separate Maven
module:
<dependency>
<groupId>io.helidon.integrations.neo4j</groupId>
<artifactId>helidon-integrations-neo4j-health</artifactId>
</dependency>
Health checks for Neo4j will be included in /health endpoint output.