diff --git a/common/src/main/java/com/alibaba/nacos/common/notify/NotifyCenter.java b/common/src/main/java/com/alibaba/nacos/common/notify/NotifyCenter.java index 059fee9fb..41b03d0cc 100644 --- a/common/src/main/java/com/alibaba/nacos/common/notify/NotifyCenter.java +++ b/common/src/main/java/com/alibaba/nacos/common/notify/NotifyCenter.java @@ -18,8 +18,8 @@ package com.alibaba.nacos.common.notify; import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException; import com.alibaba.nacos.common.JustForTest; -import com.alibaba.nacos.common.notify.listener.Subscriber; import com.alibaba.nacos.common.notify.listener.SmartSubscriber; +import com.alibaba.nacos.common.notify.listener.Subscriber; import com.alibaba.nacos.common.utils.BiFunction; import com.alibaba.nacos.common.utils.ClassUtils; import com.alibaba.nacos.common.utils.MapUtils; @@ -27,10 +27,10 @@ import com.alibaba.nacos.common.utils.ThreadUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Map; -import java.util.ServiceLoader; -import java.util.NoSuchElementException; import java.util.Iterator; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.ServiceLoader; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; @@ -170,7 +170,6 @@ public class NotifyCenter { * @param event type */ public static void registerSubscriber(final Subscriber consumer) { - final Class cls = consumer.subscribeType(); // If you want to listen to multiple events, you do it separately, // based on subclass's subscribeTypes method return list, it can register to publisher. if (consumer instanceof SmartSubscriber) { @@ -186,16 +185,17 @@ public class NotifyCenter { return; } - if (ClassUtils.isAssignableFrom(SlowEvent.class, cls)) { - INSTANCE.sharePublisher.addSubscriber(consumer, cls); + final Class subscribeType = consumer.subscribeType(); + if (ClassUtils.isAssignableFrom(SlowEvent.class, subscribeType)) { + INSTANCE.sharePublisher.addSubscriber(consumer, subscribeType); return; } - addSubscriber(consumer, consumer.subscribeType()); + addSubscriber(consumer, subscribeType); } /** - * Add a subscriber to pusblisher. + * Add a subscriber to publisher. * * @param consumer subscriber instance. * @param subscribeType subscribeType. @@ -217,7 +217,6 @@ public class NotifyCenter { * @param consumer subscriber instance. */ public static void deregisterSubscriber(final Subscriber consumer) { - final Class cls = consumer.subscribeType(); if (consumer instanceof SmartSubscriber) { for (Class subscribeType : ((SmartSubscriber) consumer).subscribeTypes()) { if (ClassUtils.isAssignableFrom(SlowEvent.class, subscribeType)) { @@ -229,15 +228,16 @@ public class NotifyCenter { return; } - if (ClassUtils.isAssignableFrom(SlowEvent.class, cls)) { - INSTANCE.sharePublisher.removeSubscriber(consumer, cls); + final Class subscribeType = consumer.subscribeType(); + if (ClassUtils.isAssignableFrom(SlowEvent.class, subscribeType)) { + INSTANCE.sharePublisher.removeSubscriber(consumer, subscribeType); return; } - if (removeSubscriber(consumer, consumer.subscribeType())) { + if (removeSubscriber(consumer, subscribeType)) { return; } - throw new NoSuchElementException("The subcriber has no event publisher"); + throw new NoSuchElementException("The subscriber has no event publisher"); } /** @@ -250,12 +250,11 @@ public class NotifyCenter { private static boolean removeSubscriber(final Subscriber consumer, Class subscribeType) { final String topic = ClassUtils.getCanonicalName(subscribeType); - if (INSTANCE.publisherMap.containsKey(topic)) { - EventPublisher publisher = INSTANCE.publisherMap.get(topic); - publisher.removeSubscriber(consumer); + EventPublisher eventPublisher = INSTANCE.publisherMap.get(topic); + if (eventPublisher != null) { + eventPublisher.removeSubscriber(consumer); return true; } - return false; } @@ -281,16 +280,16 @@ public class NotifyCenter { * @param event event instance. */ private static boolean publishEvent(final Class eventType, final Event event) { - final String topic = ClassUtils.getCanonicalName(eventType); if (ClassUtils.isAssignableFrom(SlowEvent.class, eventType)) { return INSTANCE.sharePublisher.publish(event); } - if (INSTANCE.publisherMap.containsKey(topic)) { - EventPublisher publisher = INSTANCE.publisherMap.get(topic); + final String topic = ClassUtils.getCanonicalName(eventType); + + EventPublisher publisher = INSTANCE.publisherMap.get(topic); + if (publisher != null) { return publisher.publish(event); } - LOGGER.warn("There are no [{}] publishers for this event, please register", topic); return false; } @@ -321,8 +320,7 @@ public class NotifyCenter { // MapUtils.computeIfAbsent is a unsafe method. MapUtils.computeIfAbsent(INSTANCE.publisherMap, topic, publisherFactory, eventType, queueMaxSize); } - EventPublisher publisher = INSTANCE.publisherMap.get(topic); - return publisher; + return INSTANCE.publisherMap.get(topic); } /**