Search in sources :

Example 16 with IZkDataListener

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));
}
Also used : IZkDataListener(org.I0Itec.zkclient.IZkDataListener) Test(org.junit.Test)

Example 17 with IZkDataListener

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();
        }
    }
}
Also used : CommandListener(com.weibo.api.motan.registry.support.command.CommandListener) ServiceListener(com.weibo.api.motan.registry.support.command.ServiceListener) IZkChildListener(org.I0Itec.zkclient.IZkChildListener) IZkDataListener(org.I0Itec.zkclient.IZkDataListener) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URL(com.weibo.api.motan.rpc.URL)

Aggregations

IZkDataListener (org.I0Itec.zkclient.IZkDataListener)17 IZkChildListener (org.I0Itec.zkclient.IZkChildListener)7 CommandListener (com.weibo.api.motan.registry.support.command.CommandListener)3 Set (java.util.Set)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.Test)3 Test (org.testng.annotations.Test)3 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ZkNodeExistsException (org.I0Itec.zkclient.exception.ZkNodeExistsException)2 CallbackHandler (org.apache.helix.manager.zk.CallbackHandler)2 SamzaException (org.apache.samza.SamzaException)2 JobConfig (com.jeesuite.scheduler.model.JobConfig)1 MonitorCommond (com.jeesuite.scheduler.monitor.MonitorCommond)1 ServiceListener (com.weibo.api.motan.registry.support.command.ServiceListener)1 URL (com.weibo.api.motan.rpc.URL)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1