Preface
Previous use of Eventbus in projects for event notifications and subscriptions.
Now implement a publish/Subscribe event bus using Rxjava: Rxbus. Rxjava 1.x Implementation Rxbus Code:
/** * Desc: Take advantage of the Publishsubject feature: Unlike ordinary subject, subscriptions are not immediately triggered at the time of subscription, * but allow us to manually invoke OnNext (), OnError (), oncompleted to trigger events at any time. * Author:stone * email:aa86799@163.com * time:24/04/2017/public class Rxbus {private Concurre
Nthashmap<object, list<subject>> subjectmapper = new concurrenthashmap<> ();
Private Rxbus () {} private static class Holder {private static Rxbus instance = new Rxbus ();
public static Rxbus getinstance () {return holder.instance; Public <T> observable<t> Register (@NonNull class<t> clz) {return register (Clz.getname ())
; Public <T> observable<t> Register (@NonNull Object tag) {list<subject> subjectlist = subj
Ectmapper.get (tag);
if (null = = subjectlist) {subjectlist = new arraylist<> ();
Subjectmapper.put (tag, subjectlist); } subject<t, t> Subject = PublishsUbject.create ();
Subjectlist.add (subject);
SYSTEM.OUT.PRINTLN ("Registered to Rxbus");
return subject; Public <T> void Unregister (@NonNull class<t> clz, @NonNull observable observable) {unregister (
Clz.getname (), observable); public void unregister (@NonNull Object tag, @NonNull observable observable) {list<subject> subjects
= Subjectmapper.get (tag);
if (null!= subjects) {subjects.remove (observable);
if (Subjects.isempty ()) {subjectmapper.remove (tag);
System.out.println ("Cancellation of registration from Rxbus");
}} public void post (@NonNull Object content) {post (Content.getclass (). GetName (), content); public void post (@NonNull object tag, @NonNull object content) {list<subject> subjects = SUBJECTM
Apper.get (tag); if (!subjects.isempty ()) {for (Subject subject:subjects) {suBject.onnext (content);
}
}
}
}
Several key methods:
register--by tag, generates a subject List, and uses Publishsubject to create a subject and return, which is also a subclass of the observable.
unregister--removes the tag corresponding to the observable in the subject list. If the subject list is empty, it will also be removed.
post--traversal tag corresponds to the subject in the subject list and executes OnNext (). What is actually performed here is the OnNext () of the Observer observer, the definition of Subject: Public abstract class subject<t, r> extends Implements Observer<t>. Test code:
/*
Rxbus
* *
observable<string> observable = Rxbus.getinstance (). Register (string.class);
Observable.map (s-> {
try {
int v = integer.valueof (s);
SYSTEM.OUT.PRINTLN ("Map transformation succeeded, Source =" + s);
return v;
} catch (Exception e) {
System.out.println ("Map transform failed, Source =" + s);
return s;
}
}). Subscribe (value-> {
System.out.println ("subscription" + value);
});
Rxbus.getinstance (). Post ("888");
Rxbus.getinstance (). Post ("Hair fat");
Rxbus.getinstance (). Unregister (String.class, observable);
The interesting thing here is that lambda expressions are used. In the map transform, if the string is converted to an integer, the integer is returned without a problem, and the string is returned if the exception is reported. Similarly, at the end of a subscription, the type of the value parameter is also determined by the map transformation.
Rxjava 2.x Implementation
After the Rxjava 2.0, Io.reactivex.Observable no back pressure processing, if a large number of messages piled up in the bus too late to deal with will produce missingbackpressureexception or outofmemoryerror, there are new classes Io.reactivex.Fl Owable specifically for back pressure problems.
Observable implementation without back pressure processing, as in rxjava1.x, using Publishsubject to achieve.
To achieve the 2.x version with back pressure, use flowableprocessor subclass Publishprocessor to generate flowable to achieve rxbus. Rxbus Code:
public class Rxbus {
private final flowableprocessor<object> MBus;
Private Rxbus () {
MBus = Publishprocessor.create (). toserialized ();
}
private static class Holder {
private static Rxbus instance = new Rxbus ();
}
public static Rxbus getinstance () {return
holder.instance;
}
public void post (@NonNull Object obj) {
mbus.onnext (obj);
}
Public <T> flowable<t> Register (class<t> clz) {return
mbus.oftype (CLZ);
}
public void UnregisterAll () {
//all MBus generated flowable will be placed on all messages that are completed state follow up
. Mbus.oncomplete ();
}
public Boolean hassubscribers () {return
mbus.hassubscribers ();
}
}
Test code:
Flowable<integer> f1 = rxbus.getinstance (). Register (integer.class);
F1.subscribe (Value-> System.out.println ("Subscribe to F1 message ..." + value));
Rxbus.getinstance (). Post (999);
Reference
Http://www.cnblogs.com/tiantianbyconan/p/4578699.html "[Android] based on Rxjava, rxandroid Eventbus Implementation"
Http://johnnyshieh.github.io/android/2017/03/10/rxbus-rxjava2/?utm_source=tuicool&utm_medium=referral the Rxjava 2 version of the Rxbus "