- 浏览: 561934 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
jiang2011jiang:
mybatis3源码核心类1--Configuration -
tuyf_hs:
同求 图片
zookeeper+dubbo+dubbo管理集群的简要配置[单机] -
安静听歌:
请问图片还能找的会吗?你的图片和原文的图片都挂了,,,如果有图 ...
zookeeper+dubbo+dubbo管理集群的简要配置[单机] -
ahua186186:
yngwiet 写道楼主,有一个地方不太明白,为什么要用“ge ...
ListView中getChildAt(index)的使用注意事项 -
yngwiet:
楼主,有一个地方不太明白,为什么要用“getChildAt(p ...
ListView中getChildAt(index)的使用注意事项
基本原理:
通过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 578经过1周的源码研究,终于对shiro的原理有了深刻的理解,基于 ... -
shiro login成功后保存了哪些数据
2018-06-19 17:05 1467shiro login成功后 保存了Principals 和 ... -
shiro 会话原理分析
2018-06-19 12:40 15891、从哪里获取sessionid每次请求都会尝试获取ses ... -
shiro内部原理分析
2018-06-15 17:07 2404一句话总结:会话域Context一路收集principal ... -
Zookeeper入门-001 源码环境搭建
2018-03-15 11:47 9031.到github下载源码:https://github.c ... -
dubbo服务治理之路由规则研究
2018-01-31 15:50 22081.今天没太多事情,挤 ... -
shiro SecurityUtils.getSubject()深度分析
2018-01-12 17:38 489301.总的来说,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 2354最近看了看开源项目appllo配置中心的源码,发现一个很有意思 ... -
httpclient发送webservice
2017-05-03 23:25 0http://aperise.iteye.com/blog/2 ... -
与大师面对面交流:Chris Richardson 来华布道微服务架构
2016-11-28 21:28 820http://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 27761.生成公钥和私钥放到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 22341.spring AOP的原理都知道是从解析自定义标签时注册一 ...
相关推荐
Spring Boot利用@Async实现异步调用:自定义线程池 Spring Boot是一个流行的Java框架,它提供了许多实用的特性来简化应用程序的开发。其中,异步调用是Spring Boot中的一个重要特性,它允许开发者将一些耗时的任务...
前端开源库-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-...
在Spring Boot框架中,@Async注解是实现异步任务处理的关键工具,它允许开发者将耗时的操作从主线程中分离出来,以提高应用程序的响应速度。本文将深入探讨@Async的工作原理、配置方法以及使用场景,帮助你更好地...
本文将详细介绍async-profiler 1.8.1版本在Linux x64平台上的应用及其实现的原理。 一、async-profiler概述 async-profiler是由Evan Jones开发的一款开源工具,其主要特点是能够在不影响程序执行的前提下,进行...
@EnableAsync 注解会导入 AsyncConfigurationSelector,进而注册 AsyncAnnotationBeanPostProcessor,这个后置处理器实现了 Ordered 接口,它会扫描带有 @Async 注解的方法,并将其包装成一个异步执行的代理对象。...
11:01:33,545 DEBUG ThreadPoolAsynchronousRunner:730 - com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@5b28c08e -- Running DeadlockDetector[Exiting. No pending tasks.] 11:01:43,558 ...
async-web-server-cpp 是一个基于 C++ 实现的异步 Web 服务器库,它专为 ROS1(Robot Operating System 1)环境设计。ROS 是一个开源操作系统,主要用于机器人硬件和软件系统的开发、测试和部署。ROS1 是 ROS 的第一...
5. **认证和安全性**:支持MongoDB的各种安全特性,包括SSL/TLS加密连接、身份验证(如SCRAM-SHA-1或MONGODB-CR)、角色权限管理和访问控制。 6. **CRUD操作**:提供对MongoDB基本的Create(创建)、Read(读取)、...
async-profiler-2.9-linux-x64.tar.gz
在Android应用开发中,网络通信是必不可少的一部分,而`android-async-http`库是一个非常流行的异步HTTP客户端库,特别适合处理与服务器的交互。这个库由Leonardo Uribe创建,它提供了简单易用的API,使开发者可以...
Spring中@Async注解执行异步任务的方法 在业务处理中,有些业务使用异步的方式更为合理。这篇文章主要介绍了Spring中@Async注解执行异步任务的方法。@Async注解是Spring提供的一个annotation,可以将方法异步化,...
@Async 注解是 Spring 框架提供的一种异步执行方法的实现方式,从 Spring 3.0 版本开始提供。使用 @Async 注解可以将方法标识为异步方法,通过 SimpleAsyncTaskExecutor 执行,但不推荐使用这个默认线程池。 @Async...
async-webservices-example-webapp-7.0.0pre1-sources.jar
async_web_server_cpp在ros2环境下的源代码包,下载可以直接编译使用,详细用法可以参看相关文章
下面我们将详细探讨Async-HTTP-Client的核心特性和使用方法。 1. **异步编程模型**: Async-HTTP-Client采用了非阻塞I/O模型,基于NIO(非阻塞I/O)和Netty框架,能够在高并发环境下高效处理网络请求。这种模型...
资源来自pypi官网。 资源全名:asyncapi_generator-0.1.0-py3-none-any.whl
这个"android-async-http progress-wheel测试程序"是为了展示如何结合这两个库来实现一个功能完善的网络请求组件。测试程序中可能包含以下关键知识点: 1. **android-async-http的使用**: - 创建AsyncHttpClient...
《Android异步HTTP框架——..."android-async-http-1.4.3"作为Android开发中的一个重要工具,极大地简化了网络请求的实现,提高了开发效率。理解并熟练运用这个框架,对于任何Android开发者来说都是一项必备技能。
强大的网络请求库,主要特征如下: 处理异步Http请求,并通过匿名内部类处理回调结果 Http请求均位于非UI线程,...通过线程池处理并发请求 处理文件上传、下载 响应结果自动打包JSON格式 自动处理连接断开时请求重连