From 9f12f76d6c7ca08a3ebe3cc8d83ee5c9a203d60f Mon Sep 17 00:00:00 2001 From: Hu Zongtang Date: Fri, 17 Jul 2020 16:12:32 +0800 Subject: [PATCH] [ISSUE#2859]fix no throw exception when publish event but no subsciber. (#3364) --- .../client/config/impl/EventDispatcher.java | 128 ------------------ .../client/config/impl/ServerListManager.java | 5 +- .../config/impl/ServerlistChangeEvent.java | 27 ++++ 3 files changed, 30 insertions(+), 130 deletions(-) delete mode 100644 client/src/main/java/com/alibaba/nacos/client/config/impl/EventDispatcher.java create mode 100644 client/src/main/java/com/alibaba/nacos/client/config/impl/ServerlistChangeEvent.java diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/EventDispatcher.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/EventDispatcher.java deleted file mode 100644 index cbb6071c887..00000000000 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/EventDispatcher.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.alibaba.nacos.client.config.impl; - -import com.alibaba.nacos.client.utils.LogUtils; -import org.slf4j.Logger; - -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CopyOnWriteArrayList; - -/** - * Event subscription and publishing tools. - * - * @author Nacos - */ -public class EventDispatcher { - - private static final Logger LOGGER = LogUtils.logger(EventDispatcher.class); - - /** - * 添加事件监听器. - */ - public static void addEventListener(AbstractEventListener listener) { - for (Class type : listener.interest()) { - getListenerList(type).addIfAbsent(listener); - } - } - - /** - * 发布事件,首先发布该事件暗示的其他事件,最后通知所有对应的监听器. - */ - public static void fireEvent(AbstractEvent abstractEvent) { - if (null == abstractEvent) { - return; - } - - // 发布该事件暗示的其他事件 - for (AbstractEvent implyEvent : abstractEvent.implyEvents()) { - try { - // 避免死循环 - if (abstractEvent != implyEvent) { - fireEvent(implyEvent); - } - } catch (Exception e) { - LOGGER.warn(e.toString(), e); - } - } - - for (AbstractEventListener listener : getListenerList(abstractEvent.getClass())) { - try { - listener.onEvent(abstractEvent); - } catch (Exception e) { - LOGGER.warn(e.toString(), e); - } - } - } - - static synchronized CopyOnWriteArrayList getListenerList( - Class eventType) { - CopyOnWriteArrayList listeners = LISTENER_MAP.get(eventType); - if (null == listeners) { - listeners = new CopyOnWriteArrayList(); - LISTENER_MAP.put(eventType, listeners); - } - return listeners; - } - - @SuppressWarnings("checkstyle:linelength") - static final Map, CopyOnWriteArrayList> LISTENER_MAP = new HashMap, CopyOnWriteArrayList>(); - - /** - * Client事件. - */ - public abstract static class AbstractEvent { - - @SuppressWarnings("unchecked") - protected List implyEvents() { - return Collections.EMPTY_LIST; - } - } - - /** - * 事件监听器. - */ - public abstract static class AbstractEventListener { - - public AbstractEventListener() { - EventDispatcher.addEventListener(this); - } - - /** - * 感兴趣的事件列表. - * - * @return event list - */ - public abstract List> interest(); - - /** - * 处理事件. - * - * @param abstractEvent event to do - */ - public abstract void onEvent(AbstractEvent abstractEvent); - } - - /** - * serverList has changed. - */ - public static class ServerlistChangeEvent extends AbstractEvent { - } -} diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java index 3e0a79afe37..2c3b2119aa4 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java @@ -19,13 +19,13 @@ import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.SystemPropertyKeyConst; import com.alibaba.nacos.api.exception.NacosException; -import com.alibaba.nacos.client.config.impl.EventDispatcher.ServerlistChangeEvent; import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.client.utils.EnvUtil; import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.client.utils.TemplateUtils; import com.alibaba.nacos.common.lifecycle.Closeable; +import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.common.utils.IoUtils; import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.common.utils.ThreadUtils; @@ -335,7 +335,8 @@ private void updateIfChanged(List newList) { iterator = iterator(); currentServerAddr = iterator.next(); - EventDispatcher.fireEvent(new ServerlistChangeEvent()); + // Using unified event processor, NotifyCenter + NotifyCenter.publishEvent(new ServerlistChangeEvent()); LOGGER.info("[{}] [update-serverlist] serverlist updated to {}", name, serverUrls); } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerlistChangeEvent.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerlistChangeEvent.java new file mode 100644 index 00000000000..c30db33ce81 --- /dev/null +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerlistChangeEvent.java @@ -0,0 +1,27 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.client.config.impl; + +import com.alibaba.nacos.common.notify.SlowEvent; + +/** + * Server List Change Event. + * + * @author zongtanghu + */ +public class ServerlistChangeEvent extends SlowEvent { +}