Is it possible do read from a webflux flux in chunks? ( other than using delayElements )
For example after I write
Flux.range(1, 10).doOnNext(System.out::println).take(5).subscribe();
is there any way to continue to read the next 5 integers?
If not, is there an alternative for the consumer to decide when to request the next piece of data?
Edit:
To clarify, I would like to read the first 5 values, then pause, then read the next 5 values without recreating the emitter flux.
then you need a full-fledged asynchronous subscriber object, not just a chain of methods.
// use maven dependency 'org.df4j:df4j-core:8.3'
import org.df4j.core.dataflow.Actor;
import org.df4j.core.port.InpFlow;
import org.junit.Assert;
import org.junit.Test;
import reactor.core.publisher.Flux;
public class FluxSubscriberTest {
@Test
public void test10() {
FluxSubscriber subscriber = new FluxSubscriber();
Flux.range(1, 10).subscribe(subscriber.inp);
subscriber.start();
boolean ok = subscriber.blockingAwait(5000);
Assert.assertTrue(ok);
}
static class FluxSubscriber extends Actor {
InpFlow<Integer> inp = new InpFlow<>(this, 5);
int count = 0;
@Override
protected void runAction() throws Throwable {
if (inp.isCompleted()) {
System.out.println("input stream completed");
complete();
return;
}
Integer value = inp.remove();
System.out.println("value="+value);
if (++count==5) {
count = 0;
System.out.println("pause:");
delay(1000);
}
}
}
}
In fact, it reads 5 items first, and then one by one after each call to inp.remove()
. If this is not exactly what you want, then you can extend class InpFlow
to modify the policy when it invokes Subscription.request()
.
Source codes are avalable at https://github.com/akaigoro/df4j (yes I am the author).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.