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();
}
}
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));
}
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();
}
}
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();
}
}
}
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));
}
Aggregations