Eventbus Source Read (c)--Subscribe

Source: Internet
Author: User
Tags eventbus

After the subscription of the class, will not accept the eventbus,post out of the message. So let's take a look at the subscription process today.

Eventbus.getdefault (). Register (this);
    /*** Registers the given subscriber to receive events. Subscribers must call {@link#unregister (Object)} once they * is no longer interested in receiving events. * <p/> * Subscribers has event handling methods that must being annotated by {@linkSubscribe}. * The {@linkSubscribe} Annotation also allows configuration like {@link* Threadmode} and priority. */     Public voidRegister (Object subscriber) {Class<?> Subscriberclass =Subscriber.getclass (); List<SubscriberMethod> Subscribermethods =subscribermethodfinder.findsubscribermethods (Subscriberclass); synchronized( This) {             for(Subscribermethod subscribermethod:subscribermethods) {Subscribe (subscriber, Subscribermethod); }        }    }

This method is relatively simple to find a way to subscribe from the class, and then execute subscribe (subscriber, Subscribermethod);

The implementation of the Subscribe method is rather long, and we look at it in chunks:

Class<?> EventType =Subscribermethod.eventtype; Subscription newsubscription=NewSubscription (subscriber, Subscribermethod); Copyonwritearraylist<Subscription> subscriptions =Subscriptionsbyeventtype.get (EventType); if(Subscriptions = =NULL) {Subscriptions=NewCopyonwritearraylist<>();        Subscriptionsbyeventtype.put (EventType, subscriptions); } Else {            if(Subscriptions.contains (newsubscription)) {Throw NewEventbusexception ("subscriber" + subscriber.getclass () + "already registered to event" +EventType); }        }        intSize =subscriptions.size ();  for(inti = 0; I <= size; i++) {            if(i = = Size | | subscribermethod.priority >Subscriptions.get (i). subscribermethod.priority)                {Subscriptions.add (i, newsubscription);  Break; }        }

First, a new subscription is created in subscribe that is used to store subscribers and subscribers. Then, through the subscriber's subscription model, the Subscriber queue--subscriptions is found in this mode. If the new subscriptions is added directly, an exception is thrown if it already exists. Next, the subscription is added to subscriptions based on the priority of the subscription method.

        list<class<?>> subscribedevents = typesbysubscriber.get (subscriber);         if NULL ) {            new arraylist<>();            Typesbysubscriber.put (subscriber, subscribedevents);        }        Subscribedevents.add (EventType);

This section of code determines whether the current feed has a list of events that already exist, if it already exists, join directly, if not, create a new join. Typesbysubscriber will be used to determine if the feed is registered.

        if(subscribermethod.sticky) {if(eventinheritance) {//Existing Sticky events of all subclasses of EventType has to be considered. //note:iterating over all events inefficient with lots of sticky events,//thus data structure should be changed to allow a more efficient lookup//(e.g. an additional map storing sub classes of Super Classes:class-list<class>).Set<map.entry<class<?&gt, object>> entries =Stickyevents.entryset ();  for(Map.entry<class<?>, object>entry:entries) {Class<?> Candidateeventtype =Entry.getkey (); if(Eventtype.isassignablefrom (Candidateeventtype)) {Object stickyevent=Entry.getvalue ();                    Checkpoststickyeventtosubscription (Newsubscription, stickyevent); }                }            } Else{Object stickyevent=Stickyevents.get (EventType);            Checkpoststickyeventtosubscription (Newsubscription, stickyevent); }        }    }

Finally, a piece of sticky event handling, do not delve into this.

As you can see, Eventbus's subscription and the final bridge to send events are subscriptionsbyeventtype. In the Send event

    Private BooleanPostsingleeventforeventtype (Object event, Postingthreadstate postingstate, class<?>EventClass) {Copyonwritearraylist<Subscription>subscriptions; synchronized( This) {Subscriptions= subscriptionsbyeventtype. Get (EventClass); }        if(Subscriptions! =NULL&&!Subscriptions.isempty ()) {             for(Subscription subscription:subscriptions) {postingstate.event=event; Postingstate.subscription=subscription; Booleanaborted =false; Try{posttosubscription (subscription, event, Postingstate.ismainthread); Aborted=postingstate.canceled; } finally{postingstate.event=NULL; Postingstate.subscription=NULL; Postingstate.canceled=false; }                if(aborted) { Break; }            }            return true; }        return false; }

During the subscription process, the feed is put into subscriptionsbyeventtype .

   

The initial reading process, here, next time to see the implementation of sticky events.

done~

Eventbus Source Read (c)--Subscribe

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.