您的当前位置:首页正文

事件总线框架:EventBus 实现原理

2024-11-19 来源:个人技术集锦


事件总线框架:EventBus 实现原理

EventBus框架原理解析
https:///qq_30124547/article/details/108380894
EventBus


AIDL 观察者模式。

Subscription:订阅者信息,包含订阅主体及方法,一个订阅主体可能会监听多个Event事件,所以订阅主体会被多次引用

EventBus Poster:主要为处理任务的线程,主要三个实现AsyncPoster和BackgroundPoster实现差不多,都是使用线程池处理任务。HandlerPoster就是Android主线程的Handler了

HandlerPoster就是Android主线程的Handler了

EventBusBuilder:建造者模式,提供了默认的builder实现,EventBus

PendingPost:PendingPostQueue池中的对象,自身带缓存,缓存数量源码中为1w。

Subscibe注解b Target = method的注解,可定义线程模型、粘性事件、优先级,运行时注解一般都是通过反射调用的方式。

MainThreadSupport:主线程的封装,比较简单。

SubscriberInfoIndex:新特性,可以把观察者方法提前进行映射,不需要运行时反射获取。使用EventBusAnnotationProcessor自动生成SubscriberInfoIndex继承类。当然也可以手动实现,如果不怕麻烦的话,

post


invokeSub mainThread

mainThreadPoster

enquueu

SubscriberMethodFinder 

findUsingReflectionInSingleClass

SubscriberMethodFinder
SubscriberMethodFinder

findUsingReflectionInSingleClass


findState.clazz.getDeclare();

method.getParameter

checkAdd 

methods
Class<?>[] classes = 

method.getModifiers

Subscibe注解b method.getAnnotation


method.getDeclaringClass .getName()


粘性事件
在注册event的时候,就会检查是否为粘性事件,如果是,则匹配直接回调

event


Android开发事件总线之EventBus运用和框架原理深入理解
https://www.cnblogs.com/alex-mh/p/6737709.html

android的广播机制是基于系统的Binder机制实现IPC或者进程内部的通信

Publisher   Event post  EventBus

Event  Subscriber onEvent


Subscribe sticky = (true)

EventBus 3.0源码详解
Registers the given subscriber to receive events. 

SubscriberMethod   subscriberClass

indUsingReflectionInSingleClass方法:
findUsingReflectionInSingleClass
findState
getModifiers  Modifier.Publishe

Subscribe subscribeAnnotation = 
method.getAnnotation Subscribe.class


  findState.subscriberMethods.add(new SubscriberMethod(method, eventType, threadMode,
                            subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
                            
                            
                SubscriberMethod            


至此,我们得到了所有的订阅函数列表,下一步,会对每一个订阅函数进行注册:

// Must be called in synchronized block
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {

subscriptionsByEventType.put eventType,subscription

typesBySubscriber.get subscriber
List<Class<?>> subscribedEvents = 


            checkPostStickyEventToSubscription(newSubscription, stickyEvent);
            
            监听者
            class订阅方法
            
postSingleEvent eventQueue.remove 0,postingState

post NoSubscriberEvent postToSubscription

subscription.subscriberMethod.threadMode

invokeSubscriber.enqueue subscription,event

asyncPoster.enqueue 

EventBus 原理解析
https://www.jianshu.com/p/d9516884dbd4

@Subscribe

handleEvent event

MAIN_ORDERED
BACKGROUND

ThreadMode  mainThreadPoster backgroundPoster(asyncPoster)

executorService        private static final EventBusBuilder DEFAULT_BUILDER = new EventBusBuilder();

EventBus.builder() builder
.build
.register

findSubscriberMethods subclasses

METHOD_CACHE ConcurrentHashMap  SubscriberMethod

 // 保存查找到的订阅事件的方法
            METHOD_CACHE.put(subscriberClass, subscriberMethods);
            
            
subscriberMethod.method    eventType

findState.moveToSuperclass

subscriberMethods

getMethodsAndRelease findState

SubscriberMethodFinder

       Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
            anyMeth
()            


  List<Class<?>> subscribedEvents 
subscribeAnnotation.threadMode
ThreadMode threadMode = 

findState.subscriberMethods.add(new SubscriberMethod(method, eventType, threadMode,
                                    subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
                                    
                                    
    subscribeAnnotation.priority,subscribeAnnotation.sticky

    register findSubscriberMethods
    
    subscribe
    
    subscribe(Object subscriber,
    SubscriberMethod subscriberMethod) {
    }

Subscriptions eventType
typesBySubscriber.get subscriber

checkPostStickyEventToSubscription
newSubscription stickyEvent


postSingleEvent PostThreadState:如果事件处理函数指定了线程模型为PostThread,那么事件的发布和接收处理会在同一个线程当中。:如果事件处理函数指定了线程模型为PostThread,那么事件的发布和接收处理会在同一个线程当中。


先使用说明+ 原理解析

lookupAllEventTypes eventClass

23 Class<?> eventClass subscriptions

postToSubscription 

invokeSubscriber  subscription,event

enqueue.subscription,event
//postToSubscription()


invokeSubscriber

method.invoke 23 .subscribe,event


mainThreadPoster.enqueue subscription,event

    if (!sendMessage(obtainMessage())) {

    queue.poll 
    invokeSubscriber
    
    sticky  subscriberMethod
    
    checkPostStickyEventToSubscription newSubscription,stickyEvent
    
    
    postToSubscription newSubscription
    
Subscriber Index  Subscriber Index 

annotationProcessor    compile
annotation-processor       

  defaultConfig {
        javaCompileOptions {
            annotationProcessorOptions {
                // 根据项目实际情况,指定辅助索引类的名称和包名
                arguments = [ eventBusIndex : 'com.shh.sometest.MyEventBusIndex' ]
            }
        }
    }
}
dependencies {
    compile 'org.greenrobot:eventbus:3.1.1'
    // 引入注解处理器
    annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.1.1'
}

Map<Class<?>,SubscriberInfo> Subscriber_index

get SubscribeClass SubscribeInfo


SubscribeMethod  Class<?> subscriberClass

HandlerPoster Poster

  findState.initForSubscriber(subscriberClass);
  //initForSubscriber subscriberClass

subscriberMNe/
09
 if (findState.clazz == superclassInfo.getSubscriberClass()) {

 findState.clazz == superclassInfo.get

 public SubscriberInfo getSubscriberInfo(Class<?> subscriberClass) {
 prepareFindState FindState findState
 
 .clazz   getSubscriberInfo findState
 
 //getSubscriberMethods
 
       // 获得当前注册类中所有订阅了事件的方法
       
       
       
 findUsingReflectionInSingleClass
 
 ReflectionIn

moveToSuperclass

getMethodsAndRelease findState

getSubscriberInfo findState.clazz

 if (findState.clazz == superclassInfo.getSubscriberClass()) {

Subscriber Index  
SubscribeMethod


SubscriberExceptionEvent

stickyEvents

subscribedTypes


typesBySubscriber
Android反射调用//-》 https:///calm1516/article/details/90902784

Class.forName

c.getDeclaredFields Field[] fields getModifiers
c.getModifiers  c.getSimpleName

field.getType.getSimpleName

field.23

getSimpleName

method.getParameters

method.getType.getSimpleName

getModifiers

newInstance

a.set mutoude123 ,1

int res = (int)add.invoke(myClass,null);

显示全文