新增@Async注解,使得普通方法能方便的增强异步功能.
This commit is contained in:
@@ -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;
|
||||
|
||||
+10
-3
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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<Runnable>());
|
||||
|
||||
@Override
|
||||
public void intercept(Invocation inv) throws Exception {
|
||||
executorService.submit(() -> {
|
||||
try {
|
||||
inv.invoke();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -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;
|
||||
+2
-1
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user