public static <T, S> Flux<T> generate(Callable<S> stateSupplier, BiFunction<S, SynchronousSink<T>, S> generator, Consumer<? super S> stateConsumer) {
//创建FluxGenerate生产者,接收Callable<S> stateSupplier、BiFunction<S, SynchronousSink<T>, S> generator、Consumer<? super S> stateConsumer
return onAssembly(new FluxGenerate<>(stateSupplier, generator, stateConsumer));
}
@Override
public void subscribe(CoreSubscriber<? super T> actual) {
S state;
try {
//调用生产者内部的Callable<S> stateSupplier拿到结果state
state = stateSupplier.call();
} catch (Throwable e) {
Operators.error(actual, Operators.onOperatorError(e, actual.currentContext()));
return;
}
//调用Subscriber#onSubscriber(Subscription)建立订阅关系,,这里的Subscription为GenerateSubscription,其包装actual, state, generator, stateConsumer,调用Subscription#request(n)将会调用GenerateSubscription#request()
actual.onSubscribe(new GenerateSubscription<>(actual, state, generator, stateConsumer));
}
//直接来看GenerateSubscription#request(long n):
//当自定义Subscription#request(n)时,调用slowPath(n)
@Override
public void request(long n) {
if (Operators.validate(n)) {
//cas add n to request
if (Operators.addCap(REQUESTED, this, n) == 0) {
if (n == Long.MAX_VALUE) {
//非背压
fastPath();
} else {
//自定义按需消费
slowPath(n);
}
}
}
}
void fastPath() {
//获取GenerateSubscription内部包装的Callable<S> stateSupplier#call()得到的结果state。state来源同下面generator
S s = state;
//同样,拿到BiFunction<S, SynchronousSink<T>, S> generator
//这里首先创建生产者FluxGenerate时,BiFunction<S, SynchronousSink<T>, S> g = generator作为其初始构造成员传入,当产生订阅时,在subscribe()中调用Subscriber#onSubscribe(Subscription)时,通过生产者FluxGenerate的成员BiFunction<S, SynchronousSink<T>, S> generator作为GenerateSubscription构造成员之一传入,因此Subscriber#onSubscribe(Subscription)便建立了发布订阅过程,进而调用Subscription#request(n)开启数据流
final BiFunction<S, SynchronousSink<T>, S> g = generator;
for (; ; ) {
if (cancelled) {
cleanup(s);
return;
}
try {
//BiFunction<S, SynchronousSink<T>, S> generator,apply执行,传入state和GenerateSubscription对象,进而调用SynchronousSink(GenerateSubscription实现了该接口)#next(t),回调Subscriber消费下发元素
s = g.apply(s, this);
} catch (Throwable e) {
cleanup(s);
actual.onError(Operators.onOperatorError(e, actual.currentContext()));
return;
}
//terminate为true标识订阅完成,调用cleanup(s)
if (terminate || cancelled) {
cleanup(s);
return;
}
if (!hasValue) {
cleanup(s);
actual.onError(new IllegalStateException("The generator didn't call any of the " +
"SynchronousSink method"));
return;
}
hasValue = false;
}
}
//消费n个
void slowPath(long n) {
S s = state;
long e = 0L;
final BiFunction<S, SynchronousSink<T>, S> g = generator;
for (; ; ) {
while (e != n) {
if (cancelled) {
cleanup(s);
return;
}
try {
//
s = g.apply(s, this);
} catch (Throwable ex) {
cleanup(s);
actual.onError(ex);
return;
}
if (terminate || cancelled) {
cleanup(s);
return;
}
if (!hasValue) {
cleanup(s);
actual.onError(new IllegalStateException("The generator didn't call any of the " +
"SynchronousSink method"));
return;
}
e++;
hasValue = false;
}
n = requested;
if (n == e) {
state = s;
//当n==e,e为消费元素的数量,如果n==e return
n = REQUESTED.addAndGet(this, -e);
if (n == 0L) {
return;
}
}
}
}
//由Subscription#request()内部调用
@Override
public void next(T t) {
if (terminate) {
Operators.onNextDropped(t, actual.currentContext());
return;
}
if (hasValue) {
error(new IllegalStateException("More than one call to onNext"));
return;
}
//noinspection ConstantConditions
if (t == null) {
error(new NullPointerException("The generator produced a null value"));
return;
}
//设置hasValue=true
hasValue = true;
if (outputFused) {
generatedValue = t;
} else {
//消费t元素
actual.onNext(t);
}
}
//完成时下发complete事件,并由Subscriber消费,设置terminate中断
@Override
public void complete() {
if (terminate) {
return;
}
terminate = true;
if (!outputFused) {
actual.onComplete();
}
}
//清理资源,执行stateConsumer.accept(s);
void cleanup(S s) {
try {
state = null;
stateConsumer.accept(s);
} catch (Throwable e) {
Operators.onErrorDropped(e, actual.currentContext());
}
}
总结:Flux#generate()创建FluxGenerate,接收Callable<S> stateSupplier, BiFunction<S, SynchronousSink<T>, S> generator, Consumer<? super S> stateConsumer。当订阅建立时,通过Callable<S> stateSupplier#call()得到状态值,将状态值传入BiFunction<S, SynchronousSink<T>, S>得到新的同类型状态值,由SynchronousSink实现类GenerateSubscription进行下发状态值由Subscriber消费,消费完成时执行stateConsumer清理资源。