Refine search
@Override public void saveData(String statePath, String data) { data = data == null ? "" : data; byte[] bytes = data.getBytes(); try { if (curatorFramework.checkExists().forPath(statePath) == null) { curatorFramework.create().creatingParentsIfNeeded().forPath(statePath, bytes); } else { curatorFramework.setData().forPath(statePath, bytes); } logger.info(String.format("data was saved. path: %s, data: %s.", statePath, data)); } catch (Exception e) { throw new RuntimeException(e); } }
public void writeBytes(String path, byte[] bytes) { try { if (_curator.checkExists().forPath(path) == null) { CreateBuilder builder = _curator.create(); ProtectACLCreateModePathAndBytesable<String> createAble = (ProtectACLCreateModePathAndBytesable<String>) builder .creatingParentsIfNeeded(); createAble.withMode(CreateMode.PERSISTENT).forPath(path, bytes); } else { _curator.setData().forPath(path, bytes); } } catch (Exception e) { throw new RuntimeException(e); } }
@Override public void setData(String path, Object data) { byte[] zkDataBytes; if (data instanceof Serializable) { zkDataBytes = zkSerializer.serialize(data); } else { zkDataBytes = (byte[]) data; } try { client.setData().forPath(path, zkDataBytes); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
@Override public void setData(String path, Object data) { byte[] zkDataBytes; if (data instanceof Serializable) { zkDataBytes = zkSerializer.serialize(data); } else { zkDataBytes = (byte[]) data; } try { client.setData().forPath(path, zkDataBytes); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
public void setData(String path, Object obj) { path = "/" + path; byte[] ser = _ser.serializeObject(obj); try { if (_curator.checkExists().forPath(path) != null) { _curator.setData().forPath(path, ser); } else { TransactionalState.createNode(_curator, path, ser, _zkAcls, CreateMode.PERSISTENT); } } catch (Exception e) { throw new RuntimeException(e); } }
void commitState (String stream, String shardId, Map<Object, Object> state) { byte[] bytes = JSONValue.toJSONString(state).getBytes(Charset.forName("UTF-8")); try { String path = getZkPath(stream, shardId); if (curatorFramework.checkExists().forPath(path) == null) { curatorFramework.create() .creatingParentsIfNeeded() .withMode(CreateMode.PERSISTENT) .forPath(path, bytes); } else { curatorFramework.setData().forPath(path, bytes); } } catch (Exception e) { throw new RuntimeException(e); } }
@Override public void updateMasterKey(int keySeq, String s) { CuratorFramework zk = getSession(); String keyPath = rootNode + NODE_KEYS + "/" + String.format(ZK_SEQ_FORMAT, keySeq); try { zk.setData().forPath(keyPath, s.getBytes()); } catch (Exception e) { throw new TokenStoreException("Error setting data in " + keyPath, e); } }
public void setData(String path, Object obj) { path = "/" + path; byte[] ser; try { ser = JSONValue.toJSONString(obj).getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } try { if (_curator.checkExists().forPath(path) != null) { _curator.setData().forPath(path, ser); } else { createNode(_curator, path, ser, _zkAcls, CreateMode.PERSISTENT); } } catch (Exception e) { throw new RuntimeException(e); } }
public static void main(String[] args) throws Exception { final String remoteAddress = "localhost:2181"; final String groupId = "Sentinel-Demo"; final String dataId = "SYSTEM-CODE-DEMO-FLOW"; final String rule = "[\n" + " {\n" + " \"resource\": \"TestResource\",\n" + " \"controlBehavior\": 0,\n" + " \"count\": 10.0,\n" + " \"grade\": 1,\n" + " \"limitApp\": \"default\",\n" + " \"strategy\": 0\n" + " }\n" + "]"; CuratorFramework zkClient = CuratorFrameworkFactory.newClient(remoteAddress, new ExponentialBackoffRetry(SLEEP_TIME, RETRY_TIMES)); zkClient.start(); String path = getPath(groupId, dataId); Stat stat = zkClient.checkExists().forPath(path); if (stat == null) { zkClient.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, null); } zkClient.setData().forPath(path, rule.getBytes()); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } zkClient.close(); }
public static void setOffsetInZooKeeper(CuratorFramework curatorClient, String groupId, String topic, int partition, long offset) throws Exception { ZKGroupTopicDirs topicDirs = new ZKGroupTopicDirs(groupId, topic); String path = topicDirs.consumerOffsetDir() + "/" + partition; curatorClient.newNamespaceAwareEnsurePath(path).ensure(curatorClient.getZookeeperClient()); byte[] data = Long.toString(offset).getBytes(ConfigConstants.DEFAULT_CHARSET); curatorClient.setData().forPath(path, data); }
public synchronized void on(String destination) { if (mode == Mode.LOCAL) { BooleanMutex mutex = LOCAL_LOCK.get(destination); if (mutex != null && !mutex.state()) { mutex.set(true); } } else { try { String path = SYN_SWITCH_ZK_NODE + destination; try { curatorClient.getCurator() .create() .creatingParentContainersIfNeeded() .withMode(CreateMode.PERSISTENT) .forPath(path, "on".getBytes(StandardCharsets.UTF_8)); } catch (Exception e) { curatorClient.getCurator().setData().forPath(path, "on".getBytes(StandardCharsets.UTF_8)); } } catch (Exception e) { throw new RuntimeException(e); } } }
public synchronized void off(String destination) { if (mode == Mode.LOCAL) { BooleanMutex mutex = LOCAL_LOCK.get(destination); if (mutex != null && mutex.state()) { mutex.set(false); } } else { try { String path = SYN_SWITCH_ZK_NODE + destination; try { curatorClient.getCurator() .create() .creatingParentContainersIfNeeded() .withMode(CreateMode.PERSISTENT) .forPath(path, "off".getBytes(StandardCharsets.UTF_8)); } catch (Exception e) { curatorClient.getCurator().setData().forPath(path, "off".getBytes(StandardCharsets.UTF_8)); } } catch (Exception e) { throw new RuntimeException(e); } } }
/** * A helper method that sets a field value. * @throws java.lang.Exception */ private void setFieldData(Type type, String id, String name, String val) throws Exception { try { zk.create().withMode(CreateMode.PERSISTENT).withACL(Ids.OPEN_ACL_UNSAFE) .forPath(makeFieldZnode(type, id, name), val.getBytes(ENCODING)); } catch (KeeperException.NodeExistsException e) { zk.setData().forPath(makeFieldZnode(type, id, name), val.getBytes(ENCODING)); } }