From 9bfcd1c498547ece3b057556e6ea16838605e390 Mon Sep 17 00:00:00 2001 From: Zhen Date: Mon, 2 Oct 2017 16:42:18 +0200 Subject: [PATCH] Add examples for 1.5 driver documentation --- .../driver/ConfigConnectionPoolExample.java | 55 +++++++++++++++++++ .../ConfigLoadBalancingStrategyExample.java | 51 +++++++++++++++++ .../org/neo4j/docs/driver/ExamplesIT.java | 23 ++++++++ 3 files changed, 129 insertions(+) create mode 100644 examples/src/main/java/org/neo4j/docs/driver/ConfigConnectionPoolExample.java create mode 100644 examples/src/main/java/org/neo4j/docs/driver/ConfigLoadBalancingStrategyExample.java diff --git a/examples/src/main/java/org/neo4j/docs/driver/ConfigConnectionPoolExample.java b/examples/src/main/java/org/neo4j/docs/driver/ConfigConnectionPoolExample.java new file mode 100644 index 0000000000..7ec7e6c4c8 --- /dev/null +++ b/examples/src/main/java/org/neo4j/docs/driver/ConfigConnectionPoolExample.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2002-2017 "Neo Technology," + * Network Engine for Objects in Lund AB [http://neotechnology.com] + * + * This file is part of Neo4j. + * + * 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 + * + * http://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.neo4j.docs.driver; + +import java.util.concurrent.TimeUnit; + +import org.neo4j.driver.v1.AuthTokens; +import org.neo4j.driver.v1.Config; +import org.neo4j.driver.v1.Driver; +import org.neo4j.driver.v1.GraphDatabase; +import org.neo4j.driver.v1.StatementResult; + +public class ConfigConnectionPoolExample implements AutoCloseable +{ + private final Driver driver; + + // tag::config-connection-pool[] + public ConfigConnectionPoolExample( String uri, String user, String password ) + { + driver = GraphDatabase.driver( uri, AuthTokens.basic( user, password ), Config.build() + .withMaxConnectionLifetime( 30, TimeUnit.MINUTES ) + .withMaxConnectionPoolSize( 50 ) + .withConnectionAcquisitionTimeout( 2, TimeUnit.MINUTES ) + .toConfig() ); + } + // end::config-connection-pool[] + + @Override + public void close() throws Exception + { + driver.close(); + } + + public boolean canConnect() + { + StatementResult result = driver.session().run( "RETURN 1" ); + return result.single().get( 0 ).asInt() == 1; + } +} diff --git a/examples/src/main/java/org/neo4j/docs/driver/ConfigLoadBalancingStrategyExample.java b/examples/src/main/java/org/neo4j/docs/driver/ConfigLoadBalancingStrategyExample.java new file mode 100644 index 0000000000..b15fcf3d3d --- /dev/null +++ b/examples/src/main/java/org/neo4j/docs/driver/ConfigLoadBalancingStrategyExample.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2002-2017 "Neo Technology," + * Network Engine for Objects in Lund AB [http://neotechnology.com] + * + * This file is part of Neo4j. + * + * 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 + * + * http://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.neo4j.docs.driver; + +import org.neo4j.driver.v1.AuthTokens; +import org.neo4j.driver.v1.Config; +import org.neo4j.driver.v1.Driver; +import org.neo4j.driver.v1.GraphDatabase; +import org.neo4j.driver.v1.StatementResult; + +public class ConfigLoadBalancingStrategyExample implements AutoCloseable +{ + private final Driver driver; + + // tag::config-load-balancing-strategy[] + public ConfigLoadBalancingStrategyExample( String uri, String user, String password ) + { + driver = GraphDatabase.driver( uri, AuthTokens.basic( user, password ), Config.build() + .withLoadBalancingStrategy( Config.LoadBalancingStrategy.LEAST_CONNECTED ) + .toConfig() ); + } + // end::config-load-balancing-strategy[] + + @Override + public void close() throws Exception + { + driver.close(); + } + + public boolean canConnect() + { + StatementResult result = driver.session().run( "RETURN 1" ); + return result.single().get( 0 ).asInt() == 1; + } +} diff --git a/examples/src/test/java/org/neo4j/docs/driver/ExamplesIT.java b/examples/src/test/java/org/neo4j/docs/driver/ExamplesIT.java index ba58fb979e..d68cea3096 100644 --- a/examples/src/test/java/org/neo4j/docs/driver/ExamplesIT.java +++ b/examples/src/test/java/org/neo4j/docs/driver/ExamplesIT.java @@ -122,6 +122,29 @@ public void testShouldRunAutocommitTransactionExample() throws Exception } } + @Test + public void testShouldRunConfigConnectionPoolExample() throws Exception + { + // Given + try ( ConfigConnectionPoolExample example = new ConfigConnectionPoolExample( uri, USER, PASSWORD ) ) + { + // Then + assertTrue( example.canConnect() ); + } + } + + @Test + public void testShouldRunConfigLoadBalancingStrategyExample() throws Exception + { + // Given + try ( ConfigLoadBalancingStrategyExample example = + new ConfigLoadBalancingStrategyExample( uri, USER, PASSWORD ) ) + { + // Then + assertTrue( example.canConnect() ); + } + } + @Test public void testShouldRunBasicAuthExample() throws Exception {