Skip to content

Commit b583401

Browse files
authored
Merge pull request #536 from ali-ince/1.7-example-address-resolver
Add custom resolver example
2 parents 346c1d2 + c274976 commit b583401

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
23+
import org.neo4j.driver.v1.net.ServerAddress;
24+
import org.neo4j.driver.v1.util.StubServer;
25+
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
import static org.junit.jupiter.api.Assertions.assertTrue;
28+
29+
public class ExamplesStubIT
30+
{
31+
@Test
32+
void testShouldRunConfigCustomResolverExample() throws Exception
33+
{
34+
StubServer server1 = StubServer.start( "get_routing_table_only.script", 9001 );
35+
StubServer server2 = StubServer.start( "return_1.script", 9002 );
36+
37+
// Given
38+
try ( ConfigCustomResolverExample example = new ConfigCustomResolverExample( "bolt+routing://x.acme.com", ServerAddress.of( "localhost", 9001 ) ) )
39+
{
40+
// Then
41+
assertTrue( example.canConnect() );
42+
}
43+
finally
44+
{
45+
assertEquals( 0, server1.exitStatus() );
46+
assertEquals( 0, server2.exitStatus() );
47+
}
48+
}
49+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
!: AUTO INIT
2+
!: AUTO RESET
3+
!: AUTO PULL_ALL
4+
5+
S: SUCCESS {"server": "Neo4j/3.2.2"}
6+
C: RUN "CALL dbms.cluster.routing.getRoutingTable({context})" {"context": {}}
7+
PULL_ALL
8+
S: SUCCESS {"fields": ["ttl", "servers"]}
9+
RECORD [9223372036854775807, [{"addresses": ["127.0.0.1:9001"],"role": "WRITE"}, {"addresses": ["127.0.0.1:9002"], "role": "READ"},{"addresses": ["127.0.0.1:9001"], "role": "ROUTE"}]]
10+
SUCCESS {}
11+
S: <EXIT>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
!: AUTO INIT
2+
!: AUTO RESET
3+
!: AUTO PULL_ALL
4+
!: AUTO RUN "COMMIT" {}
5+
!: AUTO RUN "ROLLBACK" {}
6+
!: AUTO RUN "BEGIN" {}
7+
8+
C: RUN "RETURN 1" {}
9+
PULL_ALL
10+
S: SUCCESS {"fields": ["1"]}
11+
RECORD [1]
12+
SUCCESS {}

0 commit comments

Comments
 (0)