Example usage for org.springframework.integration.metadata MetadataStoreListenerAdapter MetadataStoreListenerAdapter

List of usage examples for org.springframework.integration.metadata MetadataStoreListenerAdapter MetadataStoreListenerAdapter

Introduction

In this page you can find the example usage for org.springframework.integration.metadata MetadataStoreListenerAdapter MetadataStoreListenerAdapter.

Prototype

MetadataStoreListenerAdapter

Source Link

Usage

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

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

    // 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   w  ww. j  a v  a2s.  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();
    metadataStore.put(testKey, "Integration");
    waitAtBarrier("add", barriers);
    assertThat(notifiedChanges, hasSize(1));
    assertThat(notifiedChanges.get(0), IsIterableContainingInOrder.contains("add", testKey, "Integration"));

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

    barriers.get("update").reset();
    metadataStore.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();
    metadataStore.replace(testKey, "Integration-2", "Integration-3");
    waitAtBarrier("update", barriers);
    assertThat(notifiedChanges, hasSize(3));
    assertThat(notifiedChanges.get(2),
            IsIterableContainingInOrder.contains("update", testKey, "Integration-3"));

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

    barriers.get("remove").reset();
    metadataStore.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
    Thread.sleep(1000);
    assertThat(notifiedChanges, hasSize(4));
}

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/*w  ww  .j  a  v  a 2 s . co 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));
}