CopyOnWriteArraySet<Subscriber> currentSubscribers = subscribers.get(eventType); if (currentSubscribers == null ||!currentSubscribers.removeAll(listenerMethodsForType)) { throw new IllegalArgumentException( "missing event subscriber for an annotated method. Is "+ listener +" registered?"); }
// don't try to remove the set if it's empty; that can't be done safely without a lock // anyway, if the set is empty it'll just be wrapping an array of length 0 } }
发布消息
1 2 3 4 5 6 7 8 9 10 11 12
publicvoidpost(Object event) { //根据消息类型,和消息的祖先类型,获取对应的观察者 Iterator<Subscriber> eventSubscribers = subscribers.getSubscribers(event); //至少有一个观察者,则循环回掉他们的方法,否则,发送一个DeadEvent,可以写一个观察者观察DeadEvent类,这样如果有某种类型的事件没有观察者可以检测到 if (eventSubscribers.hasNext()) { dispatcher.dispatch(event, eventSubscribers); } elseif (!(event instanceof DeadEvent)) { // the event had no subscribers and was not itself a DeadEvent post(new DeadEvent(this, event)); } }