Rxjava is an implementation of responsive programming. In responsive programming, when the data arrives, the consumer RESPONDS. Responsive programming can pass events to registered Observer. Today we will study the rxjava, and analyze it the source to feel its observer pattern.
Rxjava the simple use of a, Mavan pom.xml to increase the dependence of Rxjava
Here we are using rxjava1.3.0, the latest has been updated to 2.
<Dependency> <groupId>Io.reactivex</groupId> <Artifactid>Rxjava</Artifactid> <version>1.3.0</version></Dependency>
second, the use of the test class
Importrx. Observable;Importrx. subscriber;/** * @authorHuhx*/ public classRxjavatest { public Static voidmain (string[] Args) {Observable<String> observable = observable.just ("one", "one", "one", "three"); Observable.subscribe (NewSubscriber<string>() {@Override public voidoncompleted () {System.out.println ("oncompleted"); } @Override public voidonError (throwable Item) {System.out.println ("onError"); } @Override public voidonNext (String Item) {System.out.println ("Item is" +item); } }); }}
third, The result of printing is as Follows:
is are threeoncompleted
Simple analysis of Rxjava source code
According to the above code, we simply analyze the process of the Program.
first, observable.just ("one", "a", "three") code:
Just is a factory method that constructs a observable object with Heart.
public static <T> observable<t> just (t t1, t t2, t t3) { return from ((t[])new
object[] {t1, t2, t3});}
The code for the From method is as Follows:
public static <T> observable<t> from (t[] array) { int n = array.length; if (n = = 0) {return empty () ; Else if (n = = 1) {return just (array[0]); } return unsafecreate (new onsubscribefromarray<t>(array));}
second, our focus is to look at the Unsafecreate method:
public static <T> observable<t> unsafecreate (onsubscribe<t> f) { return New observable<t>(rxjavahooks.oncreate (f));
For the rxjavahooks.oncreate () method, the code for the Rxjavahooks static block is executed First.
Static { // initialize a lot of variables with Rxjavahooks and events }
OnCreate method, This does not understand how to see the Follow-up.
public static <T> observable.onsubscribe<t> onCreate (observable.onsubscribe<t> onsubscribe ) { Func1<observable.onsubscribe, observable.onsubscribe> f = onobservablecreate; if NULL ) { return f.call (onsubscribe); } return onsubscribe;}
third, for the Observable.subscribe () code
public Final Super t> subscriber) { returnthis);}
The Subscribe method code for observable is as Follows:
1 Static<T> Subscription Subscribe (subscriber<?SuperT> subscriber, observable<t>Observable) {2 if(subscriber = =NULL) {3 Throw NewIllegalArgumentException ("subscriber can is not null");4 }5 if(observable.onsubscribe = =NULL) {6 Throw NewIllegalStateException ("onsubscribe function can not be Null."));7 }8 9 Subscriber.onstart ();Ten one if(! (subscriberinstanceofSafesubscriber)) { aSubscriber =NewSafesubscriber<t>(subscriber); - } - the Try { - rxjavahooks.onobservablestart (observable, observable.onsubscribe). call (subscriber); - returnRxjavahooks.onobservablereturn (subscriber); -}Catch(throwable E) { + exceptions.throwiffatal (e); - if(subscriber.isunsubscribed ()) { + rxjavahooks.onerror (rxjavahooks.onobservableerror (e)); a}Else { at Try { - subscriber.onerror (rxjavahooks.onobservableerror (e)); -}Catch(throwable E2) { - exceptions.throwiffatal (e2); -RuntimeException r =NewOnerrorfailedexception ("Error occurred attempting to subscribe [" + e.getmessage () + "] and then again while trying to pas S to OnError. ", e2); - Rxjavahooks.onobservableerror (r); in ThrowR//NOPMD - } to } + returnsubscriptions.unsubscribed (); - } the}
four, the entire normal process will go to 16 lines of code, This is our focus on the analysis of the Place. If an exception occurs, only the OnError method is executed
For this example, Rxjavahooks.onobservablestart (observable, observable.onsubscribe) Gets the result onsubscribefromarray this class. Call method that calls it.
public void Super t> child ) { child.setproducer (new fromarrayproducer<t>(child, array));}
The code for the Setproducer method executes as Follows:
1 public voidsetproducer (Producer P) {2 Longtorequest;3 BooleanPasstosubscriber =false;4 synchronized( this) {5Torequest =requested;6Producer =p;7 if(subscriber! =NULL) {8 //Middle operator ... we pass through unless a request has been made9 if(torequest = =Not_set) {Ten //we pass through to the next producer as nothing have been requested onePasstosubscriber =true; a } - } - } the //Do after releasing lock - if(passtosubscriber) { - Subscriber.setproducer (producer); -}Else { + //we execute the request with whatever have been requested (or Long.max_value) - if(torequest = =Not_set) { + producer.request (long.max_value); a}Else { at producer.request (torequest); - } - } -}
Here we go to 21 lines of code, followed by the request method of Fromarrayproducer:
public voidRequestLongN) {if(n < 0) { Throw NewIllegalArgumentException ("n >= 0 required but it is" +n); } if(n = =Long.max_value) { if(backpressureutils.getandaddrequest ( this, N) = = 0) {fastPath (); } } Else if(n! = 0) { if(backpressureutils.getandaddrequest ( this, N) = = 0) {slowpath (n); } }}
The code for FastPath () is as Follows:
void fastPath () { finalSuperthis. child; for (T t:array) { if (child.isunsubscribed ()) { return; } child.onnext (t); } if (child.isunsubscribed ()) { return; } child.oncompleted ();}
Executes the onnext () method, where T is the parameter of the Observable.just () method. A child is a defined parameter of Observable.subscribe.
Friendship Link
Java framework----use of >rxjava (i)