org.springframework.integration.zookeeper.metadata.ZookeeperMetadataStore类的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(14.2k)|赞(0)|评价(0)|浏览(89)

本文整理了Java中org.springframework.integration.zookeeper.metadata.ZookeeperMetadataStore类的一些代码示例,展示了ZookeeperMetadataStore类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZookeeperMetadataStore类的具体详情如下:
包路径:org.springframework.integration.zookeeper.metadata.ZookeeperMetadataStore
类名称:ZookeeperMetadataStore

ZookeeperMetadataStore介绍

[英]Zookeeper-based ListenableMetadataStore based on a Zookeeper node. Values are stored in the children node, the names of which are stored as keys.
[中]基于Zookeeper节点的基于Zookeeper的ListenableMetadataStore。值存储在子节点中,子节点的名称存储为键。

代码示例

代码示例来源:origin: spring-projects/spring-integration

@Override
public void put(String key, String value) {
  Assert.notNull(key, "'key' must not be null.");
  Assert.notNull(value, "'value' must not be null.");
  synchronized (this.updateMap) {
    try {
      Stat currentNode = this.client.checkExists().forPath(getPath(key));
      if (currentNode == null) {
        try {
          createNode(key, value);
        }
        catch (KeeperException.NodeExistsException e) {
          updateNode(key, value, -1);
        }
      }
      else {
        updateNode(key, value, -1);
      }
    }
    catch (Exception e) {
      throw new ZookeeperMetadataStoreException("Error while setting value for key '" + key + "':", e);
    }
  }
}

代码示例来源:origin: spring-projects/spring-integration

@Override
@After
public void tearDown() throws Exception {
  this.metadataStore.stop();
  this.client.delete().deletingChildrenIfNeeded().forPath(this.metadataStore.getRoot());
}

代码示例来源:origin: spring-projects/spring-integration

@Override
public String get(String key) {
  Assert.notNull(key, "'key' must not be null.");
  Assert.state(isRunning(), "ZookeeperMetadataStore has to be started before using.");
  synchronized (this.updateMap) {
    ChildData currentData = this.cache.getCurrentData(getPath(key));
    if (currentData == null) {
      if (this.updateMap.containsKey(key)) {
        // we have saved the value, but the cache hasn't updated yet
        // if the value had changed via replication, we would have been notified by the listener
        return this.updateMap.get(key).getValue();
      }
      else {
        // the value just doesn't exist
        return null;
      }
    }
    else {
      if (this.updateMap.containsKey(key)) {
        // our version is more recent than the cache
        if (this.updateMap.get(key).getVersion() >= currentData.getStat().getVersion()) {
          return this.updateMap.get(key).getValue();
        }
      }
      return IntegrationUtils.bytesToString(currentData.getData(), this.encoding);
    }
  }
}

代码示例来源:origin: spring-projects/spring-integration

@Override
public String putIfAbsent(String key, String value) {
  Assert.notNull(key, "'key' must not be null.");
  Assert.notNull(value, "'value' must not be null.");
  synchronized (this.updateMap) {
    try {
      createNode(key, value);
      return null;
    }
    catch (KeeperException.NodeExistsException e) {
      // so the data actually exists, we can read it
      try {
        byte[] bytes = this.client.getData().forPath(getPath(key));
        return IntegrationUtils.bytesToString(bytes, this.encoding);
      }
      catch (Exception exceptionDuringGet) {
        throw new ZookeeperMetadataStoreException("Exception while reading node with key '" + key + "':", e);
      }
    }
    catch (Exception e) {
      throw new ZookeeperMetadataStoreException("Error while trying to set '" + key + "':", e);
    }
  }
}

代码示例来源:origin: spring-projects/spring-integration

@Override
public boolean replace(String key, String oldValue, String newValue) {
  Assert.notNull(key, "'key' must not be null.");
  Assert.notNull(oldValue, "'oldValue' must not be null.");
  Assert.notNull(newValue, "'newValue' must not be null.");
  synchronized (this.updateMap) {
    Stat currentStat = new Stat();
    try {
      byte[] bytes = this.client.getData().storingStatIn(currentStat).forPath(getPath(key));
      if (oldValue.equals(IntegrationUtils.bytesToString(bytes, this.encoding))) {
        updateNode(key, newValue, currentStat.getVersion());
      }
      return true;
    }
    catch (KeeperException.NoNodeException e) {
      // ignore, the node doesn't exist there's nothing to replace
      return false;
    }
    catch (KeeperException.BadVersionException e) {
      // ignore
      return false;
    }
    catch (Exception e) {
      throw new ZookeeperMetadataStoreException("Cannot replace value");
    }
  }
}

代码示例来源:origin: spring-projects/spring-integration

@Test
public void testReplace() throws Exception {
  final String testKey = "ZookeeperMetadataStoreTests-Replace";
  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();
  otherMetadataStore.replace(testKey, "OtherValue", "Integration-2");
  assertEquals("Integration",
      IntegrationUtils.bytesToString(client.getData().forPath(metadataStore.getPath(testKey)), "UTF-8"));
  assertEquals("Integration", metadataStore.get(testKey));
  assertThat("Integration", eventually(equalsResult(() -> otherMetadataStore.get(testKey))));
  otherMetadataStore.replace(testKey, "Integration", "Integration-2");
  assertEquals("Integration-2",
      IntegrationUtils.bytesToString(client.getData().forPath(metadataStore.getPath(testKey)), "UTF-8"));
  assertThat("Integration-2", eventually(equalsResult(() -> metadataStore.get(testKey))));
  assertEquals("Integration-2", otherMetadataStore.get(testKey));
  otherMetadataStore.stop();
  CloseableUtils.closeQuietly(otherClient);
}

代码示例来源:origin: spring-projects/spring-integration

@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();
  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(() -> 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(() -> otherMetadataStore.get(testKey2))));
  otherMetadataStore.stop();
  CloseableUtils.closeQuietly(otherClient);
}

代码示例来源:origin: spring-projects/spring-integration

ZookeeperMetadataStore otherMetadataStore = new ZookeeperMetadataStore(otherClient);
barriers.put("remove", new CyclicBarrier(2));
barriers.put("update", new CyclicBarrier(2));
metadataStore.addListener(new MetadataStoreListenerAdapter() {
otherMetadataStore.put(testKey, "Integration");
waitAtBarrier("add", barriers);
assertThat(notifiedChanges, hasSize(1));
assertThat(notifiedChanges.get(0), IsIterableContainingInOrder.contains("add", testKey, "Integration"));
otherMetadataStore.putIfAbsent(testKey, "Integration++");
otherMetadataStore.put(testKey, "Integration-2");
waitAtBarrier("update", barriers);
assertThat(notifiedChanges, hasSize(2));
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));
otherMetadataStore.remove(testKey);
waitAtBarrier("remove", barriers);
assertThat(notifiedChanges, hasSize(4));
assertThat(notifiedChanges.get(3), IsIterableContainingInOrder.contains("remove", testKey, "Integration-3"));

代码示例来源:origin: spring-projects/spring-integration

@Override
public String remove(String key) {
  Assert.notNull(key, "'key' must not be null.");
  synchronized (this.updateMap) {
    try {
      byte[] bytes = this.client.getData().forPath(getPath(key));
      this.client.delete().forPath(getPath(key));
      // we guarantee that the deletion will supersede the existing data
      this.updateMap.put(key, new LocalChildData(null, Integer.MAX_VALUE));
      return IntegrationUtils.bytesToString(bytes, this.encoding);
    }
    catch (KeeperException.NoNodeException e) {
      // ignore - the node doesn't exist
      return null;
    }
    catch (Exception e) {
      throw new ZookeeperMetadataStoreException("Exception while deleting key '" + key + "'", e);
    }
  }
}

代码示例来源:origin: spring-projects/spring-integration

barriers.put("update", new CyclicBarrier(2));
try {
  metadataStore.addListener(null);
  fail("IllegalArgumentException expected");
metadataStore.addListener(new MetadataStoreListenerAdapter() {
metadataStore.put(testKey, "Integration");
waitAtBarrier("add", barriers);
assertThat(notifiedChanges, hasSize(1));
assertThat(notifiedChanges.get(0), IsIterableContainingInOrder.contains("add", testKey, "Integration"));
metadataStore.putIfAbsent(testKey, "Integration++");
metadataStore.put(testKey, "Integration-2");
waitAtBarrier("update", barriers);
assertThat(notifiedChanges, hasSize(2));
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));
metadataStore.remove(testKey);
waitAtBarrier("remove", barriers);
assertThat(notifiedChanges, hasSize(4));

代码示例来源:origin: spring-projects/spring-integration

@Test
public void testPersistKeyValue() throws Exception {
  String testKey = "ZookeeperMetadataStoreTests-Persist";
  metadataStore.put(testKey, "Integration");
  assertNotNull(client.checkExists().forPath(metadataStore.getPath(testKey)));
  assertEquals("Integration",
      IntegrationUtils.bytesToString(client.getData().forPath(metadataStore.getPath(testKey)), "UTF-8"));
}

代码示例来源:origin: spring-projects/spring-integration

@Override
public void stop(Runnable callback) {
  stop();
  callback.run();
}

代码示例来源:origin: spring-projects/spring-integration

@Test
public void testPersistEmptyStringToMetadataStore() {
  String testKey = "ZookeeperMetadataStoreTests-PersistEmpty";
  metadataStore.put(testKey, "");
  assertEquals("", metadataStore.get(testKey));
}

代码示例来源:origin: spring-projects/spring-integration

@Override
@Before
public void setUp() throws Exception {
  super.setUp();
  this.metadataStore = new ZookeeperMetadataStore(client);
  this.metadataStore.start();
}

代码示例来源:origin: spring-projects/spring-integration

@Test
public void testEnsureStarted() {
  ZookeeperMetadataStore zookeeperMetadataStore = new ZookeeperMetadataStore(this.client);
  try {
    zookeeperMetadataStore.get("foo");
  }
  catch (Exception e) {
    assertThat(e, instanceOf(IllegalStateException.class));
    assertThat(e.getMessage(), containsString("ZookeeperMetadataStore has to be started before using."));
  }
}

代码示例来源:origin: spring-projects/spring-integration

@Test
public void testAddRemoveListener() throws Exception {
  MetadataStoreListener mockListener = Mockito.mock(MetadataStoreListener.class);
  DirectFieldAccessor accessor = new DirectFieldAccessor(metadataStore);
  @SuppressWarnings("unchecked")
  List<MetadataStoreListener> listeners = (List<MetadataStoreListener>) accessor.getPropertyValue("listeners");
  assertThat(listeners, hasSize(0));
  metadataStore.addListener(mockListener);
  assertThat(listeners, hasSize(1));
  assertThat(listeners, IsIterableContainingInOrder.contains(mockListener));
  metadataStore.removeListener(mockListener);
  assertThat(listeners, hasSize(0));
}

代码示例来源:origin: spring-projects/spring-integration

@Test
public void testGetNonExistingKeyValue() {
  String retrievedValue = metadataStore.get("does-not-exist");
  assertNull(retrievedValue);
}

代码示例来源:origin: spring-projects/spring-integration

private void createNode(String key, String value) throws Exception {
  this.client.create().forPath(getPath(key), IntegrationUtils.stringToBytes(value, this.encoding));
  this.updateMap.put(key, new LocalChildData(value, 0));
}

代码示例来源:origin: org.springframework.integration/spring-integration-zookeeper

@Override
public String putIfAbsent(String key, String value) {
  Assert.notNull(key, "'key' must not be null.");
  Assert.notNull(value, "'value' must not be null.");
  synchronized (this.updateMap) {
    try {
      createNode(key, value);
      return null;
    }
    catch (KeeperException.NodeExistsException e) {
      // so the data actually exists, we can read it
      try {
        byte[] bytes = this.client.getData().forPath(getPath(key));
        return IntegrationUtils.bytesToString(bytes, this.encoding);
      }
      catch (Exception exceptionDuringGet) {
        throw new ZookeeperMetadataStoreException("Exception while reading node with key '" + key + "':", e);
      }
    }
    catch (Exception e) {
      throw new ZookeeperMetadataStoreException("Error while trying to set '" + key + "':", e);
    }
  }
}

代码示例来源:origin: org.springframework.integration/spring-integration-zookeeper

@Override
public boolean replace(String key, String oldValue, String newValue) {
  Assert.notNull(key, "'key' must not be null.");
  Assert.notNull(oldValue, "'oldValue' must not be null.");
  Assert.notNull(newValue, "'newValue' must not be null.");
  synchronized (this.updateMap) {
    Stat currentStat = new Stat();
    try {
      byte[] bytes = this.client.getData().storingStatIn(currentStat).forPath(getPath(key));
      if (oldValue.equals(IntegrationUtils.bytesToString(bytes, this.encoding))) {
        updateNode(key, newValue, currentStat.getVersion());
      }
      return true;
    }
    catch (KeeperException.NoNodeException e) {
      // ignore, the node doesn't exist there's nothing to replace
      return false;
    }
    catch (KeeperException.BadVersionException e) {
      // ignore
      return false;
    }
    catch (Exception e) {
      throw new ZookeeperMetadataStoreException("Cannot replace value");
    }
  }
}

相关文章