|
18 | 18 | */ |
19 | 19 | package org.neo4j.docs.driver; |
20 | 20 |
|
21 | | -import org.neo4j.driver.AccessMode; |
22 | 21 | import org.neo4j.driver.AuthToken; |
23 | 22 | import org.neo4j.driver.AuthTokens; |
24 | 23 | import org.neo4j.driver.Config; |
25 | | -import org.neo4j.driver.Driver; |
26 | 24 | import org.neo4j.driver.GraphDatabase; |
27 | 25 | import org.neo4j.driver.net.ServerAddress; |
28 | 26 |
|
29 | | -import java.util.Arrays; |
30 | | -import java.util.HashSet; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Set; |
| 29 | +import java.util.UUID; |
31 | 30 |
|
32 | | -import static org.neo4j.driver.SessionConfig.builder; |
33 | | -import static org.neo4j.driver.Values.parameters; |
34 | | - |
35 | | -public class ConfigCustomResolverExample implements AutoCloseable { |
36 | | - private final Driver driver; |
37 | | - |
38 | | - public ConfigCustomResolverExample(String virtualUri, AuthToken authToken, ServerAddress... addresses) { |
39 | | - var config = Config.builder() |
40 | | - .withResolver(address -> new HashSet<>(Arrays.asList(addresses))) |
41 | | - .build(); |
42 | | - |
43 | | - driver = GraphDatabase.driver(virtualUri, authToken, config); |
| 31 | +public class ConfigCustomResolverExample { |
| 32 | + @SuppressWarnings("unused") |
| 33 | + // tag::config-custom-resolver[] |
| 34 | + public void addExampleNode() { |
| 35 | + var addresses = Set.of( |
| 36 | + ServerAddress.of("a.example.com", 7676), |
| 37 | + ServerAddress.of("b.example.com", 8787), |
| 38 | + ServerAddress.of("c.example.com", 9898) |
| 39 | + ); |
| 40 | + addNode("neo4j://x.example.com", AuthTokens.basic("neo4j", "some password"), addresses, UUID.randomUUID().toString()); |
44 | 41 | } |
45 | 42 |
|
46 | | - // tag::config-custom-resolver[] |
47 | | - @SuppressWarnings("SameParameterValue") |
48 | | - private Driver createDriver(String virtualUri, String user, String password, ServerAddress... addresses) { |
| 43 | + public void addNode(String virtualUri, AuthToken authToken, Set<ServerAddress> addresses, String id) { |
49 | 44 | var config = Config.builder() |
50 | | - .withResolver(address -> new HashSet<>(Arrays.asList(addresses))) |
| 45 | + .withResolver(address -> addresses) |
51 | 46 | .build(); |
52 | | - |
53 | | - return GraphDatabase.driver(virtualUri, AuthTokens.basic(user, password), config); |
54 | | - } |
55 | | - |
56 | | - private void addPerson(String name) { |
57 | | - var username = "neo4j"; |
58 | | - var password = "some password"; |
59 | | - |
60 | | - try (var driver = createDriver( |
61 | | - "neo4j://x.example.com", |
62 | | - username, |
63 | | - password, |
64 | | - ServerAddress.of("a.example.com", 7676), |
65 | | - ServerAddress.of("b.example.com", 8787), |
66 | | - ServerAddress.of("c.example.com", 9898))) { |
67 | | - try (var session = driver.session(builder().withDefaultAccessMode(AccessMode.WRITE).build())) { |
68 | | - session.run("CREATE (a:Person {name: $name})", parameters("name", name)); |
69 | | - } |
| 47 | + try (var driver = GraphDatabase.driver(virtualUri, authToken, config)) { |
| 48 | + driver.executableQuery("CREATE ({id: $id})") |
| 49 | + .withParameters(Map.of("id", id)) |
| 50 | + .execute(); |
70 | 51 | } |
71 | 52 | } |
72 | 53 | // end::config-custom-resolver[] |
73 | | - |
74 | | - @Override |
75 | | - public void close() throws RuntimeException { |
76 | | - driver.close(); |
77 | | - } |
78 | | - |
79 | | - public boolean canConnect() { |
80 | | - var result = driver.session(builder().withDefaultAccessMode(AccessMode.READ).build()).run("RETURN 1"); |
81 | | - return result.single().get(0).asInt() == 1; |
82 | | - } |
83 | 54 | } |
0 commit comments