diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/Bean.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/Bean.java index efc4607a..dd3c9c64 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/Bean.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/Bean.java @@ -22,6 +22,8 @@ package fun.asgc.neutrino.core.annotation; +import fun.asgc.neutrino.core.aop.support.Singleton; + import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorFactory.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorFactory.java index 537396ad..c0d994b5 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorFactory.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorFactory.java @@ -21,9 +21,11 @@ */ package fun.asgc.neutrino.core.aop.interceptor; -import fun.asgc.neutrino.core.annotation.Singleton; +import fun.asgc.neutrino.core.aop.support.Async; +import fun.asgc.neutrino.core.aop.support.AsyncInterceptor; +import fun.asgc.neutrino.core.aop.support.Singleton; import fun.asgc.neutrino.core.aop.Intercept; -import fun.asgc.neutrino.core.aop.SingletonInterceptor; +import fun.asgc.neutrino.core.aop.support.SingletonInterceptor; import fun.asgc.neutrino.core.base.GlobalConfig; import fun.asgc.neutrino.core.cache.Cache; import fun.asgc.neutrino.core.cache.MemoryCache; @@ -264,11 +266,16 @@ public class InterceptorFactory { } } } - Singleton singleton = ClassUtil.getAnnotation(targetMethod, Singleton.class); + Singleton singleton = ClassUtil.getSingletonAnnotation(targetMethod); if (null != singleton && singleton.value()) { SingletonInterceptor interceptor = getOrNew(SingletonInterceptor.class); interceptors.add(interceptor); } + Async async = ClassUtil.getAsyncAnnotation(targetMethod); + if (null != async && async.value()) { + AsyncInterceptor interceptor = getOrNew(AsyncInterceptor.class); + interceptors.add(interceptor); + } return interceptors; } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/support/Async.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/support/Async.java new file mode 100644 index 00000000..ed48a93e --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/support/Async.java @@ -0,0 +1,38 @@ +/** + * 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.aop.support; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * @author: aoshiguchen + * @date: 2022/7/8 + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD}) +public @interface Async { + boolean value() default true; +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/support/AsyncInterceptor.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/support/AsyncInterceptor.java new file mode 100644 index 00000000..02072de8 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/support/AsyncInterceptor.java @@ -0,0 +1,52 @@ +/** + * 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.aop.support; + +import fun.asgc.neutrino.core.aop.Invocation; +import fun.asgc.neutrino.core.aop.interceptor.Interceptor; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * 异步拦截器 + * @author: aoshiguchen + * @date: 2022/7/8 + */ +public class AsyncInterceptor implements Interceptor { + private static final ExecutorService executorService = new ThreadPoolExecutor(10, 50, + 10L, TimeUnit.SECONDS, + new LinkedBlockingQueue()); + + @Override + public void intercept(Invocation inv) throws Exception { + executorService.submit(() -> { + try { + inv.invoke(); + } catch (Exception e) { + e.printStackTrace(); + } + }); + } +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/Singleton.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/support/Singleton.java similarity index 97% rename from neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/Singleton.java rename to neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/support/Singleton.java index 05a284f5..db3e779b 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/Singleton.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/support/Singleton.java @@ -19,7 +19,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package fun.asgc.neutrino.core.annotation; +package fun.asgc.neutrino.core.aop.support; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/SingletonInterceptor.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/support/SingletonInterceptor.java similarity index 95% rename from neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/SingletonInterceptor.java rename to neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/support/SingletonInterceptor.java index bac4f209..7f46fc4d 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/SingletonInterceptor.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/support/SingletonInterceptor.java @@ -19,8 +19,9 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package fun.asgc.neutrino.core.aop; +package fun.asgc.neutrino.core.aop.support; +import fun.asgc.neutrino.core.aop.Invocation; import fun.asgc.neutrino.core.aop.interceptor.Interceptor; import fun.asgc.neutrino.core.util.LockUtil; diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/SimpleBeanFactory.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/SimpleBeanFactory.java index 6efe9e4b..48605f5f 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/SimpleBeanFactory.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/bean/SimpleBeanFactory.java @@ -24,7 +24,6 @@ package fun.asgc.neutrino.core.bean; import com.google.common.collect.Lists; import fun.asgc.neutrino.core.annotation.*; import fun.asgc.neutrino.core.aop.Aop; -import fun.asgc.neutrino.core.aop.SingletonInterceptor; import fun.asgc.neutrino.core.aop.interceptor.ExceptionHandler; import fun.asgc.neutrino.core.aop.interceptor.Filter; import fun.asgc.neutrino.core.aop.interceptor.Interceptor; diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ClassUtil.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ClassUtil.java index 93f4fa34..040e7d99 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ClassUtil.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ClassUtil.java @@ -23,6 +23,8 @@ package fun.asgc.neutrino.core.util; import fun.asgc.neutrino.core.annotation.Component; +import fun.asgc.neutrino.core.aop.support.Async; +import fun.asgc.neutrino.core.aop.support.Singleton; import fun.asgc.neutrino.core.cache.Cache; import fun.asgc.neutrino.core.cache.MemoryCache; import lombok.extern.slf4j.Slf4j; @@ -245,6 +247,22 @@ public class ClassUtil { return false; } + public static Singleton getSingletonAnnotation(Method method) { + Singleton singleton = getAnnotation(method, Singleton.class); + if (null != singleton) { + return singleton; + } + return getAnnotation(method.getDeclaringClass(), Singleton.class); + } + + public static Async getAsyncAnnotation(Method method) { + Async async = getAnnotation(method, Async.class); + if (null != async) { + return async; + } + return getAnnotation(method.getDeclaringClass(), Async.class); + } + public static boolean isPublic(Class clazz) { return Modifier.isPublic(clazz.getModifiers()); } diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Giraffe.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Giraffe.java index b869d9d1..97a467e4 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Giraffe.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Giraffe.java @@ -22,7 +22,7 @@ package fun.asgc.neutrino.core.aop; -import fun.asgc.neutrino.core.annotation.Singleton; +import fun.asgc.neutrino.core.aop.support.Singleton; import java.util.Arrays; diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test4/Launcher.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test4/Launcher.java new file mode 100644 index 00000000..722db9d9 --- /dev/null +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/bean/test4/Launcher.java @@ -0,0 +1,61 @@ +/** + * 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.bean.test4; + +import fun.asgc.neutrino.core.annotation.NeutrinoApplication; +import fun.asgc.neutrino.core.annotation.NonIntercept; +import fun.asgc.neutrino.core.aop.support.Async; +import fun.asgc.neutrino.core.context.ApplicationRunner; +import fun.asgc.neutrino.core.context.NeutrinoLauncher; + +/** + * + * @author: aoshiguchen + * @date: 2022/7/8 + */ +@NonIntercept(false) +@NeutrinoApplication +public class Launcher implements ApplicationRunner { + + + @Override + public void run(String[] args) { + System.out.println("1111111"); + hello(); + System.out.println("2222"); + } + + public static void main(String[] args) { + NeutrinoLauncher.run(Launcher.class, args).sync(); + } + + @Async + public void hello() { + try { + Thread.sleep(5000); + System.out.println("hello"); + } catch (Exception e) { + + } + } + +}