JAVA-1848: Moved apache-curator to apache-libraries

This commit is contained in:
sampadawagde 2020-06-12 12:11:48 +05:30
parent d071ec2717
commit b192e90444
9 changed files with 0 additions and 434 deletions

View File

@ -1,7 +0,0 @@
## Apache Curator
This module contains articles about Apache Curator
### Relevant Articles:
- [Introduction to Apache Curator](https://www.baeldung.com/apache-curator)

View File

@ -1,70 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>apache-curator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>apache-curator</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<!-- curator -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-async</artifactId>
<version>${curator.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<!-- utils -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>${avaitility.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<curator.version>4.0.1</curator.version>
<zookeeper.version>3.4.11</zookeeper.version>
<!-- testing -->
<assertj.version>3.6.1</assertj.version>
<avaitility.version>1.7.0</avaitility.version>
</properties>
</project>

View File

@ -1,31 +0,0 @@
package com.baeldung.apache.curator.modeled;
public class HostConfig {
private String hostname;
private int port;
public HostConfig() {
}
public HostConfig(String hostname, int port) {
this.hostname = hostname;
this.port = port;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
}

View File

@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -1,22 +0,0 @@
package com.baeldung.apache.curator;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.junit.Before;
public abstract class BaseManualTest {
@Before
public void setup() {
org.apache.log4j.BasicConfigurator.configure();
}
protected CuratorFramework newClient() {
int sleepMsBetweenRetries = 100;
int maxRetries = 3;
RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
return CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy);
}
}

View File

@ -1,89 +0,0 @@
package com.baeldung.apache.curator.configuration;
import static com.jayway.awaitility.Awaitility.await;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.x.async.AsyncCuratorFramework;
import org.junit.Test;
import com.baeldung.apache.curator.BaseManualTest;
public class ConfigurationManagementManualTest extends BaseManualTest {
private static final String KEY_FORMAT = "/%s";
@Test
public void givenPath_whenCreateKey_thenValueIsStored() throws Exception {
try (CuratorFramework client = newClient()) {
client.start();
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
String key = getKey();
String expected = "my_value";
// Create key nodes structure
client.create()
.forPath(key);
// Set data value for our key
async.setData()
.forPath(key, expected.getBytes());
// Get data value
AtomicBoolean isEquals = new AtomicBoolean();
async.getData()
.forPath(key)
.thenAccept(
data -> isEquals.set(new String(data).equals(expected)));
await().until(() -> assertThat(isEquals.get()).isTrue());
}
}
@Test
public void givenPath_whenWatchAKeyAndStoreAValue_thenWatcherIsTriggered()
throws Exception {
try (CuratorFramework client = newClient()) {
client.start();
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
String key = getKey();
String expected = "my_value";
// Create key structure
async.create()
.forPath(key);
List<String> changes = new ArrayList<>();
// Watch data value
async.watched()
.getData()
.forPath(key)
.event()
.thenAccept(watchedEvent -> {
try {
changes.add(new String(client.getData()
.forPath(watchedEvent.getPath())));
} catch (Exception e) {
// fail ...
}
});
// Set data value for our key
async.setData()
.forPath(key, expected.getBytes());
await().until(() -> assertThat(changes.size() > 0).isTrue());
}
}
private String getKey() {
return String.format(KEY_FORMAT, UUID.randomUUID()
.toString());
}
}

View File

@ -1,79 +0,0 @@
package com.baeldung.apache.curator.connection;
import static com.jayway.awaitility.Awaitility.await;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.apache.curator.x.async.AsyncCuratorFramework;
import org.junit.Test;
public class ConnectionManagementManualTest {
@Test
public void givenRunningZookeeper_whenOpenConnection_thenClientIsOpened()
throws Exception {
int sleepMsBetweenRetries = 100;
int maxRetries = 3;
RetryPolicy retryPolicy = new RetryNTimes(maxRetries,
sleepMsBetweenRetries);
try (CuratorFramework client = CuratorFrameworkFactory
.newClient("127.0.0.1:2181", retryPolicy)) {
client.start();
assertThat(client.checkExists()
.forPath("/")).isNotNull();
}
}
@Test
public void givenRunningZookeeper_whenOpenConnectionUsingAsyncNotBlocking_thenClientIsOpened()
throws InterruptedException {
int sleepMsBetweenRetries = 100;
int maxRetries = 3;
RetryPolicy retryPolicy = new RetryNTimes(maxRetries,
sleepMsBetweenRetries);
try (CuratorFramework client = CuratorFrameworkFactory
.newClient("127.0.0.1:2181", retryPolicy)) {
client.start();
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
AtomicBoolean exists = new AtomicBoolean(false);
async.checkExists()
.forPath("/")
.thenAcceptAsync(s -> exists.set(s != null));
await().until(() -> assertThat(exists.get()).isTrue());
}
}
@Test
public void givenRunningZookeeper_whenOpenConnectionUsingAsyncBlocking_thenClientIsOpened()
throws InterruptedException {
int sleepMsBetweenRetries = 100;
int maxRetries = 3;
RetryPolicy retryPolicy = new RetryNTimes(maxRetries,
sleepMsBetweenRetries);
try (CuratorFramework client = CuratorFrameworkFactory
.newClient("127.0.0.1:2181", retryPolicy)) {
client.start();
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
AtomicBoolean exists = new AtomicBoolean(false);
async.checkExists()
.forPath("/")
.thenAccept(s -> exists.set(s != null));
await().until(() -> assertThat(exists.get()).isTrue());
}
}
}

View File

@ -1,49 +0,0 @@
package com.baeldung.apache.curator.modeled;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.x.async.AsyncCuratorFramework;
import org.apache.curator.x.async.modeled.JacksonModelSerializer;
import org.apache.curator.x.async.modeled.ModelSpec;
import org.apache.curator.x.async.modeled.ModeledFramework;
import org.apache.curator.x.async.modeled.ZPath;
import org.junit.Test;
import com.baeldung.apache.curator.BaseManualTest;
public class ModelTypedExamplesManualTest extends BaseManualTest {
@Test
public void givenPath_whenStoreAModel_thenNodesAreCreated()
throws InterruptedException {
ModelSpec<HostConfig> mySpec = ModelSpec
.builder(ZPath.parseWithIds("/config/dev"),
JacksonModelSerializer.build(HostConfig.class))
.build();
try (CuratorFramework client = newClient()) {
client.start();
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
ModeledFramework<HostConfig> modeledClient = ModeledFramework
.wrap(async, mySpec);
modeledClient.set(new HostConfig("host-name", 8080));
modeledClient.read()
.whenComplete((value, e) -> {
if (e != null) {
fail("Cannot read host config", e);
} else {
assertThat(value).isNotNull();
assertThat(value.getHostname()).isEqualTo("host-name");
assertThat(value.getPort()).isEqualTo(8080);
}
});
}
}
}

View File

@ -1,74 +0,0 @@
package com.baeldung.apache.curator.recipes;
import static org.assertj.core.api.Assertions.assertThat;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.leader.LeaderSelector;
import org.apache.curator.framework.recipes.leader.LeaderSelectorListener;
import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex;
import org.apache.curator.framework.recipes.shared.SharedCount;
import org.apache.curator.framework.state.ConnectionState;
import org.junit.Test;
import com.baeldung.apache.curator.BaseManualTest;
public class RecipesManualTest extends BaseManualTest {
@Test
public void givenRunningZookeeper_whenUsingLeaderElection_thenNoErrors() {
try (CuratorFramework client = newClient()) {
client.start();
LeaderSelector leaderSelector = new LeaderSelector(client, "/mutex/select/leader/for/job/A", new LeaderSelectorListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
}
@Override
public void takeLeadership(CuratorFramework client) throws Exception {
// I'm the leader of the job A !
}
});
leaderSelector.start();
// Wait until the job A is done among all the members
leaderSelector.close();
}
}
@Test
public void givenRunningZookeeper_whenUsingSharedLock_thenNoErrors() throws Exception {
try (CuratorFramework client = newClient()) {
client.start();
InterProcessSemaphoreMutex sharedLock = new InterProcessSemaphoreMutex(client, "/mutex/process/A");
sharedLock.acquire();
// Do process A
sharedLock.release();
}
}
@Test
public void givenRunningZookeeper_whenUsingSharedCounter_thenCounterIsIncrement() throws Exception {
try (CuratorFramework client = newClient()) {
client.start();
try (SharedCount counter = new SharedCount(client, "/counters/A", 0)) {
counter.start();
counter.setCount(0);
counter.setCount(counter.getCount() + 1);
assertThat(counter.getCount()).isEqualTo(1);
}
}
}
}