本文整理汇总了Java中org.apache.curator.utils.EnsurePath类的典型用法代码示例。如果您正苦于以下问题:Java EnsurePath类的具体用法?Java EnsurePath怎么用?Java EnsurePath使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
EnsurePath类属于org.apache.curator.utils包,在下文中一共展示了EnsurePath类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: initServiceDiscovery
import org.apache.curator.utils.EnsurePath; //导入依赖的package包/类
private void initServiceDiscovery() {
JsonInstanceSerializer<MetaData> serializer = new JsonInstanceSerializer<>(MetaData.class);
discovery = ServiceDiscoveryBuilder.builder(MetaData.class).client(curatorFramework).basePath(basePath).serializer(serializer).build();
try {
discovery.start();
new EnsurePath(basePath).ensure(curatorFramework.getZookeeperClient());
} catch (Exception e) {
log.error("failed to start discovery for {}", basePath, e);
throw new RuntimeException("failed to start discovery for " + basePath, e);
}
}
示例6: 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());
}
示例7: getServiceDiscovery
import org.apache.curator.utils.EnsurePath; //导入依赖的package包/类
/**
* Gets the discovery.
*
* @param basePath - Registration path
* @param curatorFramework - Curator
* @return the discovery
* @throws Exception the exception
*/
protected static ServiceDiscovery<MetaData> getServiceDiscovery(CuratorFramework curatorFramework, String basePath)
throws Exception {
new EnsurePath(basePath).ensure(curatorFramework.getZookeeperClient());
JsonInstanceSerializer<MetaData> serializer = new JsonInstanceSerializer<MetaData>(MetaData.class); // Payload Serializer
ServiceDiscovery<MetaData> serviceDiscovery = ServiceDiscoveryBuilder.builder(MetaData.class).client(curatorFramework).basePath(basePath).serializer(serializer).build(); // Service Discovery
serviceDiscovery.start();
return serviceDiscovery;
}
示例8: getServiceCacheBuilder
import org.apache.curator.utils.EnsurePath; //导入依赖的package包/类
/**
* Gets the discovery.
*
* @param basePath - Registration path
* @param curatorFramework - Curator
* @return the ServiceCacheBuilder
* @throws Exception the exception
*/
protected static ServiceDiscovery<MetaData> getServiceCacheBuilder(CuratorFramework curatorFramework, String basePath)
throws Exception {
new EnsurePath(basePath).ensure(curatorFramework.getZookeeperClient());
JsonInstanceSerializer<MetaData> serializer = new JsonInstanceSerializer<MetaData>(MetaData.class); // Payload Serializer
ServiceDiscovery<MetaData> serviceDiscovery = ServiceDiscoveryBuilder.builder(MetaData.class).client(curatorFramework).basePath(basePath).serializer(serializer).build(); // Service Discovery
serviceDiscovery.start();
return serviceDiscovery;
}
示例9: getServiceProvider
import org.apache.curator.utils.EnsurePath; //导入依赖的package包/类
/**
* Gets the discovery.
*
* @param basePath - Registration path
* @param curatorFramework - Curator
* @return the ServiceProvider
* @throws Exception the exception
*/
protected static ServiceProvider<MetaData> getServiceProvider(CuratorFramework curatorFramework, String basePath, String serviceName)
throws Exception {
new EnsurePath(basePath).ensure(curatorFramework.getZookeeperClient());
JsonInstanceSerializer<MetaData> serializer = new JsonInstanceSerializer<MetaData>(MetaData.class); // Payload Serializer
ServiceDiscovery<MetaData> serviceDiscovery = getServiceDiscovery(curatorFramework, basePath);
ServiceProvider<MetaData> serviceProvider = serviceDiscovery.serviceProviderBuilder().serviceName(serviceName).build(); // Service Provider for a particular service
serviceProvider.start();
return serviceProvider;
}
示例10: curatorServiceDiscovery
import org.apache.curator.utils.EnsurePath; //导入依赖的package包/类
@Bean(initMethod = "start", destroyMethod = "close")
public ServiceDiscovery curatorServiceDiscovery(
CuratorFramework curatorFramework,
@Value("${curator.discovery.basePath:/homeadvisor/services}") String basePath) throws Exception
{
final Class payloadClass = Object.class;
new EnsurePath(basePath).ensure(curatorFramework.getZookeeperClient());
return ServiceDiscoveryBuilder.builder(payloadClass)
.client(curatorFramework)
.basePath(basePath)
.serializer(new JsonInstanceSerializer(payloadClass))
.build();
}
示例11: 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());
}
示例12: doAdvertise
import org.apache.curator.utils.EnsurePath; //导入依赖的package包/类
public void doAdvertise(Service<byte[]> service, boolean flag)
{
try {
new EnsurePath(basePath).ensure(curatorFramework.getZookeeperClient());
ServiceInstance<byte[]> instance = getInstance(service);
if (flag) {
discovery.registerService(instance);
} else {
discovery.unregisterService(instance);
}
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
示例13: testInitialize
import org.apache.curator.utils.EnsurePath; //导入依赖的package包/类
@Test
public void testInitialize() throws Exception {
Stat stat = new Stat();
CuratorFramework curatorFramework = mockFramework();
GetDataBuilder getDataBuilder = mock(GetDataBuilder.class);
WatchPathable watchPathable = mock(WatchPathable.class);
SyncBuilder syncBuilder = mock(SyncBuilder.class);
Pathable pathable = mock(Pathable.class);
when(curatorFramework.sync()).thenReturn(syncBuilder);
when(syncBuilder.inBackground(any(BackgroundCallback.class))).thenReturn(pathable);
when(curatorFramework.getZookeeperClient()).thenReturn(mock(CuratorZookeeperClient.class));
when(curatorFramework.getData()).thenReturn(getDataBuilder);
when(curatorFramework.getState()).thenReturn(CuratorFrameworkState.STARTED);
when(getDataBuilder.storingStatIn(any(Stat.class))).thenReturn(watchPathable);
when(watchPathable.forPath(anyString())).thenReturn(new byte[0]);
when(getDataBuilder.forPath(anyString())).thenReturn(null);
ExistsBuilder existsBuilder = mock(ExistsBuilder.class);
EnsurePath ensurePath = mock(EnsurePath.class);
when(curatorFramework.checkExists()).thenReturn(existsBuilder);
when(curatorFramework.newNamespaceAwareEnsurePath(anyString())).thenReturn(ensurePath);
when(ensurePath.excludingLast()).thenReturn(ensurePath);
when(existsBuilder.forPath("/test/config")).thenReturn(stat);
when(existsBuilder.usingWatcher(any(CuratorWatcher.class))).thenReturn(existsBuilder);
when(existsBuilder.inBackground(any(BackgroundCallback.class))).thenReturn(existsBuilder);
ExampleConfigAdapter exampleConfigAdapter = new ExampleConfigAdapter(curatorFramework);
assertNotNull(exampleConfigAdapter);
}
示例14: testReadConfig
import org.apache.curator.utils.EnsurePath; //导入依赖的package包/类
@Test
public void testReadConfig() throws Exception {
ExampleConfig exampleConfig = new ExampleConfig();
exampleConfig.name = "ray";
ObjectMapper objectMapper = new ObjectMapper();
Stat stat = new Stat();
CuratorFramework curatorFramework = mockFramework();
GetDataBuilder getDataBuilder = mock(GetDataBuilder.class);
WatchPathable watchPathable = mock(WatchPathable.class);
SyncBuilder syncBuilder = mock(SyncBuilder.class);
Pathable pathable = mock(Pathable.class);
when(curatorFramework.getZookeeperClient()).thenReturn(mock(CuratorZookeeperClient.class));
when(curatorFramework.getData()).thenReturn(getDataBuilder);
when(curatorFramework.getState()).thenReturn(CuratorFrameworkState.STARTED);
when(curatorFramework.sync()).thenReturn(syncBuilder);
when(syncBuilder.inBackground(any(BackgroundCallback.class))).thenReturn(pathable);
when(getDataBuilder.storingStatIn(any(Stat.class))).thenReturn(watchPathable);
when(watchPathable.forPath(anyString())).thenReturn(objectMapper.writeValueAsBytes(exampleConfig));
when(getDataBuilder.forPath(anyString())).thenReturn(objectMapper.writeValueAsBytes(exampleConfig));
ExistsBuilder existsBuilder = mock(ExistsBuilder.class);
EnsurePath ensurePath = mock(EnsurePath.class);
when(curatorFramework.checkExists()).thenReturn(existsBuilder);
when(curatorFramework.newNamespaceAwareEnsurePath(anyString())).thenReturn(ensurePath);
when(ensurePath.excludingLast()).thenReturn(ensurePath);
when(existsBuilder.forPath("/test/config")).thenReturn(stat);
when(existsBuilder.usingWatcher(any(CuratorWatcher.class))).thenReturn(existsBuilder);
when(existsBuilder.inBackground(any(BackgroundCallback.class))).thenReturn(existsBuilder);
ExampleConfigAdapter exampleConfigAdapter = new ExampleConfigAdapter(curatorFramework);
exampleConfigAdapter.start();
assertNotNull(exampleConfigAdapter);
assertTrue(exampleConfigAdapter.get().isPresent());
assertEquals(exampleConfigAdapter.get().get().name, "ray");
}
示例15: 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.");
}