|
| 1 | +/* |
| 2 | + * Copyright (c) 2002-2018 "Neo4j," |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.neo4j.docs.driver; |
| 20 | + |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.HashSet; |
| 23 | + |
| 24 | +import org.neo4j.driver.v1.AccessMode; |
| 25 | +import org.neo4j.driver.v1.AuthTokens; |
| 26 | +import org.neo4j.driver.v1.Config; |
| 27 | +import org.neo4j.driver.v1.Driver; |
| 28 | +import org.neo4j.driver.v1.GraphDatabase; |
| 29 | +import org.neo4j.driver.v1.Session; |
| 30 | +import org.neo4j.driver.v1.StatementResult; |
| 31 | +import org.neo4j.driver.v1.net.ServerAddress; |
| 32 | + |
| 33 | +import static org.neo4j.driver.v1.Values.parameters; |
| 34 | + |
| 35 | +public class ConfigCustomResolverExample implements AutoCloseable |
| 36 | +{ |
| 37 | + private final Driver driver; |
| 38 | + |
| 39 | + public ConfigCustomResolverExample( String virtualUri, ServerAddress... addresses ) |
| 40 | + { |
| 41 | + driver = GraphDatabase.driver( virtualUri, AuthTokens.none(), |
| 42 | + Config.build().withoutEncryption().withResolver( address -> new HashSet<>( Arrays.asList( addresses ) ) ).toConfig() ); |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + // tag::config-custom-resolver[] |
| 47 | + private Driver createDriver( String virtualUri, String user, String password, ServerAddress... addresses ) |
| 48 | + { |
| 49 | + return GraphDatabase.driver( virtualUri, AuthTokens.basic( user, password ), |
| 50 | + Config.build().withResolver( address -> new HashSet<>( Arrays.asList( addresses ) ) ).toConfig() ); |
| 51 | + } |
| 52 | + |
| 53 | + private void addPerson( String name ) |
| 54 | + { |
| 55 | + String username = "neo4j"; |
| 56 | + String password = "some password"; |
| 57 | + |
| 58 | + try ( Driver driver = createDriver( "bolt+routing://x.acme.com", username, password, ServerAddress.of( "a.acme.com", 7676 ), |
| 59 | + ServerAddress.of( "b.acme.com", 8787 ), ServerAddress.of( "c.acme.com", 9898 ) ) ) |
| 60 | + { |
| 61 | + try ( Session session = driver.session( AccessMode.WRITE ) ) |
| 62 | + { |
| 63 | + session.run( "CREATE (a:Person {name: $name})", parameters( "name", name ) ); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + // end::config-custom-resolver[] |
| 68 | + |
| 69 | + @Override |
| 70 | + public void close() throws Exception |
| 71 | + { |
| 72 | + driver.close(); |
| 73 | + } |
| 74 | + |
| 75 | + public boolean canConnect() |
| 76 | + { |
| 77 | + StatementResult result = driver.session( AccessMode.READ ).run( "RETURN 1" ); |
| 78 | + return result.single().get( 0 ).asInt() == 1; |
| 79 | + } |
| 80 | +} |
0 commit comments