Search in sources :

Example 1 with ServiceListener

use of com.weibo.api.motan.registry.support.command.ServiceListener in project motan by weibocom.

the class ZookeeperRegistry method subscribeService.

@Override
protected void subscribeService(final URL url, final ServiceListener serviceListener) {
    try {
        clientLock.lock();
        ConcurrentHashMap<ServiceListener, IZkChildListener> childChangeListeners = serviceListeners.get(url);
        if (childChangeListeners == null) {
            serviceListeners.putIfAbsent(url, new ConcurrentHashMap<ServiceListener, IZkChildListener>());
            childChangeListeners = serviceListeners.get(url);
        }
        IZkChildListener zkChildListener = childChangeListeners.get(serviceListener);
        if (zkChildListener == null) {
            childChangeListeners.putIfAbsent(serviceListener, new IZkChildListener() {

                @Override
                public void handleChildChange(String parentPath, List<String> currentChilds) {
                    serviceListener.notifyService(url, getUrl(), nodeChildsToUrls(url, parentPath, currentChilds));
                    LoggerUtil.info(String.format("[ZookeeperRegistry] service list change: path=%s, currentChilds=%s", parentPath, currentChilds.toString()));
                }
            });
            zkChildListener = childChangeListeners.get(serviceListener);
        }
        try {
            // 防止旧节点未正常注销
            removeNode(url, ZkNodeType.CLIENT);
            createNode(url, ZkNodeType.CLIENT);
        } catch (Exception e) {
            LoggerUtil.warn("[ZookeeperRegistry] subscribe service: create node error, path=%s, msg=%s", ZkUtils.toNodePath(url, ZkNodeType.CLIENT), e.getMessage());
        }
        String serverTypePath = ZkUtils.toNodeTypePath(url, ZkNodeType.AVAILABLE_SERVER);
        zkClient.subscribeChildChanges(serverTypePath, zkChildListener);
        LoggerUtil.info(String.format("[ZookeeperRegistry] subscribe service: path=%s, info=%s", ZkUtils.toNodePath(url, ZkNodeType.AVAILABLE_SERVER), url.toFullStr()));
    } catch (Throwable e) {
        throw new MotanFrameworkException(String.format("Failed to subscribe %s to zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e);
    } finally {
        clientLock.unlock();
    }
}
Also used : ServiceListener(com.weibo.api.motan.registry.support.command.ServiceListener) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) IZkChildListener(org.I0Itec.zkclient.IZkChildListener) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Example 2 with ServiceListener

use of com.weibo.api.motan.registry.support.command.ServiceListener in project motan by weibocom.

the class ZookeeperRegistryTest method subAndUnsubService.

@Test
public void subAndUnsubService() throws Exception {
    ServiceListener serviceListener = new ServiceListener() {

        @Override
        public void notifyService(URL refUrl, URL registryUrl, List<URL> urls) {
            if (!urls.isEmpty()) {
                Assert.assertTrue(urls.contains(serviceUrl));
            }
        }
    };
    registry.subscribeService(clientUrl, serviceListener);
    Assert.assertTrue(containsServiceListener(clientUrl, serviceListener));
    registry.doRegister(serviceUrl);
    registry.doAvailable(serviceUrl);
    Thread.sleep(2000);
    registry.unsubscribeService(clientUrl, serviceListener);
    Assert.assertFalse(containsServiceListener(clientUrl, serviceListener));
}
Also used : ServiceListener(com.weibo.api.motan.registry.support.command.ServiceListener) List(java.util.List) URL(com.weibo.api.motan.rpc.URL) Test(org.junit.Test)

Example 3 with ServiceListener

use of com.weibo.api.motan.registry.support.command.ServiceListener in project motan by weibocom.

the class ZookeeperRegistry method unsubscribeService.

@Override
protected void unsubscribeService(URL url, ServiceListener serviceListener) {
    try {
        clientLock.lock();
        Map<ServiceListener, IZkChildListener> childChangeListeners = serviceListeners.get(url);
        if (childChangeListeners != null) {
            IZkChildListener zkChildListener = childChangeListeners.get(serviceListener);
            if (zkChildListener != null) {
                zkClient.unsubscribeChildChanges(ZkUtils.toNodeTypePath(url, ZkNodeType.CLIENT), zkChildListener);
                childChangeListeners.remove(serviceListener);
            }
        }
    } catch (Throwable e) {
        throw new MotanFrameworkException(String.format("Failed to unsubscribe service %s to zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e);
    } finally {
        clientLock.unlock();
    }
}
Also used : ServiceListener(com.weibo.api.motan.registry.support.command.ServiceListener) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) IZkChildListener(org.I0Itec.zkclient.IZkChildListener)

Example 4 with ServiceListener

use of com.weibo.api.motan.registry.support.command.ServiceListener 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)

Example 5 with ServiceListener

use of com.weibo.api.motan.registry.support.command.ServiceListener in project motan by weibocom.

the class ConsulRegistryTest method subAndUnsubService.

@Test
public void subAndUnsubService() throws Exception {
    ServiceListener serviceListener = createNewServiceListener(serviceUrl);
    ServiceListener serviceListener2 = createNewServiceListener(serviceUrl);
    registry.subscribeService(clientUrl, serviceListener);
    registry.subscribeService(clientUrl2, serviceListener2);
    Assert.assertTrue(containsServiceListener(serviceUrl, clientUrl, serviceListener));
    Assert.assertTrue(containsServiceListener(serviceUrl, clientUrl2, serviceListener2));
    registry.doRegister(serviceUrl);
    registry.doRegister(serviceUrl2);
    registry.doAvailable(null);
    Thread.sleep(sleepTime);
    registry.unsubscribeService(clientUrl, serviceListener);
    Assert.assertFalse(containsServiceListener(serviceUrl, clientUrl, serviceListener));
    Assert.assertTrue(containsServiceListener(serviceUrl, clientUrl2, serviceListener2));
    registry.unsubscribeService(clientUrl2, serviceListener2);
    Assert.assertFalse(containsServiceListener(serviceUrl, clientUrl2, serviceListener2));
}
Also used : ServiceListener(com.weibo.api.motan.registry.support.command.ServiceListener) Test(org.junit.Test)

Aggregations

ServiceListener (com.weibo.api.motan.registry.support.command.ServiceListener)5 IZkChildListener (org.I0Itec.zkclient.IZkChildListener)3 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)2 URL (com.weibo.api.motan.rpc.URL)2 Test (org.junit.Test)2 CommandListener (com.weibo.api.motan.registry.support.command.CommandListener)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 IZkDataListener (org.I0Itec.zkclient.IZkDataListener)1