Search in sources :

Example 1 with AsyncCuratorFramework

use of org.apache.curator.x.async.AsyncCuratorFramework in project tutorials by eugenp.

the class ConfigurationManagementManualTest method givenPath_whenWatchAKeyAndStoreAValue_thenWatcherIsTriggered.

@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());
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) AsyncCuratorFramework(org.apache.curator.x.async.AsyncCuratorFramework) AsyncCuratorFramework(org.apache.curator.x.async.AsyncCuratorFramework) ArrayList(java.util.ArrayList) BaseTest(com.baeldung.apache.curator.BaseTest) Test(org.junit.Test)

Example 2 with AsyncCuratorFramework

use of org.apache.curator.x.async.AsyncCuratorFramework in project tutorials by eugenp.

the class ConfigurationManagementManualTest method givenPath_whenCreateKey_thenValueIsStored.

@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());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CuratorFramework(org.apache.curator.framework.CuratorFramework) AsyncCuratorFramework(org.apache.curator.x.async.AsyncCuratorFramework) AsyncCuratorFramework(org.apache.curator.x.async.AsyncCuratorFramework) BaseTest(com.baeldung.apache.curator.BaseTest) Test(org.junit.Test)

Example 3 with AsyncCuratorFramework

use of org.apache.curator.x.async.AsyncCuratorFramework in project tutorials by eugenp.

the class ConnectionManagementManualTest method givenRunningZookeeper_whenOpenConnectionUsingAsyncNotBlocking_thenClientIsOpened.

@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());
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CuratorFramework(org.apache.curator.framework.CuratorFramework) AsyncCuratorFramework(org.apache.curator.x.async.AsyncCuratorFramework) AsyncCuratorFramework(org.apache.curator.x.async.AsyncCuratorFramework) RetryPolicy(org.apache.curator.RetryPolicy) Test(org.junit.Test)

Example 4 with AsyncCuratorFramework

use of org.apache.curator.x.async.AsyncCuratorFramework in project tutorials by eugenp.

the class ConnectionManagementManualTest method givenRunningZookeeper_whenOpenConnectionUsingAsyncBlocking_thenClientIsOpened.

@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());
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CuratorFramework(org.apache.curator.framework.CuratorFramework) AsyncCuratorFramework(org.apache.curator.x.async.AsyncCuratorFramework) AsyncCuratorFramework(org.apache.curator.x.async.AsyncCuratorFramework) RetryPolicy(org.apache.curator.RetryPolicy) Test(org.junit.Test)

Example 5 with AsyncCuratorFramework

use of org.apache.curator.x.async.AsyncCuratorFramework in project tutorials by eugenp.

the class ModelTypedExamplesManualTest method givenPath_whenStoreAModel_thenNodesAreCreated.

@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);
            }
        });
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) AsyncCuratorFramework(org.apache.curator.x.async.AsyncCuratorFramework) AsyncCuratorFramework(org.apache.curator.x.async.AsyncCuratorFramework) BaseTest(com.baeldung.apache.curator.BaseTest) Test(org.junit.Test)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)5 AsyncCuratorFramework (org.apache.curator.x.async.AsyncCuratorFramework)5 Test (org.junit.Test)5 BaseTest (com.baeldung.apache.curator.BaseTest)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 RetryPolicy (org.apache.curator.RetryPolicy)2 RetryNTimes (org.apache.curator.retry.RetryNTimes)2 ArrayList (java.util.ArrayList)1