From b845a45d06494560fe3389417dd48afd8ada5bbc Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Wed, 5 Oct 2022 23:55:58 +0800 Subject: [PATCH] =?UTF-8?q?ApplicationChannel=E6=B6=88=E6=81=AF=E5=88=86?= =?UTF-8?q?=E9=85=8D=E9=80=BB=E8=BE=91=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../asgc/neutrino/core/base/TagMatcher.java | 35 ----------- .../asgc/neutrino/core/base/TopicMatcher.java | 35 ----------- .../base/event/ApplicationEventChannel.java | 62 +++++++++++++++---- .../base/event/ApplicationEventReceiver.java | 33 +++------- .../asgc/neutrino/core/base/event/Test1.java | 2 +- 5 files changed, 59 insertions(+), 108 deletions(-) delete mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/base/TagMatcher.java delete mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/base/TopicMatcher.java diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/base/TagMatcher.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/base/TagMatcher.java deleted file mode 100644 index 08ebc0cd..00000000 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/base/TagMatcher.java +++ /dev/null @@ -1,35 +0,0 @@ -/** - * Copyright (c) 2022 aoshiguchen - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package fun.asgc.neutrino.core.base; - -/** - * @author: aoshiguchen - * @date: 2022/10/4 - */ -public interface TagMatcher { - /** - * tag匹配 - * @param tag 标签 - * @return 是否匹配 - */ - boolean tagMatch(String tag); -} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/base/TopicMatcher.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/base/TopicMatcher.java deleted file mode 100644 index deb8270c..00000000 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/base/TopicMatcher.java +++ /dev/null @@ -1,35 +0,0 @@ -/** - * Copyright (c) 2022 aoshiguchen - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package fun.asgc.neutrino.core.base; - -/** - * @author: aoshiguchen - * @date: 2022/10/4 - */ -public interface TopicMatcher { - /** - * topic匹配 - * @param topic topic - * @return 是否匹配 - */ - boolean topicMatch(String topic); -} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/base/event/ApplicationEventChannel.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/base/event/ApplicationEventChannel.java index e3df9427..50e0b446 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/base/event/ApplicationEventChannel.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/base/event/ApplicationEventChannel.java @@ -21,11 +21,12 @@ */ package fun.asgc.neutrino.core.base.event; -import com.google.common.collect.Sets; import fun.asgc.neutrino.core.base.CustomThreadFactory; import fun.asgc.neutrino.core.base.Dispatcher; import fun.asgc.neutrino.core.util.Assert; import fun.asgc.neutrino.core.util.CollectionUtil; +import fun.asgc.neutrino.core.util.StringUtil; +import fun.asgc.neutrino.core.web.AntPathMatcher; import java.util.ArrayList; import java.util.List; @@ -42,6 +43,7 @@ public class ApplicationEventChannel implements EventChannel> receiverList; private Dispatcher> dispatcher; private ThreadPoolExecutor threadPoolExecutor; + private static final AntPathMatcher antPathMatcher = new AntPathMatcher(); public ApplicationEventChannel() { this.receiverList = new ArrayList<>(); @@ -77,20 +79,54 @@ public class ApplicationEventChannel implements EventChannel msg) { this.receiverList.forEach(receiver -> { threadPoolExecutor.submit(() -> { - if (!receiver.topicMatch(msg.context().topic())) { - return; - } - Set tags = msg.context().tags(); - if (CollectionUtil.isEmpty(tags)) { - tags = Sets.newHashSet(""); - } - for (String tag : tags) { - if (receiver.tagMatch(tag)) { - receiver.receive(msg); - break; - } + if (match(msg, receiver)) { + receiver.receive(msg); } }); }); } + + /** + * 判断指定消息和指定接受者是否匹配 + * @param msg 消息 + * @param receiver 接受者 + * @return 是否匹配 + */ + private boolean match(ApplicationEvent msg, ApplicationEventReceiver receiver) { + if (null == msg || null == receiver) { + return false; + } + return topicMatch(msg.context().topic(), receiver.getTopic()) && tagMatch(msg.context().tags(), receiver.getTags()); + } + + /** + * topic匹配起 + * @param eventTopic 事件主题 + * @param subscriptionTopic 订阅的主题 + * @return 是否匹配 + */ + private boolean topicMatch(String eventTopic, String subscriptionTopic) { + if (StringUtil.isEmpty(subscriptionTopic)) { + return true; + } + return antPathMatcher.match(subscriptionTopic, eventTopic == null ? "" : eventTopic); + } + + /** + * 标签匹配 + * @param eventTags 事件标签 + * @param subscriptionTags 关注的标签 + * @return 是否匹配 + */ + private boolean tagMatch(Set eventTags, Set subscriptionTags) { + if (CollectionUtil.isEmpty(subscriptionTags)) { + return true; + } + for (String tag : eventTags) { + if (subscriptionTags.contains(tag)) { + return true; + } + } + return false; + } } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/base/event/ApplicationEventReceiver.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/base/event/ApplicationEventReceiver.java index 905ccbbf..312f1085 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/base/event/ApplicationEventReceiver.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/base/event/ApplicationEventReceiver.java @@ -22,11 +22,6 @@ package fun.asgc.neutrino.core.base.event; import com.alibaba.fastjson.JSONObject; -import fun.asgc.neutrino.core.base.TagMatcher; -import fun.asgc.neutrino.core.base.TopicMatcher; -import fun.asgc.neutrino.core.util.CollectionUtil; -import fun.asgc.neutrino.core.util.StringUtil; -import fun.asgc.neutrino.core.web.AntPathMatcher; import lombok.extern.slf4j.Slf4j; import java.util.Set; @@ -36,27 +31,9 @@ import java.util.Set; * @date: 2022/9/29 */ @Slf4j -public class ApplicationEventReceiver implements EventReceiver>,TagMatcher,TopicMatcher { +public class ApplicationEventReceiver implements EventReceiver> { private String topic; private Set tags; - private static final AntPathMatcher antPathMatcher = new AntPathMatcher(); - - @Override - public boolean tagMatch(String tag) { - if (CollectionUtil.isEmpty(this.tags)) { - return true; - } - return this.tags.contains(tag); - } - - @Override - public boolean topicMatch(String topic) { - if (StringUtil.isEmpty(this.topic)) { - return true; - } - return antPathMatcher.match(this.topic, topic == null ? "" : topic); - } - @Override public void receive(ApplicationEvent msg) { log.debug("ApplicationEventReceiver receive {}", JSONObject.toJSONString(msg)); @@ -69,4 +46,12 @@ public class ApplicationEventReceiver implements EventReceiver tags) { this.tags = tags; } + + public String getTopic() { + return topic; + } + + public Set getTags() { + return tags; + } } diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/base/event/Test1.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/base/event/Test1.java index 439508e6..cd5fa545 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/base/event/Test1.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/base/event/Test1.java @@ -61,7 +61,7 @@ public class Test1 { channel.registerReceiver(receiver); ApplicationEvent event = new ApplicationEvent<>(); - event.context().setId("123"); +// event.context().setId("123"); event.context().setTopic("/student/create"); event.context().setTags(Sets.newHashSet("create")); event.setData(new Student().setId("1").setName("张三").setAge(28).setSex("男"));