Java教程

guava EventBus源码阅读

本文主要是介绍guava EventBus源码阅读,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

post → @Subscribe 方法流程

1.post 调用EventBus  this.dispatcher.dispatch(event, eventSubscribers);
2.dispatcher = Dispatcher.perThreadDispatchQueue()
3. new Dispatcher.PerThreadQueuedDispatcher() = new PerThreadQueuedDispatcher(queue,dispatching)
4. PerThreadQueuedDispatcher.dispatch = while(nextEvent.subscribers.hasNext()) {
                            ((Subscriber)nextEvent.subscribers.next()).dispatchEvent(nextEvent.event);
                        }
到此 post跟踪完毕  现在就是要知道 nextEvent 是谁 

1. nextEvent  = new Dispatcher.PerThreadQueuedDispatcher.Event(event, subscribers)
继续追 event,subscribers 
发现是最开始 post传进来的,
现在我们要明白一件事情  post 就是  调用了 eventSubscribers里面的dispatchEvent,开始追subscribers 
2.Iterator<Subscriber> eventSubscribers = this.subscribers.getSubscribers(event);
3.
Iterator<Subscriber> getSubscribers(Object event) {
        ImmutableSet<Class<?>> eventTypes = flattenHierarchy(event.getClass());
        List<Iterator<Subscriber>> subscriberIterators = Lists.newArrayListWithCapacity(eventTypes.size());
        UnmodifiableIterator var4 = eventTypes.iterator();

        while(var4.hasNext()) {
            Class<?> eventType = (Class)var4.next();
            CopyOnWriteArraySet<Subscriber> eventSubscribers = (CopyOnWriteArraySet)this.subscribers.get(eventType);
            if (eventSubscribers != null) {
                **subscriberIterators.add(eventSubscribers.iterator());**
            }
        }

        return Iterators.concat(subscriberIterators.iterator());
    } 
4. 发现eventSubscribers 是从this.subscribers拿的开始追this.subscribers
5.
void register(Object listener) {
        Multimap<Class<?>, Subscriber> listenerMethods = this.findAllSubscribers(listener);

        Collection eventMethodsInListener;
        CopyOnWriteArraySet eventSubscribers;
        for(Iterator var3 = listenerMethods.asMap().entrySet().iterator(); var3.hasNext(); eventSubscribers.addAll(eventMethodsInListener)) {
            Entry<Class<?>, Collection<Subscriber>> entry = (Entry)var3.next();
            Class<?> eventType = (Class)entry.getKey();
            eventMethodsInListener = (Collection)entry.getValue();
            eventSubscribers = (CopyOnWriteArraySet)this.subscribers.get(eventType);
            if (eventSubscribers == null) {
                CopyOnWriteArraySet<Subscriber> newSet = new CopyOnWriteArraySet();
                eventSubscribers = (CopyOnWriteArraySet)MoreObjects.firstNonNull(this.subscribers.putIfAbsent(eventType, newSet), newSet);
            }
        }

    }
6.发现了,eventSubscribers这个是在register做的初始化
看看我们register传了什么
7.
EventBus eventBus = new EventBus();
        eventBus.register(new EventListener());
        eventBus.post(1);
8.class EventListener {
    @Subscribe
    public void listenInteger(Integer param) {
        System.out.println("EventListener#listenInteger ->" + param);
    }

    @Subscribe
    public void listenInteger2(Integer param) {
        System.out.println("EventListener#listenInteger2 ->" + param);
    }

    @Subscribe
    public void listenString(String param) {
        System.out.println("EventListener#listenString ->" + param);
    }
}
9.明白明白了,this.subscribers里面就是我们传入的带有Subscribe注解各种方法,

10.并且这些方法在 
private Multimap<Class<?>, Subscriber> findAllSubscribers(Object listener) {
        Multimap<Class<?>, Subscriber> methodsInListener = HashMultimap.create();
        Class<?> clazz = listener.getClass();
        UnmodifiableIterator var4 = getAnnotatedMethods(clazz).iterator();

        while(var4.hasNext()) {
            Method method = (Method)var4.next();
            Class<?>[] parameterTypes = method.getParameterTypes();
            Class<?> eventType = parameterTypes[0];
            methodsInListener.put(eventType, Subscriber.create(this.bus, listener, method));
        }

        return methodsInListener;
    }
包装成了Subscriber
11.现在我们只要看 Subscriber的dispatchEvent就知道到底干了什么了

12.
final void dispatchEvent(final Object event) {
        this.executor.execute(new Runnable() {
            public void run() {
                try {
                    Subscriber.this.invokeSubscriberMethod(event);
                } catch (InvocationTargetException var2) {
                    Subscriber.this.bus.handleSubscriberException(var2.getCause(), Subscriber.this.context(event));
                }

            }
        });
    }

    @VisibleForTesting
    void invokeSubscriberMethod(Object event) throws InvocationTargetException {
        try {
            this.method.invoke(this.target, Preconditions.checkNotNull(event));
        } catch (IllegalArgumentException var3) {
            throw new Error("Method rejected target/argument: " + event, var3);
        } catch (IllegalAccessException var4) {
            throw new Error("Method became inaccessible: " + event, var4);
        } catch (InvocationTargetException var5) {
            if (var5.getCause() instanceof Error) {
                throw (Error)var5.getCause();
            } else {
                throw var5;
            }
        }
    }
13.发现这不就是调用自己的method了么,到此全部解析完毕。
这篇关于guava EventBus源码阅读的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!