Guava EvenBus源码解读

发布订阅模型

image-20240410205859440

Guava EventBus概述

img

事件注册

 

img

register

 

findAllSubscribers

 

getAnnotatedMethodsNotCached

 

Subscriber

 

事件分发

img

post
getSubscribers
flattenHierarchyCache

 

事件执行

SubscriberRegistry().post方法的事件分发器dispatch常见的类型可以分为两类:PerThreadQueuedDispatcher, LegacyAsyncDispatcher

 

perThreadDispatch

 

LegacyAsyncDispatcher

 

订阅者执行顺序

线程执行

线程池
并发模型

优缺点

Spring Event对比

工作流程

--> 定义事件ApplicationEvent及对应的ApplicationListener

--> 当事件发生时,通过ApplicationEventPublisher接口实现类的publistEvent方法发布事件;

--> publistEvent方法通过ApplicationEventMulticaster接口实现类的multicastEvent方法广播事件;

-->multicastEvent方法找到事件的全部Listener,并通过invokeListener执行他们定义的事件处理逻辑;

-->invokeListener方法将通过ApplicationListenerMethodAdapteronApplicationEvent执行定义的事件处理逻辑,这里的ApplicationListenerMethodAdapterhandler方法的适配器,负责统一handler方法的调用;

-->最后onApplicationEvent将通过processEvent方法使用反射调用,真正执行handler逻辑。

 

过滤特性
顺序特性

参考