Example usage for org.springframework.integration.zookeeper.metadata ZookeeperMetadataStore putIfAbsent

List of usage examples for org.springframework.integration.zookeeper.metadata ZookeeperMetadataStore putIfAbsent

Introduction

In this page you can find the example usage for org.springframework.integration.zookeeper.metadata ZookeeperMetadataStore putIfAbsent.

Prototype

@Override
    public String putIfAbsent(String key, String value) 

Source Link

Usage

From source file:org.springframework.integration.zookeeper.metadata.ZookeeperMetadataStoreTests.java

@Test
public void testPutIfAbsent() throws Exception {
    final String testKey = "ZookeeperMetadataStoreTests-Persist";
    final String testKey2 = "ZookeeperMetadataStoreTests-Persist-2";
    metadataStore.put(testKey, "Integration");
    assertNotNull(client.checkExists().forPath(metadataStore.getPath(testKey)));
    assertEquals("Integration",
            IntegrationUtils.bytesToString(client.getData().forPath(metadataStore.getPath(testKey)), "UTF-8"));
    CuratorFramework otherClient = createNewClient();
    final ZookeeperMetadataStore otherMetadataStore = new ZookeeperMetadataStore(otherClient);
    otherMetadataStore.start();/*from   w  w  w.  j  a v a  2 s .com*/
    otherMetadataStore.putIfAbsent(testKey, "OtherValue");
    assertEquals("Integration",
            IntegrationUtils.bytesToString(client.getData().forPath(metadataStore.getPath(testKey)), "UTF-8"));
    assertEquals("Integration", metadataStore.get(testKey));
    assertThat("Integration", eventually(equalsResult(new Evaluator<String>() {
        @Override
        public String evaluate() {
            return otherMetadataStore.get(testKey);
        }
    })));
    otherMetadataStore.putIfAbsent(testKey2, "Integration-2");
    assertEquals("Integration-2",
            IntegrationUtils.bytesToString(client.getData().forPath(metadataStore.getPath(testKey2)), "UTF-8"));
    assertEquals("Integration-2", otherMetadataStore.get(testKey2));
    assertThat("Integration-2", eventually(equalsResult(new Evaluator<String>() {
        @Override
        public String evaluate() {
            return metadataStore.get(testKey2);
        }
    })));
    closeClient(otherClient);
}

From source file:org.springframework.integration.zookeeper.metadata.ZookeeperMetadataStoreTests.java

@Test
public void testListenerInvokedOnRemoteChanges() throws Exception {
    String testKey = "ZookeeperMetadataStoreTests";

    CuratorFramework otherClient = createNewClient();
    ZookeeperMetadataStore otherMetadataStore = new ZookeeperMetadataStore(otherClient);

    // register listeners
    final List<List<String>> notifiedChanges = new ArrayList<List<String>>();
    final Map<String, CyclicBarrier> barriers = new HashMap<String, CyclicBarrier>();
    barriers.put("add", new CyclicBarrier(2));
    barriers.put("remove", new CyclicBarrier(2));
    barriers.put("update", new CyclicBarrier(2));
    metadataStore.addListener(new MetadataStoreListenerAdapter() {
        @Override//from ww  w. j a  va 2  s  .  c o  m
        public void onAdd(String key, String value) {
            notifiedChanges.add(Arrays.asList("add", key, value));
            waitAtBarrier("add", barriers);
        }

        @Override
        public void onRemove(String key, String oldValue) {
            notifiedChanges.add(Arrays.asList("remove", key, oldValue));
            waitAtBarrier("remove", barriers);
        }

        @Override
        public void onUpdate(String key, String newValue) {
            notifiedChanges.add(Arrays.asList("update", key, newValue));
            waitAtBarrier("update", barriers);
        }
    });

    // the tests themselves
    barriers.get("add").reset();
    otherMetadataStore.put(testKey, "Integration");
    waitAtBarrier("add", barriers);
    assertThat(notifiedChanges, hasSize(1));
    assertThat(notifiedChanges.get(0), IsIterableContainingInOrder.contains("add", testKey, "Integration"));

    otherMetadataStore.putIfAbsent(testKey, "Integration++");
    // there is no update and therefore we expect no changes
    assertThat(notifiedChanges, hasSize(1));

    barriers.get("update").reset();
    otherMetadataStore.put(testKey, "Integration-2");
    waitAtBarrier("update", barriers);
    assertThat(notifiedChanges, hasSize(2));
    assertThat(notifiedChanges.get(1),
            IsIterableContainingInOrder.contains("update", testKey, "Integration-2"));

    barriers.get("update").reset();
    otherMetadataStore.replace(testKey, "Integration-2", "Integration-3");
    waitAtBarrier("update", barriers);
    assertThat(notifiedChanges, hasSize(3));
    assertThat(notifiedChanges.get(2),
            IsIterableContainingInOrder.contains("update", testKey, "Integration-3"));

    otherMetadataStore.replace(testKey, "Integration-2", "Integration-none");
    assertThat(notifiedChanges, hasSize(3));

    barriers.get("remove").reset();
    otherMetadataStore.remove(testKey);
    waitAtBarrier("remove", barriers);
    assertThat(notifiedChanges, hasSize(4));
    assertThat(notifiedChanges.get(3),
            IsIterableContainingInOrder.contains("remove", testKey, "Integration-3"));

    // sleep and try to see if there were any other updates - if there any pending updates, we should catch them by now
    Thread.sleep(1000);
    assertThat(notifiedChanges, hasSize(4));
}