Observable.create(new ObservableOnSubscribe() {
@Override
public void subscribe(ObservableEmitter emitter) throws Exception {
LogUtils.loge("Observable onSubscribe subscribe...");
if (!emitter.isDisposed()) {
emitter.onNext("test1");
emitter.onComplete();
}
}
}).subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {
LogUtils.loge("Observer onSubscribe ...");
}
@Override
public void onNext(String s) {
LogUtils.loge("Observer onNext str = " + s);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
LogUtils.loge("Observer onComplete ...");
}
});
public static Observable create(ObservableOnSubscribe source) {
return RxJavaPlugins.onAssembly(new ObservableCreate(source));
}
public static Observable onAssembly(@NonNull Observable source) {
Function super Observable, ? extends Observable> f = onObservableAssembly;
// onObservableAssembly为里为空,所以f为空。返回的是我们传入的observable
if (f != null) {
return apply(f, source);
}
return source;
}
@SchedulerSupport(SchedulerSupport.NONE)
@Override
public final void subscribe(Observer super T> observer) {
ObjectHelper.requireNonNull(observer, "observer is null");
try {
observer = RxJavaPlugins.onSubscribe(this, observer);
// 这里实际上是调用的是ObservableCreate中的subscribeActual方法
subscribeActual(observer);
} catch (NullPointerException e) { // NOPMD
} catch (Throwable e) {
}
}
public static Observer super T> onSubscribe(@NonNull Observable source, @NonNull Observer super T> observer) {
BiFunction super Observable, ? super Observer, ? extends Observer> f = onObservableSubscribe;
// onObservableSubscribe,所以f为空。返回的是我们传入的observer
if (f != null) {
return apply(f, source, observer);
}
return observer;
}
@Override
protected void subscribeActual(Observer super T> observer) {
// 构建Emitter
CreateEmitter parent = new CreateEmitter(observer);
// 调用observer的onSubscribe,并传入创建的Emitter
observer.onSubscribe(parent);
try {
// 调用subscribe方法
source.subscribe(parent);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
parent.onError(ex);
}
}
@Override
public boolean isDisposed() {
return emitter.isDisposed();
}
static final class CreateEmitter
extends AtomicReference
implements ObservableEmitter, Disposable {
private static final long serialVersionUID = -3434801548987643227L;
final Observer super T> observer;
CreateEmitter(Observer super T> observer) {
this.observer = observer;
}
@Override
public void onNext(T t) {
if (!isDisposed()) {
observer.onNext(t);
}
}
@Override
public void onComplete() {
if (!isDisposed()) {
try {
observer.onComplete();
} finally {
dispose();
}
}
}
@Override
public void dispose() {
DisposableHelper.dispose(this);
}
@Override
public boolean isDisposed() {
return DisposableHelper.isDisposed(get());
}
}
public enum DisposableHelper implements Disposable {
DISPOSED
;
public static boolean isDisposed(Disposable d) {
// 判断需要dispose的对象是否是已经dispose的
return d == DISPOSED;
}
public static boolean dispose(AtomicReference field) {
/ 取到当前的Disposable的对象
Disposable current = field.get();
// 得到已经disposed的对象
Disposable d = DISPOSED;
if (current != d) {
// 将AtomicReference中Disposable标识为disposed状态
current = field.getAndSet(d);
if (current != d) {
if (current != null) {
current.dispose();
}
return true;
}
}
return false;
}
@Override
public boolean isDisposed() {
return true;
}
}
Flowable.create((FlowableOnSubscribe) emitter -> {
LogUtils.loge("FlowableOnSubscribe subscribe");
if (!emitter.isCancelled()) {
emitter.onNext("test11");
emitter.onComplete();
}
}, BackpressureStrategy.DROP).subscribe(new Subscriber() {
@Override
public void onSubscribe(Subscription s) {
// 需要主动发起请求否则会请求不到数据
s.request(Integer.MAX_VALUE);
LogUtils.loge("Subscriber onSubscribe");
}
@Override
public void onNext(String s) {
LogUtils.loge("Subscriber onNext s = " + s);
}
@Override
public void onError(Throwable t) {
}
@Override
public void onComplete() {
LogUtils.loge("Subscriber onComplete");
}
});
public static Flowable create(FlowableOnSubscribe source, BackpressureStrategy mode) {
// 这里返回的就是FlowableCreate
return RxJavaPlugins.onAssembly(new FlowableCreate(source, mode));
}
public final void subscribe(Subscriber super T> s) {
subscribe(new StrictSubscriber(s));
}
public final void subscribe(FlowableSubscriber super T> s) {
Subscriber super T> z = RxJavaPlugins.onSubscribe(this, s);
// 调用的是FlowableCreate的subscribeActual方法
subscribeActual(z);
}
@Override
public void subscribeActual(Subscriber super T> t) {
BaseEmitter emitter;
// 根据背压策略构建emitter
switch (backpressure) {
case MISSING: {
emitter = new MissingEmitter(t);
break;
}
case ERROR: {
emitter = new ErrorAsyncEmitter(t);
break;
}
case DROP: {
emitter = new DropAsyncEmitter(t);
break;
}
case LATEST: {
emitter = new LatestAsyncEmitter(t);
break;
}
default: {
emitter = new BufferAsyncEmitter(t, bufferSize());
break;
}
}
// 调用 Subscriber的onSubscribe
t.onSubscribe(emitter);
try {
// 调用subscribe方法
source.subscribe(emitter);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
emitter.onError(ex);
}
}
static final class DropAsyncEmitter extends NoOverflowBaseAsyncEmitter {
private static final long serialVersionUID = 8360058422307496563L;
DropAsyncEmitter(Subscriber super T> downstream) {
super(downstream);
}
}
abstract static class NoOverflowBaseAsyncEmitter extends BaseEmitter {
private static final long serialVersionUID = 4127754106204442833L;
NoOverflowBaseAsyncEmitter(Subscriber super T> downstream) {
super(downstream);
}
@Override
public final void onNext(T t) {
if (isCancelled()) {
return;
}
// get的值不为0才会调用onNext方法
// 只有调用了request(n)的时候,这里才不会为0
if (get() != 0) {
downstream.onNext(t);
BackpressureHelper.produced(this, 1);
} else {
onOverflow();
}
}
abstract void onOverflow();
}
abstract static class BaseEmitter
extends AtomicLong
implements FlowableEmitter, Subscription {
private static final long serialVersionUID = 7326289992464377023L;
final Subscriber super T> downstream;
final SequentialDisposable serial;
BaseEmitter(Subscriber super T> downstream) {
this.downstream = downstream;
this.serial = new SequentialDisposable();
}
@Override
public final void cancel() {
serial.dispose();
onUnsubscribed();
}
@Override
public final boolean isCancelled() {
return serial.isDisposed();
}
@Override
public final void request(long n) {
if (SubscriptionHelper.validate(n)) {
BackpressureHelper.add(this, n);
}
}
}
public static boolean validate(long n) {
/* 如果传入的值小于等于0的时候,就直接返回了, 这就是为啥不调用这个request方法,onNext方法是不走的
*/
if (n <= 0) {
RxJavaPlugins.onError(new IllegalArgumentException("n > 0 required but it was " + n));
return false;
}
return true;
}
public static long add(AtomicLong requested, long n) {
for (;;) {
long r = requested.get();
if (r == Long.MAX_VALUE) {
return Long.MAX_VALUE;
}
long u = addCap(r, n);
// requested的这里会被设置成最大值
if (requested.compareAndSet(r, u)) {
return r;
}
}
}
public static long produced(AtomicLong requested, long n) {
for (;;) {
long current = requested.get();
if (current == Long.MAX_VALUE) {
return Long.MAX_VALUE;
}
long update = current - n;
if (update < 0L) {
RxJavaPlugins.onError(new IllegalStateException("More produced than requested: " + update));
update = 0L;
}
if (requested.compareAndSet(current, update)) {
return update;
}
}
}
Subscriber->onSubscribe
s.request(Integer.MAX_VALUE);
FlowableCreate.BaseEmitter#request ->
io.reactivex.internal.util.BackpressureHelper#add
将设置的值更新到BaseEmitter中,BaseEmitter继承自AtomicLong
FlowableOnSubscribe-> subscribe
emitter.onNext(a);
io.reactivex.internal.operators.flowable.FlowableCreate.NoOverflowBaseAsyncEmitter#onNext
获取AtomicLong的值,不为0的时候,才会调用Subscriber的onNext方法
总之,Flowable是复用强制拉取,解决背压策略的