com.alibaba.dubbo.registry.NotifyListener Java Examples

The following examples show how to use com.alibaba.dubbo.registry.NotifyListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MulticastRegistryTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
/**
 * 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() {

        @Override
        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());

}
 
Example #2
Source File: RegistryProtocolTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@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();
    assertEquals(false, exporter.getInvoker().isAvailable());
    destroyRegistryProtocol();
    
}
 
Example #3
Source File: RegistryProtocolTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@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();

    }
 
Example #4
Source File: MulticastRegistry.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
protected void registered(URL url) {
    for (Map.Entry<URL, Set<NotifyListener>> entry : getSubscribed().entrySet()) {
        URL key = entry.getKey();
        if (UrlUtils.isMatch(key, url)) {
            Set<URL> urls = received.get(key);
            if (urls == null) {
                received.putIfAbsent(key, new ConcurrentHashSet<URL>());
                urls = received.get(key);
            }
            urls.add(url);
            List<URL> list = toList(urls);
            for (NotifyListener listener : entry.getValue()) {
                notify(key, listener, list);
                synchronized (listener) {
                    listener.notify();
                }
            }
        }
    }
}
 
Example #5
Source File: RegistryProtocolTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@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();
    assertEquals(false, exporter.getInvoker().isAvailable());
    destroyRegistryProtocol();
    
}
 
Example #6
Source File: AbstractRegistry.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
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;
            }

//            查询url的监听器
            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);
                    }
                }
            }
        }
    }
 
Example #7
Source File: RegistryProtocolTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
/**
 * 服务名称不匹配,不能override invoker
 * 服务名称匹配,服务版本号不匹配
 */
@Test
public void testNotifyOverride_notmatch() 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/com.alibaba.dubbo.registry.protocol.HackService?timeout=100"));
    listener.notify(urls);
    assertEquals(true, exporter.getInvoker().isAvailable());
    assertEquals(null, exporter.getInvoker().getUrl().getParameter("timeout"));
    exporter.unexport();
    destroyRegistryProtocol();
}
 
Example #8
Source File: MulticastRegistryTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
/**
 * Test method for
 * {@link com.alibaba.dubbo.registry.support.injvm.InjvmRegistry#subscribe(java.util.Map, com.alibaba.dubbo.registry.support.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());

}
 
Example #9
Source File: SimpleRegistryService.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public void subscribe(URL url, NotifyListener listener) {
    if (getUrl().getPort() == 0) {
        URL registryUrl = RpcContext.getContext().getUrl();
        if (registryUrl != null && registryUrl.getPort() > 0
        		&& RegistryService.class.getName().equals(registryUrl.getPath())) {
            super.setUrl(registryUrl);
            super.register(registryUrl);
        }
    }
    String client = RpcContext.getContext().getRemoteAddressString();
    ConcurrentMap<URL, Set<NotifyListener>> clientListeners = remoteSubscribed.get(client);
    if (clientListeners == null) {
        remoteSubscribed.putIfAbsent(client, new ConcurrentHashMap<URL, Set<NotifyListener>>());
        clientListeners = remoteSubscribed.get(client);
    }
    Set<NotifyListener> listeners = clientListeners.get(url);
    if (listeners == null) {
        clientListeners.putIfAbsent(url, new ConcurrentHashSet<NotifyListener>());
        listeners = clientListeners.get(url);
    }
    listeners.add(listener);
    super.subscribe(url, listener);
    subscribed(url, listener);
}
 
Example #10
Source File: AbstractRegistry.java    From dubbox with Apache License 2.0 6 votes vote down vote up
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);
                }
            }
        }
    }
}
 
Example #11
Source File: MulticastRegistry.java    From dubbox with Apache License 2.0 6 votes vote down vote up
protected void registered(URL url) {
    for (Map.Entry<URL, Set<NotifyListener>> entry : getSubscribed().entrySet()) {
        URL key = entry.getKey();
        if (UrlUtils.isMatch(key, url)) {
            Set<URL> urls = received.get(key);
            if (urls == null) {
                received.putIfAbsent(key, new ConcurrentHashSet<URL>());
                urls = received.get(key);
            }
            urls.add(url);
            List<URL> list = toList(urls);
            for (NotifyListener listener : entry.getValue()) {
                notify(key, listener, list);
                synchronized (listener) {
                    listener.notify();
                }
            }
        }
    }
}
 
Example #12
Source File: MulticastRegistryTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
/**
 * Test method for
 * {@link com.alibaba.dubbo.registry.support.injvm.InjvmRegistry#subscribe(java.util.Map, com.alibaba.dubbo.registry.support.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());

}
 
Example #13
Source File: AbstractRegistry.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void unsubscribe(URL url, NotifyListener listener) {
    if (url == null) {
        throw new IllegalArgumentException("unsubscribe url == null");
    }
    if (listener == null) {
        throw new IllegalArgumentException("unsubscribe listener == null");
    }
    if (logger.isInfoEnabled()){
        logger.info("Unsubscribe: " + url);
    }
    Set<NotifyListener> listeners = subscribed.get(url);
    if (listeners != null) {
        listeners.remove(listener);
    }
}
 
Example #14
Source File: FailbackRegistry.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
private void removeFailedSubscribed(URL url, NotifyListener listener) {
    Set<NotifyListener> listeners = failedSubscribed.get(url);
    if (listeners != null) {
        listeners.remove(listener);
    }
    listeners = failedUnsubscribed.get(url);
    if (listeners != null) {
        listeners.remove(listener);
    }
    Map<NotifyListener, List<URL>> notified = failedNotified.get(url);
    if (notified != null) {
        notified.remove(listener);
    }
}
 
Example #15
Source File: AbstractRegistryService.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void unsubscribe(String service, URL url, NotifyListener listener) {
    if (service == null) {
        throw new IllegalArgumentException("service == null");
    }
    if (url == null) {
        throw new IllegalArgumentException("parameters == null");
    }
    if (listener == null) {
        throw new IllegalArgumentException("listener == null");
    }
    subscribed.remove(service);
    removeListener(service, listener);
}
 
Example #16
Source File: AbstractRegistryService.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private void removeListener(final String service, final NotifyListener listener){
    if (listener == null) {
        return;
    }
    List<NotifyListener> listeners = notifyListeners.get(service);
    if (listeners != null) {
        listeners.remove(listener);
    }
}
 
Example #17
Source File: SimpleRegistryService.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public void unsubscribe(URL url, NotifyListener listener) {
    if (! Constants.ANY_VALUE.equals(url.getServiceInterface())
            && url.getParameter(Constants.REGISTER_KEY, true)) {
        unregister(url);
    }
    String client = RpcContext.getContext().getRemoteAddressString();
    Map<URL, Set<NotifyListener>> clientListeners = remoteSubscribed.get(client);
    if (clientListeners != null && clientListeners.size() > 0) {
        Set<NotifyListener> listeners = clientListeners.get(url);
        if (listeners != null && listeners.size() > 0) {
            listeners.remove(listener);
        }
    }
}
 
Example #18
Source File: AbstractRegistryService.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void unsubscribe(String service, URL url, NotifyListener listener) {
    if (service == null) {
        throw new IllegalArgumentException("service == null");
    }
    if (url == null) {
        throw new IllegalArgumentException("parameters == null");
    }
    if (listener == null) {
        throw new IllegalArgumentException("listener == null");
    }
    subscribed.remove(service);
    removeListener(service, listener);
}
 
Example #19
Source File: AbstractRegistryService.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
private void removeListener(final String service, final NotifyListener listener){
    if (listener == null) {
        return;
    }
    List<NotifyListener> listeners = notifyListeners.get(service);
    if (listeners != null) {
        listeners.remove(listener);
    }
}
 
Example #20
Source File: AbstractRegistryService.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void subscribe(String service, URL url, NotifyListener listener) {
    if (service == null) {
        throw new IllegalArgumentException("service == null");
    }
    if (url == null) {
        throw new IllegalArgumentException("parameters == null");
    }
    if (listener == null) {
        throw new IllegalArgumentException("listener == null");
    }
    subscribed.put(service, url.getParameters()); 
    addListener(service, listener);
}
 
Example #21
Source File: MulticastRegistry.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Override
protected void doUnsubscribe(URL url, NotifyListener listener) {
    if (!Constants.ANY_VALUE.equals(url.getServiceInterface())
            && url.getParameter(Constants.REGISTER_KEY, true)) {
        unregister(url);
    }
    broadcast(Constants.UNSUBSCRIBE + " " + url.toFullString());
}
 
Example #22
Source File: FailbackRegistryTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
protected void doUnsubscribe(URL url, NotifyListener listener) {
    if (bad) {
        throw new RuntimeException("can not invoke!");
    }
    //System.out.println("do doUnsubscribe");
    latch.countDown();
}
 
Example #23
Source File: AbstractRegistryService.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public void subscribe(String service, URL url, NotifyListener listener) {
    if (service == null) {
        throw new IllegalArgumentException("service == null");
    }
    if (url == null) {
        throw new IllegalArgumentException("parameters == null");
    }
    if (listener == null) {
        throw new IllegalArgumentException("listener == null");
    }
    subscribed.put(service, url.getParameters()); 
    addListener(service, listener);
}
 
Example #24
Source File: SimpleRegistryService.java    From tutorials with MIT License 5 votes vote down vote up
protected void registered(URL url) {
    for (Map.Entry<URL, Set<NotifyListener>> entry : getSubscribed().entrySet()) {
        URL key = entry.getKey();
        if (UrlUtils.isMatch(key, url)) {
            List<URL> list = lookup(key);
            for (NotifyListener listener : entry.getValue()) {
                listener.notify(list);
            }
        }
    }
}
 
Example #25
Source File: AbstractRegistryService.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private void doNotify(String service, List<URL> urls) {
    notified.put(service, urls);
    List<NotifyListener> listeners = notifyListeners.get(service);
    if (listeners != null) {
        for (NotifyListener listener : listeners) {
            try {
                notify(service, urls, listener);
            } catch (Throwable t) {
                logger.error("Failed to notify registry event, service: " + service + ", urls: " +  urls + ", cause: " + t.getMessage(), t);
            }
        }
    }
}
 
Example #26
Source File: SimpleRegistryService.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public void unsubscribe(String service, URL url, NotifyListener listener) {
    super.unsubscribe(service, url, listener);
    String client = RpcContext.getContext().getRemoteAddressString();
    Map<String, NotifyListener> listeners = remoteListeners.get(client);
    if (listeners != null && listeners.size() > 0) {
        listeners.remove(service);
    }
    List<URL> urls = getRegistered().get(service);
    if (urls != null && urls.size() > 0) {
        listener.notify(urls);
    }
}
 
Example #27
Source File: AbstractRegistryService.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public void unsubscribe(String service, URL url, NotifyListener listener) {
    if (service == null) {
        throw new IllegalArgumentException("service == null");
    }
    if (url == null) {
        throw new IllegalArgumentException("parameters == null");
    }
    if (listener == null) {
        throw new IllegalArgumentException("listener == null");
    }
    subscribed.remove(service);
    removeListener(service, listener);
}
 
Example #28
Source File: AbstractRegistryService.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private void removeListener(final String service, final NotifyListener listener){
    if (listener == null) {
        return;
    }
    List<NotifyListener> listeners = notifyListeners.get(service);
    if (listeners != null) {
        listeners.remove(listener);
    }
}
 
Example #29
Source File: SimpleRegistryService.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public void unsubscribe(String service, URL url, NotifyListener listener) {
    super.unsubscribe(service, url, listener);
    String client = RpcContext.getContext().getRemoteAddressString();
    Map<String, NotifyListener> listeners = remoteListeners.get(client);
    if (listeners != null && listeners.size() > 0) {
        listeners.remove(service);
    }
    List<URL> urls = getRegistered().get(service);
    if (urls != null && urls.size() > 0) {
        listener.notify(urls);
    }
}
 
Example #30
Source File: FailbackRegistry.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(URL url, NotifyListener listener) {
    super.subscribe(url, listener);
    removeFailedSubscribed(url, listener);
    try {
        // 向服务器端发送订阅请求
        doSubscribe(url, listener);
    } catch (Exception e) {
        Throwable t = e;

        List<URL> urls = getCacheUrls(url);
        if (urls != null && urls.size() > 0) {
            notify(url, listener, urls);
            logger.error("Failed to subscribe " + url + ", Using cached list: " + urls + " from cache file: " + getUrl().getParameter(Constants.FILE_KEY, System.getProperty("user.home") + "/dubbo-registry-" + url.getHost() + ".cache") + ", cause: " + t.getMessage(), t);
        } else {
            // 如果开启了启动时检测,则直接抛出异常
            boolean check = getUrl().getParameter(Constants.CHECK_KEY, true)
                    && url.getParameter(Constants.CHECK_KEY, true);
            boolean skipFailback = t instanceof SkipFailbackWrapperException;
            if (check || skipFailback) {
                if(skipFailback) {
                    t = t.getCause();
                }
                throw new IllegalStateException("Failed to subscribe " + url + ", cause: " + t.getMessage(), t);
            } else {
                logger.error("Failed to subscribe " + url + ", waiting for retry, cause: " + t.getMessage(), t);
            }
        }

        // 将失败的订阅请求记录到失败列表,定时重试
        addFailedSubscribed(url, listener);
    }
}