org.apache.curator.framework.api.BackgroundPathable Java Examples

The following examples show how to use org.apache.curator.framework.api.BackgroundPathable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ZKUtil.java    From codes-scratch-zookeeper-netty with Apache License 2.0 5 votes vote down vote up
public static boolean exists(CuratorFramework client, String path, CuratorWatcher watcher) {
    try {
        if (watcher != null) {
            return ((BackgroundPathable) client.checkExists().usingWatcher(watcher)).forPath(path) != null;
        }
        return client.checkExists().forPath(path) != null;
    } catch (Exception e) {
        LOGGER.error("ZKUtil-->>exists(CuratorFramework client, String path, CuratorWatcher watcher) error, ", e);
    }
    return false;
}
 
Example #2
Source File: TreeCache.java    From curator with Apache License 2.0 5 votes vote down vote up
private <T, P extends Watchable<BackgroundPathable<T>> & BackgroundPathable<T>> Pathable<T> maybeWatch(
    P dataBuilder) {
    if (disableZkWatches) {
        return dataBuilder.inBackground(this);
    } else {
        return dataBuilder.usingWatcher(this).inBackground(this);
    }
}
 
Example #3
Source File: ZKUtil.java    From codes-scratch-zookeeper-netty with Apache License 2.0 5 votes vote down vote up
public static List<String> getChilds(CuratorFramework client, String path, CuratorWatcher watcher) {
    try {
        if (watcher != null) {
            return (List) ((BackgroundPathable) client.getChildren().usingWatcher(watcher)).forPath(path);
        }
        return (List) client.getChildren().forPath(path);
    } catch (Exception e) {
        LOGGER.error("ZKUtil-->>getChilds(CuratorFramework client, String path, CuratorWatcher watcher) error,", e);
    }
    return null;
}
 
Example #4
Source File: ZKUtil.java    From codes-scratch-zookeeper-netty with Apache License 2.0 5 votes vote down vote up
public static String getData(CuratorFramework client, String path, CuratorWatcher watcher) {
    try {
        if (client.checkExists().forPath(path) == null) {
            return null;
        }
        if (watcher != null) {
            return List2StringUtil
                    .toString((byte[]) ((BackgroundPathable) client.getData().usingWatcher(watcher)).forPath(path));
        }
        return List2StringUtil.toString((byte[]) client.getData().forPath(path));
    } catch (Exception e) {
        LOGGER.error("ZKUtil-->>getData(CuratorFramework client, String path, CuratorWatcher watcher)  error ", e);
    }
    return null;
}
 
Example #5
Source File: CuratorStateManagerTest.java    From incubator-heron with Apache License 2.0 5 votes vote down vote up
/**
 * Test deleteNode method
 * @throws Exception
 */
@Test
public void testDeleteNode() throws Exception {
  CuratorStateManager spyStateManager = spy(new CuratorStateManager());
  CuratorFramework mockClient = mock(CuratorFramework.class);
  DeleteBuilder mockDeleteBuilder = mock(DeleteBuilder.class);
  // Mockito doesn't support mock type-parametrized class, thus suppress the warning
  @SuppressWarnings("rawtypes")
  BackgroundPathable mockBackPathable = mock(BackgroundPathable.class);

  doReturn(mockClient)
      .when(spyStateManager).getCuratorClient();
  doReturn(true)
      .when(mockClient).blockUntilConnected(anyInt(), any(TimeUnit.class));
  doReturn(mockDeleteBuilder)
      .when(mockClient).delete();
  doReturn(mockBackPathable)
      .when(mockDeleteBuilder).withVersion(-1);

  spyStateManager.initialize(config);

  ListenableFuture<Boolean> result = spyStateManager.deleteExecutionState(PATH);

  // Verify the node is deleted correctly
  verify(mockDeleteBuilder).withVersion(-1);
  assertTrue(result.get());
}
 
Example #6
Source File: MockCurator.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public BackgroundPathable<T> usingWatcher(CuratorWatcher curatorWatcher) {
    throw new UnsupportedOperationException("Not implemented in MockCurator");
}
 
Example #7
Source File: MockCurator.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public BackgroundPathable<Void> withVersion(int i) {
    throw new UnsupportedOperationException("Not implemented in MockCurator");
}
 
Example #8
Source File: MockCurator.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public BackgroundPathable<T> usingWatcher(Watcher watcher) {
    throw new UnsupportedOperationException("Not implemented in MockCurator");
}
 
Example #9
Source File: CuratorStateManagerTest.java    From incubator-heron with Apache License 2.0 4 votes vote down vote up
/**
 * Test getNodeData method
 * @throws Exception
 */
@Test
public void testGetNodeData() throws Exception {
  CuratorStateManager spyStateManager = spy(new CuratorStateManager());
  final CuratorFramework mockClient = mock(CuratorFramework.class);
  GetDataBuilder mockGetBuilder = mock(GetDataBuilder.class);
  // Mockito doesn't support mock type-parametrized class, thus suppress the warning
  @SuppressWarnings("rawtypes")
  BackgroundPathable mockBackPathable = mock(BackgroundPathable.class);
  final CuratorEvent mockEvent = mock(CuratorEvent.class);
  Message.Builder mockBuilder = mock(Message.Builder.class);
  Message mockMessage = mock(Message.class);

  final byte[] data = "wy_1989".getBytes();

  doReturn(mockMessage)
      .when(mockBuilder).build();
  doReturn(data)
      .when(mockEvent).getData();
  doReturn(PATH)
      .when(mockEvent).getPath();
  doReturn(mockClient)
      .when(spyStateManager).getCuratorClient();
  doReturn(true)
      .when(mockClient).blockUntilConnected(anyInt(), any(TimeUnit.class));
  doReturn(mockGetBuilder)
      .when(mockClient).getData();
  doReturn(mockBackPathable)
      .when(mockGetBuilder).usingWatcher(any(Watcher.class));
  doAnswer(new Answer<Object>() {
    @Override
    public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
      Object[] objests = invocationOnMock.getArguments();
      // the first object is the BackgroundCallback
      ((BackgroundCallback) objests[0]).processResult(mockClient, mockEvent);
      return null;
    }
  }).when(mockBackPathable).inBackground(any(BackgroundCallback.class));

  spyStateManager.initialize(config);

  // Verify the data on node is fetched correctly
  ListenableFuture<Message> result = spyStateManager.getNodeData(null, PATH, mockBuilder);
  assertTrue(result.get().equals(mockMessage));
}
 
Example #10
Source File: GetChildrenBuilderImpl.java    From xian with Apache License 2.0 4 votes vote down vote up
@Override
public BackgroundPathable<List<String>> watched()
{
    watching = new Watching(true);
    return this;
}
 
Example #11
Source File: MockCurator.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public BackgroundPathable<T> watched() {
    throw new UnsupportedOperationException("Not implemented in MockCurator");
}
 
Example #12
Source File: GetChildrenBuilderImpl.java    From xian with Apache License 2.0 4 votes vote down vote up
@Override
public BackgroundPathable<List<String>> usingWatcher(CuratorWatcher watcher)
{
    watching = new Watching(client, watcher);
    return this;
}
 
Example #13
Source File: ZookeeperCallbackWatcher.java    From Thunder with Apache License 2.0 4 votes vote down vote up
@Override
public void usingWatcher(BackgroundPathable<?> backgroundPathable, String path) throws Exception {
    backgroundPathable.inBackground(callback, context, executor).forPath(path);
}
 
Example #14
Source File: ZookeeperWatcher.java    From Thunder with Apache License 2.0 4 votes vote down vote up
public void usingWatcher(BackgroundPathable<?> backgroundPathable, String path) throws Exception {
    backgroundPathable.forPath(path);
}
 
Example #15
Source File: GetChildrenBuilderImpl.java    From curator with Apache License 2.0 4 votes vote down vote up
@Override
public BackgroundPathable<List<String>> watched()
{
    watching = new Watching(client, true);
    return this;
}
 
Example #16
Source File: GetChildrenBuilderImpl.java    From curator with Apache License 2.0 4 votes vote down vote up
@Override
public BackgroundPathable<List<String>> usingWatcher(Watcher watcher)
{
    watching = new Watching(client, watcher);
    return this;
}
 
Example #17
Source File: GetChildrenBuilderImpl.java    From curator with Apache License 2.0 4 votes vote down vote up
@Override
public BackgroundPathable<List<String>> usingWatcher(CuratorWatcher watcher)
{
    watching = new Watching(client, watcher);
    return this;
}
 
Example #18
Source File: GetChildrenBuilderImpl.java    From xian with Apache License 2.0 4 votes vote down vote up
@Override
public BackgroundPathable<List<String>> usingWatcher(Watcher watcher)
{
    watching = new Watching(client, watcher);
    return this;
}