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 new file mode 100644 index 00000000..08ebc0cd --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/base/TagMatcher.java @@ -0,0 +1,35 @@ +/** + * 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 new file mode 100644 index 00000000..deb8270c --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/base/TopicMatcher.java @@ -0,0 +1,35 @@ +/** + * 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 2cb2170e..e3df9427 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,12 +21,15 @@ */ 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 java.util.ArrayList; import java.util.List; +import java.util.Set; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -73,7 +76,21 @@ public class ApplicationEventChannel implements EventChannel msg) { this.receiverList.forEach(receiver -> { - threadPoolExecutor.submit(() -> receiver.receive(msg)); + 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; + } + } + }); }); } } 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 d80fb4df..905ccbbf 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,18 +22,51 @@ 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; + /** * @author: aoshiguchen * @date: 2022/9/29 */ @Slf4j -public class ApplicationEventReceiver implements EventReceiver> { +public class ApplicationEventReceiver implements EventReceiver>,TagMatcher,TopicMatcher { + 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)); } + public void setTopic(String topic) { + this.topic = topic; + } + + public void setTags(Set tags) { + this.tags = 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 621e2fc0..439508e6 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 @@ -21,9 +21,12 @@ */ package fun.asgc.neutrino.core.base.event; +import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Sets; +import fun.asgc.neutrino.core.util.SystemUtil; import lombok.Data; import lombok.experimental.Accessors; +import lombok.extern.slf4j.Slf4j; import org.junit.Test; /** @@ -36,6 +39,7 @@ import org.junit.Test; * @author: aoshiguchen * @date: 2022/10/3 */ +@Slf4j public class Test1 { private ApplicationEventChannel channel = new ApplicationEventChannel<>(); private ApplicationEventPublisher publisher = new ApplicationEventPublisher<>(); @@ -49,17 +53,21 @@ public class Test1 { ApplicationEventReceiver receiver = new ApplicationEventReceiver() { @Override public void receive(ApplicationEvent msg) { - System.out.println("msg:" + msg); + log.info("data:{}", JSONObject.toJSONString(msg.data())); } }; +// receiver.setTopic("/**"); +// receiver.setTags(Sets.newHashSet("create")); channel.registerReceiver(receiver); ApplicationEvent event = new ApplicationEvent<>(); event.context().setId("123"); - event.context().setTopic("student"); + event.context().setTopic("/student/create"); event.context().setTags(Sets.newHashSet("create")); event.setData(new Student().setId("1").setName("张三").setAge(28).setSex("男")); publisher.publish(event); + + SystemUtil.waitProcessDestroy().sync(); } @Accessors(chain = true) diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/interceptor/InterceptorRegistryTest.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/interceptor/InterceptorRegistryTest.java index 3c701d66..86e14f31 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/interceptor/InterceptorRegistryTest.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/interceptor/InterceptorRegistryTest.java @@ -49,8 +49,6 @@ public class InterceptorRegistryTest { System.out.println(antPathMatcher.match("**/**.html", "/a/11/22/33/a.html")); System.out.println(antPathMatcher.match("/**/*.html", "/a/11/22/33/a.html")); System.out.println(antPathMatcher.match("/**/*.html", "/a.html")); - - } }