use of org.I0Itec.zkclient.IZkDataListener in project samza by apache.
the class TestZkUtils method testSubscribeToJobModelVersionChange.
@Test
public void testSubscribeToJobModelVersionChange() {
ZkKeyBuilder keyBuilder = new ZkKeyBuilder("test");
String root = keyBuilder.getRootPath();
zkClient.deleteRecursive(root);
class Result {
String res = "";
public String getRes() {
return res;
}
public void updateRes(String newRes) {
res = newRes;
}
}
Assert.assertFalse(zkUtils.exists(root));
// create the paths
zkUtils.validatePaths(new String[] { root, keyBuilder.getJobModelVersionPath(), keyBuilder.getProcessorsPath() });
Assert.assertTrue(zkUtils.exists(root));
Assert.assertTrue(zkUtils.exists(keyBuilder.getJobModelVersionPath()));
Assert.assertTrue(zkUtils.exists(keyBuilder.getProcessorsPath()));
final Result res = new Result();
// define the callback
IZkDataListener dataListener = new IZkDataListener() {
@Override
public void handleDataChange(String dataPath, Object data) throws Exception {
res.updateRes((String) data);
}
@Override
public void handleDataDeleted(String dataPath) throws Exception {
Assert.fail("Data wasn't deleted;");
}
};
// subscribe
zkClient.subscribeDataChanges(keyBuilder.getJobModelVersionPath(), dataListener);
zkClient.subscribeDataChanges(keyBuilder.getProcessorsPath(), dataListener);
// update
zkClient.writeData(keyBuilder.getJobModelVersionPath(), "newVersion");
// verify
Assert.assertTrue(testWithDelayBackOff(() -> "newVersion".equals(res.getRes()), 2, 1000));
// update again
zkClient.writeData(keyBuilder.getProcessorsPath(), "newProcessor");
Assert.assertTrue(testWithDelayBackOff(() -> "newProcessor".equals(res.getRes()), 2, 1000));
}
use of org.I0Itec.zkclient.IZkDataListener in project motan by weibocom.
the class ZookeeperRegistry method reconnectClient.
@SuppressWarnings("rawtypes")
private void reconnectClient() {
if (serviceListeners != null && !serviceListeners.isEmpty()) {
try {
clientLock.lock();
for (Map.Entry entry : serviceListeners.entrySet()) {
URL url = (URL) entry.getKey();
ConcurrentHashMap<ServiceListener, IZkChildListener> childChangeListeners = serviceListeners.get(url);
if (childChangeListeners != null) {
for (Map.Entry e : childChangeListeners.entrySet()) {
subscribeService(url, (ServiceListener) e.getKey());
}
}
}
for (Map.Entry entry : commandListeners.entrySet()) {
URL url = (URL) entry.getKey();
ConcurrentHashMap<CommandListener, IZkDataListener> dataChangeListeners = commandListeners.get(url);
if (dataChangeListeners != null) {
for (Map.Entry e : dataChangeListeners.entrySet()) {
subscribeCommand(url, (CommandListener) e.getKey());
}
}
}
LoggerUtil.info("[{}] reconnect all clients", registryClassName);
} finally {
clientLock.unlock();
}
}
}
Aggregations