您的位置:首页 > 编程语言 > Java开发

SpringIOC源码解析之--如何实现多播器的异步多播的(ApplicationEventMulticaster)

2019-06-16 09:27 369 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/pengweismile/article/details/92363266

支持异步多播

本文主要关注的代码为org.springframework.context.support.AbstractApplicationContext#refresh方法中的initApplicationEventMulticaster(), registerListeners()这两行代码,

1. 初始化一个事件多播器

[code]//org.springframework.context.support.AbstractApplicationContext#refresh
// Initialize event multicaster for this context.
initApplicationEventMulticaster();

2. 注册一个监听器--将一个监听事件注册到上面的多播器里面去

[code]// Check for listener beans and register them.
registerListeners();

如下图所示:

主要解决的问题:如何让spring支持事件多播器的异步多播。

如何实现:自己定义一个多播器,并给bean命名为applicationEventMulticaster:

[code]@Component(value = "applicationEventMulticaster")
public class Myulticaster extends SimpleApplicationEventMulticaster{
public MyMulticaster () {
setTaskExecutor(Executors.newSingleThreadExecutor());
}
}

解释原理: 如下我们看到在以上的判断是否自定义了多播器的代码中,判断在ioc容器中是否包含如下名字的bean作为判断条件的,所以只要我们自定义一个bean命名为applicationEventMulticaster,并把异步支持的executor植入就行了。当我们自己定义了一个继承SimpleApplicationEventMulticaster命名为applicationEventMulticaster的bean时,就会给AbstractApplicationContext类1的this.applicationEventMulticaster 复制成我们自定义的类实例。

[code]protected void initApplicationEventMulticaster() {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
//是否定义了自己的多播器
if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
this.applicationEventMulticaster =
beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
if (logger.isTraceEnabled()) {
logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
}
}
//没有定义自己的多播器就使用系统默认的多播器
else {
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
if (logger.isTraceEnabled()) {
logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " +
"[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");
}
}
}
如下图,applicationEventMulticaster就是 spring给我们预定的类的名字。
[code]/**
* Name of the ApplicationEventMulticaster bean in the factory.
* If none is supplied, a default SimpleApplicationEventMulticaster is used.
* @see org.springframework.context.event.ApplicationEventMulticaster
* @see org.springframework.context.event.SimpleApplicationEventMulticaster
*/
public static final String APPLICATION_EVENT_MULTICASTER_BEAN_NAME = "applicationEventMulticaster";

当在我们自定义的多播器中设置了executor时,下面的exeutor就不为空了 ,就会走到第一个异步多播的路径。

[code]@Override
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
Executor executor = getTaskExecutor();
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
}
else {
invokeListener(listener, event);
}
}
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: