public void stop() { CloseableUtils.closeQuietly(znode); CloseableUtils.closeQuietly(instancesCache); CloseableUtils.closeQuietly(zooKeeperClient); }
public static void main(String[] args) throws Exception { TestingServer server = new TestingServer(); CuratorFramework client = null; NodeCache cache = null; try { client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3)); client.start(); cache = new NodeCache(client, PATH); cache.start(); processCommands(client, cache); } finally { CloseableUtils.closeQuietly(cache); CloseableUtils.closeQuietly(client); CloseableUtils.closeQuietly(server); } }
@Test public void testSimple() throws Exception { Timing timing = new Timing(); ChildReaper reaper = null; CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); try { client.start(); for ( int i = 0; i < 10; ++i ) { client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i)); } reaper = new ChildReaper(client, "/test", Reaper.Mode.REAP_UNTIL_DELETE, 1); reaper.start(); timing.forWaiting().sleepABit(); Stat stat = client.checkExists().forPath("/test"); Assert.assertEquals(stat.getNumChildren(), 0); } finally { CloseableUtils.closeQuietly(reaper); CloseableUtils.closeQuietly(client); } }
CuratorFramework client = CuratorFrameworkFactory.builder() .connectString(server.getConnectString()) .sessionTimeoutMs(timing.session()) .connectionTimeoutMs(timing.connection()) .retryPolicy(new RetryOneTime(1)) .namespace("foo") .build(); try client.start(); client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i)); Stat stat = client.checkExists().forPath("/test"); Assert.assertEquals(stat.getNumChildren(), 0); CloseableUtils.closeQuietly(reaper); CloseableUtils.closeQuietly(client);
public static void main(String[] args) throws Exception { TestingServer server = new TestingServer(); CuratorFramework client = null; PersistentNode node = null; try { client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3)); client.getConnectionStateListenable().addListener(new ConnectionStateListener() { client.start(); node.waitForInitialCreate(3, TimeUnit.SECONDS); String actualPath = node.getActualPath(); System.out.println("node " + actualPath + " value: " + new String(client.getData().forPath(actualPath))); client.create().forPath(PATH2, "persistent node".getBytes()); ex.printStackTrace(); } finally { CloseableUtils.closeQuietly(node); CloseableUtils.closeQuietly(client); CloseableUtils.closeQuietly(server);
public static void main(String[] args) throws Exception { TestingServer server = new TestingServer(); CuratorFramework client = null; try { client = createSimple(server.getConnectString()); client.start(); client.create().creatingParentsIfNeeded().forPath(PATH, "test".getBytes()); CloseableUtils.closeQuietly(client); client = createWithOptions(server.getConnectString(), new ExponentialBackoffRetry(1000, 3), 1000, 1000); client.start(); System.out.println(new String(client.getData().forPath(PATH))); } catch (Exception ex) { ex.printStackTrace(); } finally { CloseableUtils.closeQuietly(client); CloseableUtils.closeQuietly(server); } }
@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); }
public static void main(String[] args) throws Exception { TestingServer server = new TestingServer(); CuratorFramework client = null; try { client = createSimple(server.getConnectString()); client.start(); ZKPaths.mkdirs(client.getZookeeperClient().getZooKeeper(),"/a"); ZKPaths.mkdirs(client.getZookeeperClient().getZooKeeper(),"/another/path"); ZKPaths.mkdirs(client.getZookeeperClient().getZooKeeper(),"/yet/another/path"); transaction(client); } catch (Exception ex) { ex.printStackTrace(); } finally { CloseableUtils.closeQuietly(client); CloseableUtils.closeQuietly(server); } }
private void initialClient() { if (!isInitialized.compareAndSet(false, true)) { return; } if (client == null && StringUtils.isEmpty(address)) { throw new RuntimeException("zookeeper server address is not set."); } boolean connected = false; try { if (client == null) { RetryPolicy retryPolicy = new ExponentialBackoffRetry(baseSleepTimeMs, maxRetries); client = CuratorFrameworkFactory.builder().connectString(address).retryPolicy(retryPolicy) .connectionTimeoutMs(connectionTimeout).sessionTimeoutMs(sessionTimeout).build(); } client.start(); connected = client.blockUntilConnected(connectionTimeout, TimeUnit.MILLISECONDS); if (!connected) { throw new RuntimeException("connect zookeeper failed."); } } catch (Exception e) { CloseableUtils.closeQuietly(client); isInitialized.compareAndSet(true, false); throw new RuntimeException("init zookeeper client error.", e); } }
client = CuratorFrameworkFactory.newClient(zkQuorum, retryPolicy); client.start(); MaaSConfig config = ConfigUtil.INSTANCE.read(client, zkRoot, new MaaSConfig(), MaaSConfig.class); JsonInstanceSerializer<ModelEndpoint> serializer = new JsonInstanceSerializer<>(ModelEndpoint.class); CloseableUtils.closeQuietly(serviceDiscovery); CloseableUtils.closeQuietly(client);
if (zooKeeperClient.checkExists().forPath(znodePath) == null) { CloseableUtils.closeQuietly(znode); throw (e instanceof IOException) ? (IOException)e : new IOException(e);
long clusterReadyTimeoutMs) throws IOException { Preconditions.checkArgument(zooKeeperClient != null && zooKeeperClient.getState() == CuratorFrameworkState.STARTED, "client is not started"); } catch (InvalidACLException e) { CloseableUtils.closeQuietly(instancesCache); long elapsedNs = System.nanoTime() - startTimeNs; if (deltaNs == 0 || deltaNs <= elapsedNs) { CloseableUtils.closeQuietly(instancesCache); LOG.error("Unable to start curator PathChildrenCache", e); throw new IOException(e);
private LoadBalancer() { try { start(); }catch(Exception ex){ LOG.error("Exception while creating a zookeeper clients and cache",ex); if ((curaFramework != null) && (connectionStateListener != null)){ curaFramework.getConnectionStateListenable() .removeListener(connectionStateListener); } if ((curaFramework != null) && (unhandledErrorListener != null)){ curaFramework.getUnhandledErrorListenable() .removeListener(unhandledErrorListener); } for (Closeable closeable : closeAbles) { CloseableUtils.closeQuietly(closeable); } } }
public static void main(String[] args) throws Exception { TestingServer server = new TestingServer(); CuratorFramework client = null; TreeCache cache = null; try { client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3)); client.start(); cache = new TreeCache(client, PATH); cache.start(); processCommands(client, cache); } finally { CloseableUtils.closeQuietly(cache); CloseableUtils.closeQuietly(client); CloseableUtils.closeQuietly(server); } }
@Test public void testSimple() throws Exception { Timing timing = new Timing(); ChildReaper reaper = null; CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); try { client.start(); for ( int i = 0; i < 10; ++i ) { client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i)); } reaper = new ChildReaper(client, "/test", Reaper.Mode.REAP_UNTIL_DELETE, 1); reaper.start(); timing.forWaiting().sleepABit(); Stat stat = client.checkExists().forPath("/test"); Assert.assertEquals(stat.getNumChildren(), 0); } finally { CloseableUtils.closeQuietly(reaper); CloseableUtils.closeQuietly(client); } }
CuratorFramework client = CuratorFrameworkFactory.builder() .connectString(server.getConnectString()) .sessionTimeoutMs(timing.session()) .connectionTimeoutMs(timing.connection()) .retryPolicy(new RetryOneTime(1)) .namespace("foo") .build(); try client.start(); client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i)); Stat stat = client.checkExists().forPath("/test"); Assert.assertEquals(stat.getNumChildren(), 0); CloseableUtils.closeQuietly(reaper); CloseableUtils.closeQuietly(client);
@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); }
public static void main(String[] args) throws Exception { TestingServer server = new TestingServer(); CuratorFramework client = null; PathChildrenCache cache = null; try { client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3)); client.start(); // in this example we will cache data. Notice that this is optional. cache = new PathChildrenCache(client, PATH, true); cache.start(); processCommands(client, cache); } finally { CloseableUtils.closeQuietly(cache); CloseableUtils.closeQuietly(client); CloseableUtils.closeQuietly(server); } }
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); try client.start(); for ( int i = 0; i < 10; ++i ) client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i)); if ( r.nextBoolean() ) client.create().forPath("/test/" + Integer.toString(i) + "/foo"); ++nonEmptyNodes; CloseableUtils.closeQuietly(reaper); CloseableUtils.closeQuietly(client);
@Override public void close() { CloseableUtils.closeQuietly(searcher); CloseableUtils.closeQuietly(reader); CloseableUtils.closeQuietly(directory); } }