gpt4 book ai didi

org.springframework.integration.zookeeper.metadata.ZookeeperMetadataStore.getPath()方法的使用及代码示例

转载 作者:知者 更新时间:2024-03-20 21:10:31 28 4
gpt4 key购买 nike

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

ZookeeperMetadataStore.getPath介绍

暂无

代码示例

代码示例来源: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

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: 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

private void updateNode(String key, String value, int version) throws Exception {
  Stat stat = this.client.setData().withVersion(version).forPath(getPath(key),
      IntegrationUtils.stringToBytes(value, this.encoding));
  this.updateMap.put(key, new LocalChildData(value, stat.getVersion()));
}

代码示例来源: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 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 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 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

@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: org.springframework.integration/spring-integration-zookeeper

@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: org.springframework.integration/spring-integration-zookeeper

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

private void updateNode(String key, String value, int version) throws Exception {
  Stat stat = this.client.setData().withVersion(version).forPath(getPath(key),
      IntegrationUtils.stringToBytes(value, this.encoding));
  this.updateMap.put(key, new LocalChildData(value, stat.getVersion()));
}

代码示例来源: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 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: org.springframework.integration/spring-integration-zookeeper

@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: 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");
    }
  }
}

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com