apache / dubbo

The java implementation of Apache Dubbo. An RPC and microservice framework.
https://dubbo.apache.org/
Apache License 2.0
40.34k stars 26.4k forks source link

The abandoned Listener #6273

Open qiaolin-li opened 4 years ago

qiaolin-li commented 4 years ago

https://github.com/apache/dubbo/blob/da6c3ebf5c2aa529c0732d4f354d0d390a471bac/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java#L267-L279

You're going to have more and more listeners, but they're not going to do anything, right

qiaolin-li commented 4 years ago

There is also a problem in the constructor of this class, the last sentence of the constructor is

notify(url.getBackupurls ());

There aren't any subscribers, so what's he doing?

ljluestc commented 1 year ago
public abstract class AbstractRegistry implements Registry {
    // ...

    public AbstractRegistry(URL url) {
        // ...
    }

    // ...

    @Override
    public List<URL> lookup(URL url) {
        List<URL> result = new ArrayList<>();
        final AtomicReference<List<URL>> reference = new AtomicReference<>();
        NotifyListener listener = new NotifyListener() {
            private volatile boolean notified = false;

            @Override
            public void notify(List<URL> urls) {
                if (!notified) {
                    notified = true;
                    reference.set(urls);
                    unsubscribe(url, this); // Unsubscribe the listener after receiving the first notification
                }
            }
        };
        subscribe(url, listener);
        List<URL> urls = reference.get();
        if (CollectionUtils.isNotEmpty(urls)) {
            for (URL u : urls) {
                if (!EMPTY_PROTOCOL.equals(u.getProtocol())) {
                    result.add(u);
                }
            }
        }
        return result;
    }

    // ...
}