Skip to content

Commit 9a8d9ec

Browse files
committed
Add custom resolver example
1 parent d6f1f86 commit 9a8d9ec

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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, String user, String password, ServerAddress[] addresses )
40+
{
41+
driver = createDriver( virtualUri, user, password, addresses );
42+
}
43+
44+
// tag::config-custom-resolver[]
45+
private Driver createDriver( String virtualUri, String user, String password, ServerAddress... addresses )
46+
{
47+
return GraphDatabase.driver( virtualUri, AuthTokens.basic( user, password ),
48+
Config.build().withResolver( address -> new HashSet<>( Arrays.asList( addresses ) ) ).toConfig() );
49+
}
50+
51+
private void addPerson( String name )
52+
{
53+
String username = "neo4j";
54+
String password = "some password";
55+
56+
try ( Driver driver = createDriver( "bolt+routing://x.acme.com", username, password, ServerAddress.of( "a.acme.com", 7676 ),
57+
ServerAddress.of( "b.acme.com", 8787 ), ServerAddress.of( "c.acme.com", 9898 ) ) )
58+
{
59+
try ( Session session = driver.session( AccessMode.WRITE ) )
60+
{
61+
session.run( "CREATE (a:Person {name: $name})", parameters( "name", name ) );
62+
}
63+
}
64+
}
65+
// end::config-custom-resolver[]
66+
67+
@Override
68+
public void close() throws Exception
69+
{
70+
driver.close();
71+
}
72+
73+
public boolean canConnect()
74+
{
75+
StatementResult result = driver.session().run( "RETURN 1" );
76+
return result.single().get( 0 ).asInt() == 1;
77+
}
78+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.RegisterExtension;
23+
24+
import org.neo4j.driver.v1.net.ServerAddress;
25+
import org.neo4j.driver.v1.util.cc.Cluster;
26+
import org.neo4j.driver.v1.util.cc.ClusterExtension;
27+
28+
import static org.junit.jupiter.api.Assertions.assertTrue;
29+
import static org.neo4j.driver.v1.util.Neo4jRunner.PASSWORD;
30+
import static org.neo4j.driver.v1.util.Neo4jRunner.USER;
31+
32+
class ExamplesClusterIT
33+
{
34+
@RegisterExtension
35+
static final ClusterExtension neo4j = new ClusterExtension();
36+
37+
@Test
38+
void testShouldRunConfigCustomResolverExample() throws Exception
39+
{
40+
Cluster cluster = neo4j.getCluster();
41+
42+
// Given
43+
try ( ConfigCustomResolverExample example = new ConfigCustomResolverExample( "bolt+routing://x.acme.com", USER, PASSWORD,
44+
cluster.cores().stream().map( c -> c.getBoltAddress() ).toArray( i -> new ServerAddress[i] ) ) )
45+
{
46+
// Then
47+
assertTrue( example.canConnect() );
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)