本文整理汇总了Java中org.apache.curator.utils.EnsurePath.ensure方法的典型用法代码示例。如果您正苦于以下问题:Java EnsurePath.ensure方法的具体用法?Java EnsurePath.ensure怎么用?Java EnsurePath.ensure使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.curator.utils.EnsurePath
的用法示例。
在下文中一共展示了EnsurePath.ensure方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendContainerHeartBeat
import org.apache.curator.utils.EnsurePath; //导入方法依赖的package包/类
/**
* Updates the ZK container node with the container info.
*
* @param cinfo current ContainerInfo object.
*/
public void sendContainerHeartBeat(final ContainerInfo cinfo) {
String containerPath = ZKUtils.getContainerPath(cinfo.getContainerId());
LOGGER.debug("Container Path:" + containerPath);
EnsurePath ensurePath = new EnsurePath(containerPath);
try {
ensurePath.ensure(curatorClient.getZookeeperClient());
byte[] searializedCinfo = Utils.serialize(cinfo);
curatorClient.setData().forPath(containerPath, searializedCinfo);
LOGGER.debug("Updated container info for: "
+ containerPath);
} catch (Exception e) {
LOGGER.error("Could not create ZK node or set heartbeat for the "
+ "container.");
e.printStackTrace();
throw new RuntimeException(e);
}
}
示例2: testEnsurePathWithNamespace
import org.apache.curator.utils.EnsurePath; //导入方法依赖的package包/类
@Test
public void testEnsurePathWithNamespace() throws Exception
{
final String namespace = "jz";
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
CuratorFramework client = builder.connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).namespace(namespace).build();
client.start();
try
{
EnsurePath ensurePath = new EnsurePath("/pity/the/fool");
ensurePath.ensure(client.getZookeeperClient());
Assert.assertNull(client.getZookeeperClient().getZooKeeper().exists("/jz/pity/the/fool", false));
ensurePath = client.newNamespaceAwareEnsurePath("/pity/the/fool");
ensurePath.ensure(client.getZookeeperClient());
Assert.assertNotNull(client.getZookeeperClient().getZooKeeper().exists("/jz/pity/the/fool", false));
}
finally
{
CloseableUtils.closeQuietly(client);
}
}
示例3: testBasic
import org.apache.curator.utils.EnsurePath; //导入方法依赖的package包/类
@Test
public void testBasic() throws Exception
{
ZooKeeper client = mock(ZooKeeper.class, Mockito.RETURNS_MOCKS);
CuratorZookeeperClient curator = mock(CuratorZookeeperClient.class);
RetryPolicy retryPolicy = new RetryOneTime(1);
RetryLoop retryLoop = new RetryLoop(retryPolicy, null);
when(curator.getConnectionHandlingPolicy()).thenReturn(new StandardConnectionHandlingPolicy());
when(curator.getZooKeeper()).thenReturn(client);
when(curator.getRetryPolicy()).thenReturn(retryPolicy);
when(curator.newRetryLoop()).thenReturn(retryLoop);
Stat fakeStat = mock(Stat.class);
when(client.exists(Mockito.<String>any(), anyBoolean())).thenReturn(fakeStat);
EnsurePath ensurePath = new EnsurePath("/one/two/three");
ensurePath.ensure(curator);
verify(client, times(3)).exists(Mockito.<String>any(), anyBoolean());
ensurePath.ensure(curator);
verifyNoMoreInteractions(client);
ensurePath.ensure(curator);
verifyNoMoreInteractions(client);
}
示例4: setUp
import org.apache.curator.utils.EnsurePath; //导入方法依赖的package包/类
@Before
public void setUp() throws Exception {
zkServer = new TestingServer();
client = CuratorFrameworkFactory
.newClient("localhost:" + zkServer.getPort(),
new ExponentialBackoffRetry(1000, 3));
client.start();
EnsurePath ensurePath = new EnsurePath(AGENT_PATH);
ensurePath.ensure(client.getZookeeperClient());
doSetUp();
}
开发者ID:moueimei,项目名称:flume-release-1.7.0,代码行数:13,代码来源:TestAbstractZooKeeperConfigurationProvider.java
示例5: validateZKPath
import org.apache.curator.utils.EnsurePath; //导入方法依赖的package包/类
private void validateZKPath(String zkPath) throws Exception {
EnsurePath path = zkClient.newNamespaceAwareEnsurePath(zkPath);
path.ensure(zkClient.getZookeeperClient());
Stat stat = zkClient.checkExists().forPath(zkPath);
Preconditions.checkNotNull(stat);
LOG.info("Path {} ensured", path.getPath());
}
示例6: main
import org.apache.curator.utils.EnsurePath; //导入方法依赖的package包/类
/**
* 测试入口
*/
public static void main(String[] args) throws Exception {
client.start();
client.usingNamespace("ensurepath");
EnsurePath ensurePath = new EnsurePath(path);
ensurePath.ensure(client.getZookeeperClient());
// 切换工作空间
EnsurePath ensurePath2 = client.newNamespaceAwareEnsurePath("/sub");
ensurePath2.ensure(client.getZookeeperClient());
}
示例7: addSubscription
import org.apache.curator.utils.EnsurePath; //导入方法依赖的package包/类
public void addSubscription(String location, TreeCacheListener listener) throws Exception {
CuratorFramework client = curator.getCurator();
EnsurePath ensureMvTestPath = new EnsurePath(location);
ensureMvTestPath.ensure(client.getZookeeperClient());
TreeCache cache = new TreeCache(client, location);
cache.getListenable().addListener(listener);
cache.start();
caches.put(location, cache);
logger.info("Added ZooKeeper subscriber for " + location + " children.");
}
示例8: createRootIfNotExists
import org.apache.curator.utils.EnsurePath; //导入方法依赖的package包/类
/**
* Function to create the root ZK dir if it does not exist.
*
* @param zkConnectionString ZK connection string, without the root
* @param floeRoot Floe root dir
* @param retryTimeout connection timeout in ms
* @param retryAttempts retry attempts (for exponential backoff)
*/
private void createRootIfNotExists(final String zkConnectionString,
final String floeRoot,
final int retryTimeout,
final int retryAttempts) {
CuratorFramework curatorClientForChRoot = null;
try {
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory
.builder()
.connectString(zkConnectionString)
.retryPolicy(
new ExponentialBackoffRetry(retryTimeout,
retryAttempts)
);
//TODO: take backoff time from config.
//TODO: .connectionTimeoutMs()
//TODO: .sessionTimeoutMs()
//TODO: Zookeeper authorization here
curatorClientForChRoot = builder.build();
curatorClientForChRoot.start();
EnsurePath ensurePath = new EnsurePath(floeRoot);
ensurePath.ensure(curatorClientForChRoot.getZookeeperClient());
LOGGER.info("Root folder exists or created.");
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("Could not create root folder.");
throw new RuntimeException(e);
} finally {
CloseableUtils.closeQuietly(curatorClientForChRoot);
}
}
示例9: createPath
import org.apache.curator.utils.EnsurePath; //导入方法依赖的package包/类
@Override
public void createPath(String path) throws Exception {
EnsurePath ensurePath = new EnsurePath(path);
ensurePath.ensure(getConnectedClientOrFailFast().getZookeeperClient());
}
示例10: testSimultaneous
import org.apache.curator.utils.EnsurePath; //导入方法依赖的package包/类
@Test
public void testSimultaneous() throws Exception
{
ZooKeeper client = mock(ZooKeeper.class, Mockito.RETURNS_MOCKS);
RetryPolicy retryPolicy = new RetryOneTime(1);
RetryLoop retryLoop = new RetryLoop(retryPolicy, null);
final CuratorZookeeperClient curator = mock(CuratorZookeeperClient.class);
when(curator.getConnectionHandlingPolicy()).thenReturn(new StandardConnectionHandlingPolicy());
when(curator.getZooKeeper()).thenReturn(client);
when(curator.getRetryPolicy()).thenReturn(retryPolicy);
when(curator.newRetryLoop()).thenReturn(retryLoop);
final Stat fakeStat = mock(Stat.class);
final CountDownLatch startedLatch = new CountDownLatch(2);
final CountDownLatch finishedLatch = new CountDownLatch(2);
final Semaphore semaphore = new Semaphore(0);
when(client.exists(Mockito.<String>any(), anyBoolean())).thenAnswer
(
new Answer<Stat>()
{
@Override
public Stat answer(InvocationOnMock invocation) throws Throwable
{
semaphore.acquire();
return fakeStat;
}
}
);
final EnsurePath ensurePath = new EnsurePath("/one/two/three");
ExecutorService service = Executors.newCachedThreadPool();
for ( int i = 0; i < 2; ++i )
{
service.submit
(
new Callable<Void>()
{
@Override
public Void call() throws Exception
{
startedLatch.countDown();
ensurePath.ensure(curator);
finishedLatch.countDown();
return null;
}
}
);
}
Assert.assertTrue(startedLatch.await(10, TimeUnit.SECONDS));
semaphore.release(3);
Assert.assertTrue(finishedLatch.await(10, TimeUnit.SECONDS));
verify(client, times(3)).exists(Mockito.<String>any(), anyBoolean());
ensurePath.ensure(curator);
verifyNoMoreInteractions(client);
ensurePath.ensure(curator);
verifyNoMoreInteractions(client);
}