Refine search
@Override protected void createEphemeral(String path, String data) { try { byte[] dataBytes = data.getBytes(charset); client.create().withMode(CreateMode.EPHEMERAL).forPath(path, dataBytes); } catch (NodeExistsException e) { } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
@Override protected void createEphemeral(String path, String data) { try { byte[] dataBytes = data.getBytes(charset); client.create().withMode(CreateMode.EPHEMERAL).forPath(path, dataBytes); } catch (NodeExistsException e) { } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
@Override public String persistSequential(final String key, final String value) { try { return client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath(key, value.getBytes(Charsets.UTF_8)); //CHECKSTYLE:OFF } catch (final Exception ex) { //CHECKSTYLE:ON RegExceptionHandler.handleException(ex); } return null; }
/** * @param name * The name of the zookeeper child * @param data * The data for the zookeeper child * @param mode * The mode in which the child needs to be created * @throws KeeperException * @throws InterruptedException **/ private String createChild(String name, byte[] data, CreateMode mode) throws Exception { return curatorFramework.create().withMode(mode).forPath(name, data); }
private void createPersistentNode(String nodePath) throws Exception { try { zkClient.create().withMode(CreateMode.PERSISTENT).forPath(nodePath); } catch (KeeperException.NodeExistsException ne) { LOG.debug(nodePath + " znode already exists !!"); } catch (Exception e) { throw new IOException(nodePath + " znode could not be created !!", e); } }
@Override protected String createEphemeral(String path, boolean sequential) { try { if (sequential) { return client.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(path); } else { return client.create().withMode(CreateMode.EPHEMERAL).forPath(path); } } catch (KeeperException.NodeExistsException e) { return path; } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
@Override protected String createPersistent(String path, boolean sequential) { try { if (sequential) { return client.create().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath(path); } else { return client.create().withMode(CreateMode.PERSISTENT).forPath(path); } } catch (KeeperException.NodeExistsException e) { return path; } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
@Override protected String createPersistent(String path, boolean sequential) { try { if (sequential) { return client.create().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath(path); } else { return client.create().withMode(CreateMode.PERSISTENT).forPath(path); } } catch (KeeperException.NodeExistsException e) { return path; } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
@Override protected String createEphemeral(String path, boolean sequential) { try { if (sequential) { return client.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(path); } else { return client.create().withMode(CreateMode.EPHEMERAL).forPath(path); } } catch (KeeperException.NodeExistsException e) { return path; } catch (Exception e) { throw new IllegalStateException(e.getMessage(), 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); } }
/** * @param name * The name of the zookeeper child * @param data * The data for the zookeeper child * @param mode * The mode in which the child needs to be created * @throws KeeperException * @throws InterruptedException **/ private String createChild(String name, byte[] data, CreateMode mode) throws Exception { return curatorFramework.create().withMode(mode).forPath(name, data); }
public ZkBasedSnowFlakeIdGenerator(String zooKeeperUrl, String applicationName) { CuratorFramework client = CuratorFrameworkFactory.newClient(zooKeeperUrl, new RetryForever(1000)); client.start(); try { String path = "/EasyTransIdGen/" + applicationName + "/P"; String nodeName = client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(path); String sequenceStr = nodeName.replaceAll(path, ""); hostSeq = Long.parseLong(sequenceStr) % (2^SnowFlake.MACHINE_BIT); client.close();//do not need to keep connection, hostSeq will not change } catch (Exception e) { throw new RuntimeException("create Id generator failed",e); } }
/** * 注册临时顺序数据 * * @param key */ public void registerEphemeralSequential(final String key) { try { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key); } catch (Exception ex) { Logs.RSD.error("persistEphemeralSequential:{}", key, ex); throw new ZKException(ex); } }
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); } }
/** * 注册临时顺序数据 * * @param key * @param value * @param cacheNode 第一次注册时设置为true, 连接断开重新注册时设置为false */ private void registerEphemeralSequential(final String key, final String value, boolean cacheNode) { try { client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(key, value.getBytes()); if (cacheNode) ephemeralSequentialNodes.put(key, value); } catch (Exception ex) { Logs.RSD.error("persistEphemeralSequential:{},{}", key, value, ex); throw new ZKException(ex); } }
@Override public void doWithZkClient(CuratorFramework client) throws Exception { nodePath = client .create() .creatingParentsIfNeeded() .withMode(CreateMode.EPHEMERAL_SEQUENTIAL) .forPath( BOOTSTRAP_SERVER_NODE_PATH + BOOTSTRAP_SERVER_NODE_PATH, bootstrapNodeAvroConverter.get().toByteArray(nodeInfo)); LOG.info("Created node with path: " + nodePath); } });
private void createInstanceNodeForStateReConnect() throws Exception { String parent = String.format("%s/%s/instances", CONSTANTS_ROOT_PATH, CommonUtils.getApplication(this.endpoint)); String path = String.format("%s/%s", parent, this.endpoint); byte[] versionByteArray = ByteUtils.longToByteArray(this.instanceVersion); try { this.instanceLock.lock(); this.curatorFramework.create().withMode(CreateMode.EPHEMERAL).forPath(path, versionByteArray); this.instanceInited = true; this.instanceCond.signalAll(); } finally { this.instanceLock.unlock(); } }