use of com.alibaba.dubbo.registry.NotifyListener in project dubbo by alibaba.
the class RegistryProtocolTest method testNotifyOverride.
@Test
public void testNotifyOverride() throws Exception {
URL newRegistryUrl = registryUrl.addParameter(Constants.EXPORT_KEY, serviceUrl);
Invoker<RegistryProtocolTest> invoker = new MockInvoker<RegistryProtocolTest>(RegistryProtocolTest.class, newRegistryUrl);
Exporter<?> exporter = protocol.export(invoker);
RegistryProtocol rprotocol = RegistryProtocol.getRegistryProtocol();
NotifyListener listener = getListener(rprotocol);
List<URL> urls = new ArrayList<URL>();
urls.add(URL.valueOf("override://0.0.0.0/?timeout=1000"));
urls.add(URL.valueOf("override://0.0.0.0/" + service + "?timeout=100"));
urls.add(URL.valueOf("override://0.0.0.0/" + service + "?x=y"));
listener.notify(urls);
assertEquals(true, exporter.getInvoker().isAvailable());
assertEquals("100", exporter.getInvoker().getUrl().getParameter("timeout"));
assertEquals("y", exporter.getInvoker().getUrl().getParameter("x"));
exporter.unexport();
// int timeout = ConfigUtils.getServerShutdownTimeout();
// Thread.sleep(timeout + 1000);
// assertEquals(false, exporter.getInvoker().isAvailable());
destroyRegistryProtocol();
}
use of com.alibaba.dubbo.registry.NotifyListener in project dubbo by alibaba.
the class SimpleRegistryService method subscribe.
@Override
public void subscribe(String service, URL url, NotifyListener listener) {
String client = RpcContext.getContext().getRemoteAddressString();
if (logger.isInfoEnabled()) {
logger.info("[subscribe] service: " + service + ",client:" + client);
}
List<URL> urls = getRegistered().get(service);
if ((RegistryService.class.getName() + ":0.0.0").equals(service) && (urls == null || urls.size() == 0)) {
register(service, new URL("dubbo", NetUtils.getLocalHost(), RpcContext.getContext().getLocalPort(), com.alibaba.dubbo.registry.RegistryService.class.getName(), url.getParameters()));
List<String> rs = registries;
if (rs != null && rs.size() > 0) {
for (String registry : rs) {
register(service, UrlUtils.parseURL(registry, url.getParameters()));
}
}
}
super.subscribe(service, url, listener);
Map<String, NotifyListener> listeners = remoteListeners.get(client);
if (listeners == null) {
remoteListeners.putIfAbsent(client, new ConcurrentHashMap<String, NotifyListener>());
listeners = remoteListeners.get(client);
}
listeners.put(service, listener);
urls = getRegistered().get(service);
if (urls != null && urls.size() > 0) {
listener.notify(urls);
}
}
use of com.alibaba.dubbo.registry.NotifyListener in project dubbo by alibaba.
the class MulticastRegistryTest method testSubscribe.
/**
* Test method for
* {@link com.alibaba.dubbo.registry.multicast.MulticastRegistry#subscribe(URL url, com.alibaba.dubbo.registry.NotifyListener)}
* .
*/
@Test
public void testSubscribe() {
// verify lisener.
final AtomicReference<URL> args = new AtomicReference<URL>();
registry.subscribe(consumerUrl, new NotifyListener() {
public void notify(List<URL> urls) {
// FIXME assertEquals(MulticastRegistry.this.service, service);
args.set(urls.get(0));
}
});
assertEquals(serviceUrl.toFullString(), args.get().toFullString());
Map<URL, Set<NotifyListener>> arg = registry.getSubscribed();
assertEquals(consumerUrl, arg.keySet().iterator().next());
}
use of com.alibaba.dubbo.registry.NotifyListener in project dubbo by alibaba.
the class ZookeeperRegistry method doSubscribe.
protected void doSubscribe(final URL url, final NotifyListener listener) {
try {
if (Constants.ANY_VALUE.equals(url.getServiceInterface())) {
String root = toRootPath();
ConcurrentMap<NotifyListener, ChildListener> listeners = zkListeners.get(url);
if (listeners == null) {
zkListeners.putIfAbsent(url, new ConcurrentHashMap<NotifyListener, ChildListener>());
listeners = zkListeners.get(url);
}
ChildListener zkListener = listeners.get(listener);
if (zkListener == null) {
listeners.putIfAbsent(listener, new ChildListener() {
public void childChanged(String parentPath, List<String> currentChilds) {
for (String child : currentChilds) {
child = URL.decode(child);
if (!anyServices.contains(child)) {
anyServices.add(child);
subscribe(url.setPath(child).addParameters(Constants.INTERFACE_KEY, child, Constants.CHECK_KEY, String.valueOf(false)), listener);
}
}
}
});
zkListener = listeners.get(listener);
}
zkClient.create(root, false);
List<String> services = zkClient.addChildListener(root, zkListener);
if (services != null && !services.isEmpty()) {
for (String service : services) {
service = URL.decode(service);
anyServices.add(service);
subscribe(url.setPath(service).addParameters(Constants.INTERFACE_KEY, service, Constants.CHECK_KEY, String.valueOf(false)), listener);
}
}
} else {
List<URL> urls = new ArrayList<URL>();
for (String path : toCategoriesPath(url)) {
ConcurrentMap<NotifyListener, ChildListener> listeners = zkListeners.get(url);
if (listeners == null) {
zkListeners.putIfAbsent(url, new ConcurrentHashMap<NotifyListener, ChildListener>());
listeners = zkListeners.get(url);
}
ChildListener zkListener = listeners.get(listener);
if (zkListener == null) {
listeners.putIfAbsent(listener, new ChildListener() {
public void childChanged(String parentPath, List<String> currentChilds) {
ZookeeperRegistry.this.notify(url, listener, toUrlsWithEmpty(url, parentPath, currentChilds));
}
});
zkListener = listeners.get(listener);
}
zkClient.create(path, false);
List<String> children = zkClient.addChildListener(path, zkListener);
if (children != null) {
urls.addAll(toUrlsWithEmpty(url, path, children));
}
}
notify(url, listener, urls);
}
} catch (Throwable e) {
throw new RpcException("Failed to subscribe " + url + " to zookeeper " + getUrl() + ", cause: " + e.getMessage(), e);
}
}
use of com.alibaba.dubbo.registry.NotifyListener in project dubbo by alibaba.
the class AbstractRegistry method notify.
protected void notify(List<URL> urls) {
if (urls == null || urls.isEmpty())
return;
for (Map.Entry<URL, Set<NotifyListener>> entry : getSubscribed().entrySet()) {
URL url = entry.getKey();
if (!UrlUtils.isMatch(url, urls.get(0))) {
continue;
}
Set<NotifyListener> listeners = entry.getValue();
if (listeners != null) {
for (NotifyListener listener : listeners) {
try {
notify(url, listener, filterEmpty(url, urls));
} catch (Throwable t) {
logger.error("Failed to notify registry event, urls: " + urls + ", cause: " + t.getMessage(), t);
}
}
}
}
}
Aggregations