diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/EventDispatcher.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/EventDispatcher.java index 38ab0e995bc..28f4b694f18 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/EventDispatcher.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/EventDispatcher.java @@ -89,6 +89,10 @@ public void removeListener(String serviceName, String clusters, EventListener li } } + public boolean isSubscribed(String serviceName, String clusters) { + return observerMap.containsKey(ServiceInfo.getKey(serviceName, clusters)); + } + public List getSubscribeServices() { List serviceInfos = new ArrayList(); for (String key : observerMap.keySet()) { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java index bb96821c3c8..219d1dce252 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java @@ -257,6 +257,8 @@ public ServiceInfo getServiceInfo(final String serviceName, final String cluster return serviceInfoMap.get(serviceObj.getKey()); } + + public void scheduleUpdateIfAbsent(String serviceName, String clusters) { if (futureMap.get(ServiceInfo.getKey(serviceName, clusters)) != null) { return; @@ -330,9 +332,18 @@ public void run() { refreshOnly(serviceName, clusters); } + lastRefTime = serviceObj.getLastRefTime(); + + if (!eventDispatcher.isSubscribed(serviceName, clusters) && + !futureMap.containsKey(ServiceInfo.getKey(serviceName, clusters))) { + // abort the update task: + NAMING_LOGGER.info("update task is stopped, service:" + serviceName + ", clusters:" + clusters); + return; + } + executor.schedule(this, serviceObj.getCacheMillis(), TimeUnit.MILLISECONDS); - lastRefTime = serviceObj.getLastRefTime(); + } catch (Throwable e) { NAMING_LOGGER.warn("[NA] failed to update serviceName: " + serviceName, e); }