Skip to content

Commit c7f9792

Browse files
Sgitariopedja4
authored andcommitted
BAEL-1108 (eugenp#3766)
* BAEL-399: A Guide to Multitenancy in Hibernate 5 * Removed unused properties in profile 2 * Changes after code review * Add XML, JavaConfig and Autowired examples. * BAEL-1517: Added Java7 style assertions * BAEL-1517: Upgrade AssertJ to 3.9.0; add Java 8 style assertion tests * Revert "Add XML, JavaConfig and Autowired examples." This reverts commit 8f4df6b. * BAEL-1517: Editor Review changes * BAEL-1517: Formatting... * BAEL-1572: AWS EC2 examples * BAEL-1108 * BAEL-1570 (eugenp#3762) * Create pom.xml * Update pom.xml * Create pom.xml * Update pom.xml * add impl * add app * minor cleanup * testing work * testing work * testing work * testing work * cleanup work * unused import cleanup * cleanup work for tests * formatting work * testing cleanup * minior rename * cleanup work * added link to article (eugenp#3768) * Fixed tests and renamed test names * BAEL-1108 Formatting
1 parent 1bb98e4 commit c7f9792

8 files changed

Lines changed: 410 additions & 0 deletions

File tree

‎apache-curator/pom.xml‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<artifactId>apache-curator</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
<packaging>jar</packaging>
7+
8+
<parent>
9+
<groupId>com.baeldung</groupId>
10+
<artifactId>parent-modules</artifactId>
11+
<version>1.0.0-SNAPSHOT</version>
12+
</parent>
13+
14+
<properties>
15+
<curator.version>4.0.1</curator.version>
16+
<zookeeper.version>3.4.11</zookeeper.version>
17+
<jackson-databind.version>2.9.4</jackson-databind.version>
18+
19+
<!-- testing -->
20+
<assertj.version>3.6.1</assertj.version>
21+
22+
</properties>
23+
24+
<dependencies>
25+
26+
<!-- curator -->
27+
28+
<dependency>
29+
<groupId>org.apache.curator</groupId>
30+
<artifactId>curator-x-async</artifactId>
31+
<version>${curator.version}</version>
32+
<exclusions>
33+
<exclusion>
34+
<groupId>org.apache.zookeeper</groupId>
35+
<artifactId>zookeeper</artifactId>
36+
</exclusion>
37+
</exclusions>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.apache.curator</groupId>
42+
<artifactId>curator-recipes</artifactId>
43+
<version>${curator.version}</version>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.apache.zookeeper</groupId>
48+
<artifactId>zookeeper</artifactId>
49+
<version>${zookeeper.version}</version>
50+
</dependency>
51+
52+
<!-- utils -->
53+
<dependency>
54+
<groupId>com.fasterxml.jackson.core</groupId>
55+
<artifactId>jackson-databind</artifactId>
56+
<version>${jackson-databind.version}</version>
57+
</dependency>
58+
59+
<!-- test scoped -->
60+
61+
<dependency>
62+
<groupId>org.assertj</groupId>
63+
<artifactId>assertj-core</artifactId>
64+
<version>${assertj.version}</version>
65+
<scope>test</scope>
66+
</dependency>
67+
</dependencies>
68+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.apache.curator.modeled;
2+
3+
public class HostConfig {
4+
private String hostname;
5+
private int port;
6+
7+
public HostConfig() {
8+
9+
}
10+
11+
public HostConfig(String hostname, int port) {
12+
this.hostname = hostname;
13+
this.port = port;
14+
}
15+
16+
public int getPort() {
17+
return port;
18+
}
19+
20+
public void setPort(int port) {
21+
this.port = port;
22+
}
23+
24+
public String getHostname() {
25+
return hostname;
26+
}
27+
28+
public void setHostname(String hostname) {
29+
this.hostname = hostname;
30+
}
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.apache.curator;
2+
3+
import org.apache.curator.RetryPolicy;
4+
import org.apache.curator.framework.CuratorFramework;
5+
import org.apache.curator.framework.CuratorFrameworkFactory;
6+
import org.apache.curator.retry.RetryNTimes;
7+
import org.junit.Before;
8+
9+
public abstract class BaseTest {
10+
11+
@Before
12+
public void setup() {
13+
org.apache.log4j.BasicConfigurator.configure();
14+
}
15+
16+
protected CuratorFramework newClient() {
17+
int sleepMsBetweenRetries = 100;
18+
int maxRetries = 3;
19+
RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
20+
return CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy);
21+
}
22+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.baeldung.apache.curator.configuration;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.UUID;
8+
import java.util.concurrent.atomic.AtomicBoolean;
9+
10+
import org.apache.curator.framework.CuratorFramework;
11+
import org.apache.curator.x.async.AsyncCuratorFramework;
12+
import org.junit.Test;
13+
14+
import com.baeldung.apache.curator.BaseTest;
15+
16+
public class ConfigurationManagementManualTest extends BaseTest {
17+
18+
private static final String KEY_FORMAT = "/%s";
19+
20+
@Test
21+
public void givenPath_whenCreateKey_thenValueIsStored() throws Exception {
22+
try (CuratorFramework client = newClient()) {
23+
client.start();
24+
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
25+
String key = getKey();
26+
String expected = "my_value";
27+
28+
// Create key nodes structure
29+
client
30+
.create()
31+
.forPath(key);
32+
33+
// Set data value for our key
34+
async
35+
.setData()
36+
.forPath(key, expected.getBytes());
37+
38+
// Get data value
39+
AtomicBoolean isEquals = new AtomicBoolean();
40+
async
41+
.getData()
42+
.forPath(key)
43+
.thenAccept(data -> isEquals.set(new String(data).equals(expected)));
44+
45+
Thread.sleep(1000);
46+
47+
assertThat(isEquals.get()).isTrue();
48+
}
49+
}
50+
51+
@Test
52+
public void givenPath_whenWatchAKeyAndStoreAValue_thenWatcherIsTriggered() throws Exception {
53+
try (CuratorFramework client = newClient()) {
54+
client.start();
55+
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
56+
String key = getKey();
57+
String expected = "my_value";
58+
59+
// Create key structure
60+
async
61+
.create()
62+
.forPath(key);
63+
64+
List<String> changes = new ArrayList<>();
65+
66+
// Watch data value
67+
async
68+
.watched()
69+
.getData()
70+
.forPath(key)
71+
.event()
72+
.thenAccept(watchedEvent -> {
73+
try {
74+
changes.add(new String(client
75+
.getData()
76+
.forPath(watchedEvent.getPath())));
77+
} catch (Exception e) {
78+
// fail ...
79+
}
80+
});
81+
82+
// Set data value for our key
83+
async
84+
.setData()
85+
.forPath(key, expected.getBytes());
86+
87+
Thread.sleep(1000);
88+
89+
assertThat(changes.size() > 0).isTrue();
90+
}
91+
}
92+
93+
private String getKey() {
94+
return String.format(KEY_FORMAT, UUID
95+
.randomUUID()
96+
.toString());
97+
}
98+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.apache.curator.connection;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.concurrent.atomic.AtomicBoolean;
6+
7+
import org.apache.curator.RetryPolicy;
8+
import org.apache.curator.framework.CuratorFramework;
9+
import org.apache.curator.framework.CuratorFrameworkFactory;
10+
import org.apache.curator.retry.RetryNTimes;
11+
import org.apache.curator.x.async.AsyncCuratorFramework;
12+
import org.junit.Test;
13+
14+
public class ConnectionManagementManualTest {
15+
16+
@Test
17+
public void givenRunningZookeeper_whenOpenConnection_thenClientIsOpened() throws Exception {
18+
int sleepMsBetweenRetries = 100;
19+
int maxRetries = 3;
20+
RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
21+
22+
try (CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy)) {
23+
client.start();
24+
assertThat(client
25+
.checkExists()
26+
.forPath("/")).isNotNull();
27+
}
28+
}
29+
30+
@Test
31+
public void givenRunningZookeeper_whenOpenConnectionUsingAsyncNotBlocking_thenClientIsOpened() throws InterruptedException {
32+
int sleepMsBetweenRetries = 100;
33+
int maxRetries = 3;
34+
RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
35+
36+
try (CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy)) {
37+
client.start();
38+
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
39+
40+
AtomicBoolean exists = new AtomicBoolean(false);
41+
async
42+
.checkExists()
43+
.forPath("/")
44+
.thenAcceptAsync(s -> exists.set(s != null));
45+
Thread.sleep(100);
46+
assertThat(exists.get()).isTrue();
47+
}
48+
}
49+
50+
@Test
51+
public void givenRunningZookeeper_whenOpenConnectionUsingAsyncBlocking_thenClientIsOpened() throws InterruptedException {
52+
int sleepMsBetweenRetries = 100;
53+
int maxRetries = 3;
54+
RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
55+
56+
try (CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy)) {
57+
client.start();
58+
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
59+
60+
AtomicBoolean exists = new AtomicBoolean(false);
61+
async
62+
.checkExists()
63+
.forPath("/")
64+
.thenAccept(s -> exists.set(s != null));
65+
Thread.sleep(100);
66+
assertThat(exists.get()).isTrue();
67+
}
68+
}
69+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.apache.curator.modeled;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.fail;
5+
6+
import org.apache.curator.framework.CuratorFramework;
7+
import org.apache.curator.x.async.AsyncCuratorFramework;
8+
import org.apache.curator.x.async.modeled.JacksonModelSerializer;
9+
import org.apache.curator.x.async.modeled.ModelSpec;
10+
import org.apache.curator.x.async.modeled.ModeledFramework;
11+
import org.apache.curator.x.async.modeled.ZPath;
12+
import org.junit.Test;
13+
14+
import com.baeldung.apache.curator.BaseTest;
15+
16+
public class ModelTypedExamplesManualTest extends BaseTest {
17+
18+
@Test
19+
public void givenPath_whenStoreAModel_thenNodesAreCreated() throws InterruptedException {
20+
21+
ModelSpec<HostConfig> mySpec = ModelSpec
22+
.builder(ZPath.parseWithIds("/config/dev"), JacksonModelSerializer.build(HostConfig.class))
23+
.build();
24+
25+
try (CuratorFramework client = newClient()) {
26+
client.start();
27+
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
28+
ModeledFramework<HostConfig> modeledClient = ModeledFramework.wrap(async, mySpec);
29+
30+
modeledClient.set(new HostConfig("host-name", 8080));
31+
32+
modeledClient
33+
.read()
34+
.whenComplete((value, e) -> {
35+
if (e != null) {
36+
fail("Cannot read host config", e);
37+
} else {
38+
assertThat(value).isNotNull();
39+
assertThat(value.getHostname()).isEqualTo("host-name");
40+
assertThat(value.getPort()).isEqualTo(8080);
41+
}
42+
43+
});
44+
}
45+
46+
}
47+
}

0 commit comments

Comments
 (0)