- 浏览: 555341 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
jiang2011jiang:
mybatis3源码核心类1--Configuration -
tuyf_hs:
同求 图片
zookeeper+dubbo+dubbo管理集群的简要配置[单机] -
安静听歌:
请问图片还能找的会吗?你的图片和原文的图片都挂了,,,如果有图 ...
zookeeper+dubbo+dubbo管理集群的简要配置[单机] -
ahua186186:
yngwiet 写道楼主,有一个地方不太明白,为什么要用“ge ...
ListView中getChildAt(index)的使用注意事项 -
yngwiet:
楼主,有一个地方不太明白,为什么要用“getChildAt(p ...
ListView中getChildAt(index)的使用注意事项
@Async核心实现1 --------AsyncExecutionAspectSupport
- 博客分类:
- JAVA SE
基本原理:
通过spring的扩展接口AbstractBeanFactoryAwareAdvisingPostProcessor,初始化自定义的切面AsyncAnnotationAdvisor来实现方法的代理,最后通过自定义方法拦截器AsyncExecutionInterceptor实现异步执行。
核心实现是:
1.继续接口MethodInterceptor
2.获取自定义注解:getExecutorQualifier
通过spring的扩展接口AbstractBeanFactoryAwareAdvisingPostProcessor,初始化自定义的切面AsyncAnnotationAdvisor来实现方法的代理,最后通过自定义方法拦截器AsyncExecutionInterceptor实现异步执行。
核心实现是:
1.继续接口MethodInterceptor
2.获取自定义注解:getExecutorQualifier
public abstract class AsyncExecutionAspectSupport implements BeanFactoryAware { public static final String DEFAULT_TASK_EXECUTOR_BEAN_NAME = "taskExecutor"; private static final boolean completableFuturePresent = ClassUtils.isPresent("java.util.concurrent.CompletableFuture", AsyncExecutionInterceptor.class.getClassLoader()); protected final Log logger; private final Map<Method, AsyncTaskExecutor> executors; private volatile Executor defaultExecutor; private AsyncUncaughtExceptionHandler exceptionHandler; private BeanFactory beanFactory; public AsyncExecutionAspectSupport(Executor defaultExecutor) { this(defaultExecutor, new SimpleAsyncUncaughtExceptionHandler()); } public AsyncExecutionAspectSupport(Executor defaultExecutor, AsyncUncaughtExceptionHandler exceptionHandler) { this.logger = LogFactory.getLog(this.getClass()); this.executors = new ConcurrentHashMap(16); this.defaultExecutor = defaultExecutor; this.exceptionHandler = exceptionHandler; } public void setExecutor(Executor defaultExecutor) { this.defaultExecutor = defaultExecutor; } public void setExceptionHandler(AsyncUncaughtExceptionHandler exceptionHandler) { this.exceptionHandler = exceptionHandler; } public void setBeanFactory(BeanFactory beanFactory) { this.beanFactory = beanFactory; } protected AsyncTaskExecutor determineAsyncExecutor(Method method) { AsyncTaskExecutor executor = (AsyncTaskExecutor)this.executors.get(method); if (executor == null) { String qualifier = this.getExecutorQualifier(method); Executor targetExecutor; if (StringUtils.hasLength(qualifier)) { targetExecutor = this.findQualifiedExecutor(this.beanFactory, qualifier); } else { targetExecutor = this.defaultExecutor; if (targetExecutor == null) { Map var5 = this.executors; synchronized(this.executors) { if (this.defaultExecutor == null) { this.defaultExecutor = this.getDefaultExecutor(this.beanFactory); } targetExecutor = this.defaultExecutor; } } } if (targetExecutor == null) { return null; } executor = targetExecutor instanceof AsyncListenableTaskExecutor ? (AsyncListenableTaskExecutor)targetExecutor : new TaskExecutorAdapter(targetExecutor); this.executors.put(method, executor); } return (AsyncTaskExecutor)executor; } protected abstract String getExecutorQualifier(Method var1); protected Executor findQualifiedExecutor(BeanFactory beanFactory, String qualifier) { if (beanFactory == null) { throw new IllegalStateException("BeanFactory must be set on " + this.getClass().getSimpleName() + " to access qualified executor '" + qualifier + "'"); } else { return (Executor)BeanFactoryAnnotationUtils.qualifiedBeanOfType(beanFactory, Executor.class, qualifier); } } protected Executor getDefaultExecutor(BeanFactory beanFactory) { if (beanFactory != null) { try { return (Executor)beanFactory.getBean(TaskExecutor.class); } catch (NoUniqueBeanDefinitionException var6) { this.logger.debug("Could not find unique TaskExecutor bean", var6); try { return (Executor)beanFactory.getBean("taskExecutor", Executor.class); } catch (NoSuchBeanDefinitionException var4) { if (this.logger.isInfoEnabled()) { this.logger.info("More than one TaskExecutor bean found within the context, and none is named 'taskExecutor'. Mark one of them as primary or name it 'taskExecutor' (possibly as an alias) in order to use it for async processing: " + var6.getBeanNamesFound()); } } } catch (NoSuchBeanDefinitionException var7) { this.logger.debug("Could not find default TaskExecutor bean", var7); try { return (Executor)beanFactory.getBean("taskExecutor", Executor.class); } catch (NoSuchBeanDefinitionException var5) { this.logger.info("No task executor bean found for async processing: no bean of type TaskExecutor and no bean named 'taskExecutor' either"); } } } return null; } protected Object doSubmit(Callable<Object> task, AsyncTaskExecutor executor, Class<?> returnType) { if (completableFuturePresent) { Future<Object> result = AsyncExecutionAspectSupport.CompletableFutureDelegate.processCompletableFuture(returnType, task, executor); if (result != null) { return result; } } if (ListenableFuture.class.isAssignableFrom(returnType)) { return ((AsyncListenableTaskExecutor)executor).submitListenable(task); } else if (Future.class.isAssignableFrom(returnType)) { return executor.submit(task); } else { executor.submit(task); return null; } } protected void handleError(Throwable ex, Method method, Object... params) throws Exception { if (Future.class.isAssignableFrom(method.getReturnType())) { ReflectionUtils.rethrowException(ex); } else { try { this.exceptionHandler.handleUncaughtException(ex, method, params); } catch (Throwable var5) { this.logger.error("Exception handler for async method '" + method.toGenericString() + "' threw unexpected exception itself", var5); } } } @UsesJava8 private static class CompletableFutureDelegate { private CompletableFutureDelegate() { } public static <T> Future<T> processCompletableFuture(Class<?> returnType, final Callable<T> task, Executor executor) { return !CompletableFuture.class.isAssignableFrom(returnType) ? null : CompletableFuture.supplyAsync(new Supplier<T>() { public T get() { try { return task.call(); } catch (Throwable var2) { throw new CompletionException(var2); } } }, executor); } } }
发表评论
-
shiro落地的设计复杂度(最后总结)
2018-06-19 17:22 548经过1周的源码研究,终于对shiro的原理有了深刻的理解,基于 ... -
shiro login成功后保存了哪些数据
2018-06-19 17:05 1428shiro login成功后 保存了Principals 和 ... -
shiro 会话原理分析
2018-06-19 12:40 15611、从哪里获取sessionid每次请求都会尝试获取ses ... -
shiro内部原理分析
2018-06-15 17:07 2358一句话总结:会话域Context一路收集principal ... -
Zookeeper入门-001 源码环境搭建
2018-03-15 11:47 8791.到github下载源码:https://github.c ... -
dubbo服务治理之路由规则研究
2018-01-31 15:50 21851.今天没太多事情,挤 ... -
shiro SecurityUtils.getSubject()深度分析
2018-01-12 17:38 488531.总的来说,SecurityUtils.getSubject ... -
从零开始玩转JMX(1):简介和 Standard MBean
2017-08-23 15:20 0http://www.importnew.com/22299. ... -
java基础回顾
2017-08-15 11:21 0http://www.cnblogs.com/skywang1 ... -
mybatis-generator-maven-plugin 插件扩展 增加自定义方法
2017-08-10 16:50 0https://my.oschina.net/alexgaoy ... -
解决了DeferredResult请求长时间占用数据库连接的问题
2017-08-04 09:55 2324最近看了看开源项目appllo配置中心的源码,发现一个很有意思 ... -
httpclient发送webservice
2017-05-03 23:25 0http://aperise.iteye.com/blog/2 ... -
与大师面对面交流:Chris Richardson 来华布道微服务架构
2016-11-28 21:28 780http://www.daocloud.io/microser ... -
eclipse反编译
2016-11-23 20:48 0http://jingyan.baidu.com/articl ... -
spring security
2016-09-12 19:28 0http://www.importnew.com/5641.h ... -
浅析JPA中EntityManager无法remove entity的问题
2016-07-18 21:50 0http://rickqin.blog.51cto.com/1 ... -
Permission Denied(publickey) 解决
2016-07-14 19:18 27421.生成公钥和私钥放到C:\Users\itservice\. ... -
权限管理系统
2016-04-18 13:29 0http://git.oschina.net/ketayao/ ... -
Java工程师成神之路--面试必须复习的基础
2015-12-28 17:20 0http://www.open-open.com/news/v ... -
spring AOP何时添加AnnotationAwareAspectJAutoProxyCreator
2015-12-27 00:46 22061.spring AOP的原理都知道是从解析自定义标签时注册一 ...
相关推荐
主要给大家介绍了关于Spring Boot利用@Async如何实现异步调用:自定义线程池的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
前端开源库-babel-helper-remap-koa2-async-to-generatorbabel-helper-remap-koa2-async-to-generator,将异步函数转换为ES2015生成器(koav2->koav1)。
async-mysql-connector-1.6.jar async-mysql-connector-1.6.jar async-mysql-connector-1.6.jar async-mysql-connector-1.6.jar async-mysql-connector-1.6.jar async-mysql-connector-1.6.jar async-mysql-...
11:01:33,545 DEBUG ThreadPoolAsynchronousRunner:730 - com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@5b28c08e -- Running DeadlockDetector[Exiting. No pending tasks.] 11:01:43,558 ...
mongodb-async-driver-2.0.1 jar包
前端开源库-babel-plugin-transform-async-to-promisesBabel插件将异步转换为承诺,将异步/等待转换为承诺链
async-webservices-example-webapp-7.0.0pre1-sources.jar
资源来自pypi官网。 资源全名:asyncapi_generator-0.1.0-py3-none-any.whl
vuex-async-module减少生成Vuex模块的异步样板代码。 兼容Vue 2.x安装npm install @ liqueflies / vuex-async-mod vuex-async-module减少生成Vuex模块的异步样板代码。 兼容Vue 2.x安装npm install @ liqueflies / ...
yarn add react-helmet-async gatsby-plugin-react-helmet-async react-helmet-async要求您的react和react-dom至少为16.6.0 。 如果它们早于此,则需要对其进行升级: yarn add react@^16.6.0 react-d
async-webservices-example-webapp-7.0.0pre0-sources.jar
强大的网络请求库,主要特征如下: 处理异步Http请求,并通过匿名内部类处理回调结果 Http请求均位于非UI线程,...通过线程池处理并发请求 处理文件上传、下载 响应结果自动打包JSON格式 自动处理连接断开时请求重连
android的异步网络加载,方便使用,免去多余的代码
asyncpage-1.0.1-sources.jar
asyncpage-1.0.0-sources.jar
TurboPower Async Professional 5.00 D7-D2010(内附安装说明)
android-async-http-master 很好的http框架
android-async-http 和圆形滚动条开源项目的测试程序。
async-profiler-1.8.1-linux-x64.tar.gz
最新异步网络请求android-async-http-1.4.5.jar